@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. You can disable namespaces or provide your own value as well.
Demo
You can play arround on StackBlitz:
https://stackblitz.com/edit/ngx-translate-module-loader
Dependencies
@larscom/ngx-translate-module-loader
depends on @ngx-translate/core and Angular.
Installation
npm i --save @larscom/ngx-translate-module-loader
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 {}
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 translation:
{
"KEY: "VALUE"
}
The final result would then be:
{
"KEY: "VALUE",
"FEATURE1" : {
"KEY: "VALUE"
},
"FEATURE2" : {
"KEY: "VALUE"
}
}
If you don't want uppercase keys, set lowercaseNamespace
to true in the options because it's uppercase by default.
If you don't want namespaces at all, set disableNamespace
to true.
You can override the default name space by setting the namespace
property in the options.
Configuration
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;
}
Examples
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}' }
]
};