Here is an example json i18n language file for English:
{ "project": { "SPONSORINFO": { "MAIN" : "Select the Sponsor Name", "SPONSORLIST": [ {"spons" :"SponsorName 1" }, {"spons" :"SponsorName 2" } ] } } }
and here is my html view:
<div class="form-group" > <label for="form-field-select-1" translate="project.SPONSORINFO.MAIN"> </label> <select class="form-control" ng-model="myModel.sponsors"> <option ng-repeat="s in projectJSON.project.SPONSORINFO.SPONSORLIST" value="{{s.spons}}">{{s.spons | translate}}</option> </select> </div>
translate="project.SPONSORINFO.MAIN"
in the label rightly displays the value "Choose sponsor name" as soon as the language switch is clicked (no update required).
Question:
I use the following function in the view controller to load a language file based on the selected language and pass it to $scope.projectJSON
so that I can call it in ng-repeat
in my html view:
var lang = $translate.use(); $http.get('assets/i18n/'+lang+'.json').success(function(data) { $scope.projectJSON= data; });
The problem is that after switching the language, the drop-down menus do not change to the selected language unless I update or change the appearance and return. How can I fix this problem so that it works as a shortcut ?
applications:
Translate the global configuration in my main.js as follows:
app.config(['$translateProvider', function ($translateProvider) { // prefix and suffix information is required to specify a pattern // You can simply use the static-files loader with this pattern: $translateProvider.useStaticFilesLoader({ prefix: 'assets/i18n/', suffix: '.json' }); // Since you've now registered more then one translation table, angular-translate has to know which one to use. // This is where preferredLanguage(langKey) comes in. $translateProvider.preferredLanguage('en'); // Store the language in the local storage $translateProvider.useLocalStorage(); }]);
in my mainCtrl.js
:
app.controller('AppCtrl', ['$rootScope', '$scope', '$state', '$translate', function ($rootScope, $scope, $state, $translate) { $scope.language = { // Handles language dropdown listIsOpen: false, // list of available languages available: { 'en': 'English', //'it_IT': 'Italiano', 'de_DE': 'Deutsch' }, // display always the current ui language init: function () { var proposedLanguage = $translate.proposedLanguage() || $translate.use(); var preferredLanguage = $translate.preferredLanguage(); // we know we have set a preferred one in app.config $scope.language.selected = $scope.language.available[(proposedLanguage || preferredLanguage)]; }, set: function (localeId, ev) { $translate.use(localeId); $scope.language.selected = $scope.language.available[localeId]; $scope.language.listIsOpen = !$scope.language.listIsOpen; } }; $scope.language.init();