@larscom/ngx-translate-module-loader
Highly configurable and flexible translations loader for @ngx-translate/core. Fetch multiple translations (http only) and configure them to your needs. Each translation file has it's own namespace out of the box so the key/value pairs do not conflict with each other.
Dependencies
@larscom/ngx-translate-module-loader
depends on @ngx-translate/core and Angular.
Installation
npm i --save @larscom/ngx-translate-module-loader
Choose the version corresponding to your Angular version
@angular/core | @larscom/ngx-translate-module-loader |
---|
>= 12 | >= 3.0.0 |
< 12 | <= 2.2.0 |
Usage
Create an exported moduleHttpLoaderFactory
function
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { ModuleTranslateLoader, IModuleTranslationOptions } from '@larscom/ngx-translate-module-loader';
import { AppComponent } from './app';
export function moduleHttpLoaderFactory(http: HttpClient) {
const baseTranslateUrl = './assets/i18n';
const options: IModuleTranslationOptions = {
modules: [
{ baseTranslateUrl },
{ baseTranslateUrl, moduleName: 'feature1' },
{ baseTranslateUrl, moduleName: 'feature2' }
]
};
return new ModuleTranslateLoader(http, options);
}
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: moduleHttpLoaderFactory,
deps: [HttpClient]
}
})
],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(readonly translate: TranslateService) {
translate.setDefaultLang('en');
}
}
Namespacing
By default, each translation file gets it's own namespace based on the moduleName
, what does it mean?
For example with these options:
export function moduleHttpLoaderFactory(http: HttpClient) {
const baseTranslateUrl = './assets/i18n';
const options: IModuleTranslationOptions = {
modules: [
{ baseTranslateUrl },
{ baseTranslateUrl, moduleName: 'feature1' },
{ baseTranslateUrl, moduleName: 'feature2' }
]
};
return new ModuleTranslateLoader(http, options);
}
Lets say each module in the above example resolves to the following JSON:
{
"KEY": "VALUE"
}
The final translation you are working with would be:
{
"KEY": "VALUE",
"FEATURE1": {
"KEY": "VALUE"
},
"FEATURE2": {
"KEY": "VALUE"
}
}
Even though all JSON files from those modules are the same, they don't conflict because they are not on the same level after they get merged.
Configuration
The configuration is very flexible, you can even define custom templates for fetching translations.
export interface IModuleTranslationOptions {
modules: IModuleTranslation[];
disableNamespace?: boolean;
lowercaseNamespace?: boolean;
deepMerge?: boolean;
version?: string | number;
translateError?: (error: any, path: string) => void;
translateMerger?: (translations: Translation[]) => Translation;
}
export interface IModuleTranslation {
moduleName?: string;
baseTranslateUrl: string;
namespace?: string;
/**
* Custom translation map function after retrieving a translation file
* @param translation the resolved translation file
*/
translateMap?: (translation: Translation) => Translation;
/**
* Custom path template for fetching translations
* @example
* '{baseTranslateUrl}/{moduleName}/{language}'
* or
* @example
* '{baseTranslateUrl}/{language}'
*
* It depends whether you have a moduleName defined
* @see moduleName
*/
pathTemplate?: string;
}
Custom templates for fetching translations
By default, translations gets fetched by using the following template:
'{baseTranslateUrl}/{moduleName}/{language}'
e.g. ./assets/feature1/en.json
You can override this option if you wish to do so:
const options: IModuleTranslationOptions = {
modules: [
{ baseTranslateUrl, pathTemplate: '{baseTranslateUrl}/my-path/{language}' },
{ baseTranslateUrl, moduleName: 'feature1', pathTemplate: '{baseTranslateUrl}/my-path/{language}/{moduleName}' },
{ baseTranslateUrl, moduleName: 'feature2', pathTemplate: '{baseTranslateUrl}/my-path/{language}/{moduleName}' }
]
};