问题描述
我正在尝试本地化 Angular 中的复数字符串.我正在使用 ng-pluralize 指令来处理复数和本地化,我在运行时根据用户语言环境将字符串传递给指令.但是我收到错误TypeError: Object # has no method 'one'",即使 $scope 加载了翻译的字符串.以下是我的 html 代码,
Hi I am trying to localize the strings for pluralization in Angular. I am using ng-pluralize directive to handle pluralization and for localizations I am passing the strings to the directive at runtime based on user locale. But I am getting error "TypeError: Object # has no method 'one'" even if the $scope is loaded with translated strings.Following is my html code,
<input class="input-text" type="number" ng-model="candyCount" ng-keypress="loadStrings()"/>
<ng-pluralize count = "candyCount" when = "translateStrings" ></ng-pluralize>
Javascript 代码
Javascript code
myApp.controller(‘MyCtrl’,function($scope){
$scope.loadStrings = function(){
$scope.translateStrings = null;
if (userLocale == "en-us"){
$scope.translateStrings =
{'0': '0 candy for sale','one': '1 candy for sale.','other': '{} candies for sale.'} ;
debugger;
}
else if (userLocale == "de-de"){
$scope.translateStrings = {'0': '0 Süßigkeiten ausgewählt',
'one': '1 Süßigkeiten zum Verkauf',
'other': '{} Süßigkeiten zum Verkauf.'
};
debugger;
}
}
});
我已经向每个条件块添加了调试器,所以当我在控制台中检查 $scope.translateStrings 时,我得到的输出是,对于我们:
I have added debugger to every condition block, so when I check for $scope.translateStrings in the console, I get output as,For en-us:
$scope.translateStrings
{'0': '0 candy for sale','one': '1 candy for sale.','other': '{} candies for sale.'}
是因为指令没有用最新的字符串更新,还是我哪里出错了.
Is it because the directive is not getting updated with the latest strings, or am I going wrong somewhere.
推荐答案
如果你想异步加载翻译字符串,你需要重新编译 ng-pluralize.
If you want async loading of translation string you need to recompile ng-pluralize.
这是一个演示:http://plnkr.co/edit/0pFdk4Ac1QC5SE7JSXeJ?p=preview
这篇关于ng-pluralize with translations 错误:“TypeError"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!