Search code examples
angularinternationalizationangular2-aotxliff

How to provide an alternate i18n language link in Angular with aot compiling?


I am currently working on i18n of my Angular app. I use AOT compiling with xlf files to create pre-compiled apps as described here. In the build, I provide a language specific base href (using the --base-href parm). For example, this means, I end up with two language specific apps using the following url paths:

  • /app_path/en/...
  • /app_path/de/...

Now I want to provide a link to the respective alternate language within my app by replacing for example en by de in the active url. I used the Router injection, so I can access its url property, but this property does not give me the full url including the base-href.

How can I find out the base url from within an Angular app?

Alternatively, is there a way to I find out language the app was built for? Can I access the target-language property from the xliff files somehow?


Solution

  • When you compile your app for the various languages you set the locale in the ng build process:

    ./node_modules/.bin/ngc --i18nFile=./locale/messages.es.xlf --locale=es --i18nFormat=xlf
    

    You can get access to this locale by injecting it to your component:

    import { Inject, LOCALE_ID } from '@angular/core';
    ...
    constructor(@Inject(LOCALE_ID) locale: string, ...
    

    Then you can use the locale string to determine the current language. In the example above the locale was set to "es"

    Hope this helps