Reputation: 12998
I'm trying to use i18next (https://github.com/archer96/ng-i18next) in my Angular project, but it seems to be loading too slow. This is my setup:
angular.module('jm.i18next').config(['$i18nextProvider', function ($i18nextProvider) {
$i18nextProvider.options = {
lng: 'en',
fallbackLng: 'en',
preload: ['en'],
supportedLngs: ['en'],
resGetPath: '../locales/__lng__.json',
useCookie: false,
useLocalStorage: false,
defaultLoadingValue: ''
};
}]);
angular.module('myApp', ['jm.i18next']).controller('MainCtrl', ['$scope', '$i18next', function ($scope, $i18next) {
console.log($i18next("key"));
setTimeout(function() {
console.log($i18next("key"));
}, 1000);
}]);
I have to add a timeout to have a value. Is there a way to make sure i18next is ready when the controller is loaded?
UPDATE
I'm trying to group workouts by type, which is translated using $i18next. But this doesnt work, since the view is "ready" before the controller has done the translation.
<select ng-model="workout" ng-options="workout as workout.name group by workout.type for workout in workouts | orderBy: ['type', 'name']"></select>
$scope.workouts = [
{id: 1, name: 'Workout 1', type: $i18next("type1")},
{id: 25, name: 'Workout 2', type: $i18next("type2")},
];
Upvotes: 3
Views: 3163
Reputation: 3743
You could also listen to the i18nextLanguageChange
event:
$scope.$on('i18nextLanguageChange', function () {
$scope.workouts = [
{id: 1, name: 'Workout 1', type: $i18next("type1")},
{id: 25, name: 'Workout 2', type: $i18next("type2")},
];
});
You'll have to tackle other race conditions on your own that way.
Upvotes: 0
Reputation: 12998
This solved my problem.
angular.module('myApp', ['ngRoute', 'jm.i18next']).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/route1', {
title: 'Route 1',
templateUrl: '../views/route1.html',
controller: 'Route1Ctrl',
resolve: {
i18next: function ($i18next) {
return $i18next;
}
}
});
}]);
Upvotes: 1