Mahesh Walasang
Mahesh Walasang

Reputation: 11

Angular build in cli way of i18n language translation depending on browser locale or browser default language

I am using angular's build in i18n language translation. The way this blog post explains but I want to switch the language by default looking into browser's default language. If my browser locale is French (fr) and when I browse my site as https://www.myi18nexample.com, content of the page should get loaded with french language labels/constants/strings. Can you please suggest how can I achieve this ?

Other few points to consider:

  1. I want to show alert messages as per browser default language.
  2. I want to show API response messages as alert or want to display as default language translated text. Ex: language ru: API response is Success but should alert message show as 'Успех'
  3. Deployed app should not have path like https://www.myi18nexample.com/en/index.html or https://www.myi18nexample.com/ru/index.html should be as normal when we deploy without multi language deployment. And routing should also be normal without /en or /ru should be just https://www.myi18nexample.com/about_us as example

Can it behave as we deploy with ngx-translator library used.

Can we differentiate what is possible and what is not possible with i18n and ngx-translate. Compare both as apple to apple comparison.

browserlocale, localization by default browser language, no slash in url for language change, should behave as we deploy without localization

thanks

Upvotes: 1

Views: 1239

Answers (1)

Michael Karén
Michael Karén

Reputation: 1185

Angular i18n does not behave like ngx-translate. If you want that kind of behavior you should use it or have a look at Transloco.

You get build time translations and each language gets deployed in its own app, hence the URL paths to different languages.

The Angular docs when it comes to i18n are comprehensive and have examples of how you can set up the redirect to the browser language: https://angular.io/guide/i18n#configuring-servers

Upvotes: 0

Related Questions