ngx-translate: translate your JSON file to other languages

For Angular 2/3/4/5/6/7 developers, use this gadget to translate your en.json file.

This is relevant for developers that use @ngx-translate/core similar to the following in app.module:

import { TranslateModule, TranslateLoader } from '@ngx-translate/core';

@NgModule({
/* ... */
  imports: [
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    })
/* ... */

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

and the en.json file looks something like:

{
  "PLAIN_STRING" : "I am an English string!",
  "WITH_HANDLEDBARS": "I have some {{placeholders}} in me {{that}} shouldn't be translated!",
  "GROUP_OF_STRINGS": {
    "ANOTHER_STRING": "I need to be translated too."
  }
}

0 thoughts on “ngx-translate: translate your JSON file to other languages”

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: