@larscom/ngx-translate-module-loader
Highly configurable and flexible translations loader for @ngx-translate/core. Fetch multiple translations (http only)
Each translation file has it's own namespace out of the box so the key/value pairs do not conflict with each other. Namespacing can be disabled or you can provide your own value.
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
1. 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 { FileType, ModuleTranslateLoader, IModuleTranslationOptions } from '@larscom/ngx-translate-module-loader';
import { AppComponent } from './app';
export function moduleHttpLoaderFactory(http: HttpClient) {
const fileType = FileType.JSON;
const baseTranslateUrl = './assets/i18n';
const options: IModuleTranslationOptions = {
modules: [
{ baseTranslateUrl, fileType },
{ moduleName: 'feature1', baseTranslateUrl, fileType },
{ moduleName: 'feature2', baseTranslateUrl, fileType }
]
};
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 fileType = FileType.JSON;
const baseTranslateUrl = './assets/i18n';
const options: IModuleTranslationOptions = {
modules: [
{ baseTranslateUrl, fileType },
{ moduleName: 'feature1', baseTranslateUrl, fileType },
{ moduleName: 'feature2', baseTranslateUrl, fileType }
]
};
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 need upper case keys, set nameSpaceUppercase
to false in the options because it's upper case by default.
If you don't want to enable namespaces at all, set enableNamespacing
to false.
You can override the default name space by setting the nameSpace
property in the options.
Configuration
export interface IModuleTranslationOptions {
modules: IModuleTranslation[];
enableNamespacing?: boolean;
nameSpaceUppercase?: boolean;
deepMerge?: boolean;
translateError?: (error: any, path: string) => void;
translateMerger?: (translations: Translation[]) => Translation;
modulePathTemplate?: string;
pathTemplate?: string;
}
export interface IModuleTranslation {
moduleName?: string;
baseTranslateUrl: string;
fileType: FileType;
nameSpace?: string;
translateMap?: (translation: Translation) => Translation;
}
Examples
Custom templates for fetching translations
By default, module translations gets fetched by using the following template:
'{baseTranslateUrl}/{moduleName}/{language}{fileType}'
e.g.: ./assets/feature1/en.json
You can override this option if you wish to do so:
const options: IModuleTranslationOptions = {
...
modulePathTemplate: '{baseTranslateUrl}/{language}/{moduleName}{fileType}'
};