0

I am using Angular for German, English, Spanish, French and Italian users How to load the local file dynamically depend of User ' Language settings ? as an example : For German users should Angular load: https://code.angularjs.org/1.4.3/i18n/angular-locale_de-de.js For Italian Users should Angular load https://code.angularjs.org/1.4.3/i18n/angular-locale_it-it.js

I am thinking to use $local.id to define the user language then based on that I load the local file , but I don't know how or if there is any other solution to load the JS file dynamically also is appreciated . regards

JPNN
  • 383
  • 1
  • 7
  • 23
  • I can only recommend ocLazyLoad this allows you to load js files only if you need them so basicly the user logs in to your Site and after you get their language you can load the specific file check the Docs https://oclazyload.readme.io/docs – stackg91 Sep 03 '15 at 14:20
  • Hi @stackg91 I was able to load the js file , however it is look added after executing angular script , because I cann't see the new format defined by the local js file (example : angular-local_de-de.js should show the number as ",") any help please ? – JPNN Sep 03 '15 at 18:51
  • so basicly your file is loaded but not executed? maybe you need to actually call a function in that javascript to get it running but i never had such a problem with lazyload – stackg91 Sep 04 '15 at 06:22

3 Answers3

0

If you have a way to find the language before you load the angular version, you can use a map to find the correct url and just append a new script tag to the end of the body. Adding a script tag will automatically fetch this script and run it.

var language = 'someLanguage',
    url = {
        'someLanguage' : 'languageURL',
        'otherLanguage' : 'otherlanguageURL'
    }[language],
    script = document.createElement('script');
script.setAttribute('src', url);
document.body.appendChild(script);

Maybe Angular has an angular-specific solution for internationalization, but alas I can't help you with that.

Shilly
  • 8,511
  • 1
  • 18
  • 24
0

You just need to load the file using $http. In order to check the users locale you can look into angular-translate call preferredLanguage. It finds out from the user browser. Worth to take a look.

Gerard Sans
  • 2,269
  • 1
  • 13
  • 13
  • Hi Gerard , is there any example you can provided ? many thanks – JPNN Sep 03 '15 at 17:41
  • var l = getLocale(); if (l=='en_EN') { $http.get('url.en-EN.json').then(...)} ... Sorry on mobile – Gerard Sans Sep 03 '15 at 17:54
  • but at my situation my files are javascript Files like if the user is 'de-de' then should added this script dynamically : https://code.angularjs.org/1.4.3/i18n/angular-locale_it-it.js thanks again for your help – JPNN Sep 03 '15 at 18:47
  • It seems you can use $.getScript(url) instead. Create a script tag after all other angular files are loaded just before body closing tag. – Gerard Sans Sep 03 '15 at 19:56
0

you can the angular.translate module for it..

https://angular-translate.github.io/

Akshay Dhankhar
  • 274
  • 1
  • 6