Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to switch AngularJS's internationalization language on the fly at runtime?

Is it possible to switch between i18n/angular-locale_en-us.js and i18n/angular-locale_de-de.js, for example, at runtime and have all date, currency and number filters update instantly? (Instantly means after the new locale file loaded of course).

Thank you.

like image 600
Francisc Avatar asked Mar 13 '14 11:03

Francisc


People also ask

Does AngularJS support internalization?

AngularJS supports inbuilt internationalization for three types of filters currency, date and numbers. We only need to incorporate corresponding js according to locale of the country. By default it handles the locale of the browser.

What is internationalization how can it be implemented in AngularJS?

To internationalize them, you will have to replace both string literals with tokens that AngularJS can then replace with the actual translated strings, based on the user's preference, while rendering the page. AngularJS can do this by using your tokens to perform a lookup in translation tables that you provide.


2 Answers

I managed to do this using this module: https://github.com/lgalfaso/angular-dynamic-locale

Works perfectly!

like image 143
Jesús Carrera Avatar answered Oct 05 '22 04:10

Jesús Carrera


It is and it is not. If you stick with built-in solution for i18n (which to be honest is somewhat broken), there is no way to switch locales. You may dynamically load the desired script, but once Angular resolve all bindings it is already too late (it won't use newly loaded locale script).

To overcome this problem, you may want to create your own filters and use something like Globalize or iLib to do the formatting.

like image 22
Paweł Dyda Avatar answered Oct 05 '22 04:10

Paweł Dyda