В угловых v1.2 я использую следующий код для обслуживания до локализованных строк в приложении:AngularJS v1.3 ломает переводы фильтра
var i18n = angular.module('i18n', []);
i18n.service('i18n', function ($http, $timeout) {
/**
A dictionary of translations keyed on culture
*/
this.translations = {},
/**
The current culture
*/
this.currentCulture = null,
/**
Sets the current culture, loading the associated translations file if not already loaded
*/
this.setCurrentCulture = function (culture) {
var self = this;
if (self.translations[culture]) {
$timeout(function() {
self.currentCulture = culture;
});
} else {
$http({ method: 'GET', url: 'i18n/' + culture + '/translations.json?' + Date.now() })
.success(function (data) {
// $timeout is used here to defer the $scope update to the next $digest cycle
$timeout(function() {
self.translations[culture] = data;
self.currentCulture = culture;
});
});
}
};
this.getTranslation = function (key) {
if (this.currentCulture) {
return this.translations[this.currentCulture][key] || key;
} else {
return key;
}
},
// Initialize the default culture
this.setCurrentCulture(config.defaultCulture);
});
i18n.filter('i18n', function (i18n) {
return function (key) {
return i18n.getTranslation(key);
};
});
В шаблоне она затем используется следующим образом:
<p>{{ 'HelloWorld' | i18n }}</p>
По какой-то причине, что я не могу понять, обновление до версии 1.3 AngularJS нарушило эту функциональность. Либо $ timeout не запускает цикл дайджеста, либо фильтр не обновляется. Я вижу, что код $ timeout запущен, но код фильтра никогда не попадает.
Любые идеи, почему это может быть нарушено в версии 1.3?
Спасибо!
Работы! Спасибо, Самих! – magritte