-3

I work on an application that should be translated in some language.

When i used filter like number, currency... the result is show in the default localization.

In the documentation i found that we can import a js to set the localization display of our application.

<html ng-app>
 <head>
….
   <script src="angular.js"></script>
   <script src="i18n/angular-locale_de-de.js"></script>
….
 </head>
</html>

What i want to do is allow the user to define his localization and in that case i think i have to change the localization script imported.

Is there a good way to do this ?

Thanks by advance for your reply

jeremy-denis
  • 6,368
  • 3
  • 18
  • 35

1 Answers1

1

I recommend using angular-translate. Link the script file in your html and import the module in angular:

var app = angular.module('myApp', ['pascalprecht.translate']);

Translate your texts like this:

<ANY>{{'TRANSLATION_ID' | translate}}</ANY>

And set your preferred language

app.config(['$translateProvider', function ($translateProvider) {
  $translateProvider
    .translations('en', translations)
    .preferredLanguage('en');
}]);

You can also use $translateProvider.determinePreferredLanguage();to determine the user language automatically. If you want to use different json files for each locale use asynchronous loading.

EDIT: Switch your locale at runtime by

app.controller('Ctrl', ['$translate', '$scope', function ($translate, $scope) {

  $scope.changeLanguage = function (langKey) {
    $translate.use(langKey);
  };
}]);
  • ok thanks, i already use angular-translate. It's reply to my need for all the text element in my application but not for the date and the number format. i don't found a reply in angular-translate to manage it but maybe i miss something. – jeremy-denis May 08 '16 at 09:59
  • Did you try [this](http://stackoverflow.com/questions/29937780/angularjs-translate-format-dynamic-dates) for the dates? – Patrick Piatkowski May 08 '16 at 10:13
  • or if you want to use the locale scripts try [angular-dynamic-locale](https://github.com/lgalfaso/angular-dynamic-locale). – Patrick Piatkowski May 08 '16 at 10:21
  • yes thanks it's seems reply to my need. The way to manage local script is interesting with angular-dynamic-locale. Thanks again for your reply. – jeremy-denis May 08 '16 at 10:48