AngularJS V1.3打破翻译过滤器 [英] AngularJS v1.3 breaks translations filter
问题描述
在角V1.2我用下面的code为应用程序提供本地化的字符串:
In Angular v1.2 I was using the following code for serving up localised strings in the application:
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);
};
});
在模板它随后被用于如下:
In the template it is then used as follows:
<p>{{ 'HelloWorld' | i18n }}</p>
由于某些原因,我无法捉摸,升级到AngularJS的V1.3已经打破了这个功能。无论是$超时不触发摘要周期,或过滤器没有更新。我可以看到$超时code正在运行,但过滤器code从不被击中。
For some reason that I can't fathom, upgrading to v1.3 of AngularJS has broken this functionality. Either the $timeout isn't triggering a digest cycle, or the filter isn't updating. I can see that the $timeout code is running, but the filter code never gets hit.
任何想法,为什么这可能在1.3版被打破?
Any ideas why this might be broken in v1.3?
谢谢!
推荐答案
在角1.3滤波被改变,使他们不再状态。你可以看到更多的信息在这个问题:什么是AngularJS状态过滤
In angular 1.3 the filtering was changed so that they are no longer "stateful". You can see more info in this question: What is a stateful filtering in AngularJS?
的最终结果是,过滤器将不再除非输入的变化重新评估。为了解决这个问题,你可以添加一行:
The end result is that filter will no longer re-evaluate unless the input changes. To fix this you can add the line:
i18n.filter('i18n', function (i18n) {
var filter = function (key) {
return i18n.getTranslation(key);
};
filter.$stateful = true; ///add this line
return filter;
});
要不你实现过滤一些其他的方式。
Or else implement your filter some other way.
这篇关于AngularJS V1.3打破翻译过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!