Can I set $locale for some application manually?
Is it possible that only way to support locals is to include localization file from angular library for current locale. What if there are multiple cultures? In that case I have to load localization files dynamically? What am I missing?
Honestly, the $locale service in angular is pretty primitive still. It's really good, but it seems to lack flexibility in this area. The biggest issue is that even if you switch your locale by dynamically reloading the proper locale file, things like the date filter won't know you've changed it because they're registering their locale information when they're set up. So you have a couple of choices currently: 1. Reload the page with the selected locale... or 2. Write your own Locale Provider and Filters that use it.
It might be possible create a service that would dynamically load the proper script file, reinitialize all affected filters and services, then refresh the views, but I'm not really sure what all that would involve at this point.
You can load the locale you want into
localStorage
, then refresh the page. Have the script below load the i18n file you need. Changing the locale on the fly isn't supported yet.I've built an angular module that takes care about i18n. AngularJS support for i18n is pretty primitve, if you want to have more control and also be more flexible, checkout angular-translate - http://angular-translate.github.io/
Let me know, if I can help out!
For anyone looking for dynamic localization today angular-dynamic-locale does a great job.
I struggled with the same issues, read all the answers here and introduced i18n/l10n in my project. This are my outcomes:
So the solution is to use both projects, angular-translate and angular-dynamic-locale.
Including this script: /out/aHR0cHM6Ly9naXRodWIuY29tL2xnYWxmYXNv/angular-dynamic-locale/blob/master/src/tmhDynamicLocale.js enables locales to be set during run time.
/assets/js/locales/filename-LOCALE.js
- note: what you write as the LOCALE in the filename is important - any locales required will get downloaded dynamically.tmhDynamicLocale
as'tmh.DynamicLocale'
e.g.var app = angular.module('app',['tmh.DynamicLocale'])
;tmhDynamicLocaleProvider
' and set the location of your locale files, e.g.tmhLocaleProvider.localeLocationPattern('/assets/js/locales/angular-locale-{{locale}}.js');
{{locale}}
will be swapped out for the locale you set at run time.app.run()
, pass in the'tmhDynamicLocale'
service e.g.app.run(['tmhDynamicLocale',function(tmhDynamicLocale){}]);
tmhDynamicLocale.set('en-gb');
. Alternatvely, astmhDynamicLocale
is a service, you could set the locale in different places service injections are allowed, e.g. controllers, although bear in mind, services are singletons, and setting the locale in a controller will set it across the app.You should now have the correct locale running. For further info, use the README for
tmhDynamicLocale
:https://github.com/lgalfaso/angular-dynamic-locale/blob/master/README.mdCredit: Lucas Mirelmann for
tmhDynamicLocale
.I found something interesting. It is not angular but it is jquery so integration should be ok. I will test performances and get back with info.
https://github.com/js-coder/x18n/wiki/Getting-started
https://github.com/js-coder/jQuery.x18n
If you want to load the angularJS localization of your browser, install https://github.com/angular/bower-angular-i18n and https://github.com/lgalfaso/angular-dynamic-locale in your project.
Read the documentation of each library to install. This is an example of my Ionic project:
In my index.html:
In my app.js
Angular provide great support for i18n/l10n. You can find the guide here
The requirements for our application determines how we can implement this support.
You can find a good explanation of how we can achieve such things in one of my previous answers here.
You can find an example fiddle for such implementation using a filter can be found here
If you are using webpack and typescript, you can load your locale dynamically.
you can also use the translation service to get the browser's locale then use it to do what you need. . . for example, in a ShortDatePipe: