ngx-translate Documentation

repository·develop·Indexed 26 days ago

https://github.com/ngx-translate/core

An internationalization (i18n) library for Angular applications (versions 16-21) that enables translation management via external files. It features a provider-based API including provideTranslateService and provideChildTranslateService for hierarchical or isolated scoping, and supports reactive translations using Angular Signals, Observables, or synchronous snapshots. The ecosystem includes @ngx-translate/http-loader for dynamically loading JSON translation files from a server.

Tokens
5.2K
Snippets
9
Records
51
Agent score
88%

What's inside ngx-translate

  1. Install and use @ngx-translate/http-loader

    develop
    The @ngx-translate/http-loader package allows you to dynamically load translation files from a server as JSON files. This is the standard way to handle internationalization in Angular applications when using ngx-translate to avoid bundling all translation files into the main application bundle.
  2. Configure the Root TranslateService

    develop
    Use provideTranslateService() to create the application's primary translation service. This service acts as the root of the translation hierarchy. You can configure the fallback language, the initial language, and the translation loader. Note that fallbackLang replaces the deprecated defaultLang and useDefaultLang pattern.
  3. Use Isolated Scoping for Translation Services

    develop

    To create a completely independent translation service that does not connect to any parent or inherit translations, use provideTranslateService() within a component's providers array. This creates a standalone store that is isolated from the application root.

    @Component({
      providers: [
        provideTranslateService({
          lang: "de",
          loader: provideTranslateHttpLoader({ prefix: "./feature-i18n/" })
        })
      ]
    })
    export class FeatureComponent {}
  4. Configure a Connected Child TranslateService

    develop
    Use provideChildTranslateService() to create a service that is linked to its parent. Connected services inherit the lang and fallbackLang from their parent. They load their own specific translations via the provided loader, but if a translation key is not found locally, the service will bubble the lookup up to the parent service.
  5. Migrate from TranslateModule to Provider-based API

    develop

    The module-based API (TranslateModule.forRoot() and forChild()) is removed. Use the new provider functions in your providers array for both standalone and module-based applications.

    Old APINew APINotes
    TranslateModule.forRoot()provideTranslateService()Use in providers array
    TranslateModule.forChild({ extend: true })provideChildTranslateService()Connected mode is now default
    TranslateModule.forChild({ isolate: true })provideTranslateService()Creates isolated service
    defaultLang + useDefaultLang: truefallbackLangSimplified API
    defaultLang + useDefaultLang: false(removed)Use fallbackLang: undefined if no fallback desired
  6. Get started with @ngx-translate/core for Angular 16-21

    develop

    For modern Angular applications (versions 16 through 21), use the official documentation at ngx-translate.org. The documentation includes installation guides, interface definitions, and instructions for developing custom plugins.

    If you are upgrading from a previous version, refer to the specific migration guides:

    A step-by-step tutorial for translating an Angular app can be found at Codeandweb's tutorial.

  7. Use Connected (Hierarchical) Scoping for Translation Services

    develop

    To create a service that participates in a translation hierarchy, use provideChildTranslateService() within a component's providers array. This allows translation lookups to bubble up through the component tree. Language changes (like calling use()) on a child will be delegated to the parent, ensuring all connected services share the same language state.

    // feature.component.ts - connected to root
    @Component({
      providers: [
        provideChildTranslateService({
          loader: provideTranslateHttpLoader({ prefix: "./feature-i18n/" })
        })
      ]
    })
    export class FeatureComponent {}
    
    // nested.component.ts - connected to feature
    @Component({
      providers: [
        provideChildTranslateService({
          loader: provideTranslateHttpLoader({ prefix: "./nested-i18n/" })
        })
      ]
    })
    export class NestedComponent {}
  8. Configure @ngx-translate/http-loader via provideTranslateHttpLoader

    develop

    Use provideTranslateHttpLoader to set up an HTTP-based translation loader. This function can be used in two ways:

    1. Single Resource Mode: Provide a prefix and suffix to load a single translation file (defaults to /assets/i18n/ and .json).
    2. Multi-Resource Mode: Provide a resources array to load translations from multiple paths or specific resource configurations.

    Common configuration options include:

    • enforceLoading: If true, appends a timestamp cache-buster to requests.
    • useHttpBackend: If true, uses HttpBackend instead of HttpClient to bypass interceptors.
    • failOnError: If true, a failed HTTP fetch (e.g., 404) propagates the error and fails the whole language load. If false (default), failed resources are replaced with an empty object and a warning is logged.
  9. Configure ngx-translate using provider functions

    develop

    To set up ngx-translate in an Angular application, use the provider functions exported from @ngx-translate/core. These functions allow you to configure the core service, child services, loaders, compilers, parsers, and missing translation handlers.

    Available provider functions:

    • provideTranslateService
    • provideChildTranslateService
    • provideTranslateLoader
    • provideTranslateCompiler
    • provideTranslateParser
    • provideMissingTranslationHandler
  10. Configure a child TranslateService

    develop

    Use provideChildTranslateService to create a localized translation scope for a specific component tree. This allows for different loaders, compilers, or parsers within a sub-section of your application. It accepts a ChildTranslateServiceConfig which is a partial version of the root configuration (it does not support fallbackLang or lang directly in the same way as the root).

    Available configuration options:

    • loader: A custom TranslateLoader.
    • compiler: A custom TranslateCompiler.
    • parser: A custom TranslateParser.
    • missingTranslationHandler: A custom MissingTranslationHandler.