【发布时间】:2016-02-17 12:17:36
【问题描述】:
我正在使用 angular-translate 添加对 3 种语言(英语、葡萄牙语和西班牙语)的支持,一切正常。
// config angular translate
app.config(['$translateProvider', function ($translateProvider) {
// configures staticFilesLoader
$translateProvider.useStaticFilesLoader({
prefix: 'translations/locale-',
suffix: '.json'
});
$translateProvider.preferredLanguage('en');
}]);
这样我就有了 3 个文件,其中包含 3 种语言的所有翻译。但我现在面临的问题是,
如果我需要翻译来自 json 文件并通过 $routeParams 和 ngRoute 动态更改的数据怎么办?
这是我在进入 i18n 之前所做的事情
// MyController
app.controller('MyController', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
$http.get('someFolder/' + $routeParams.itemId + '.json').success(function(data) {
$scope.item = data;
});
}]);
其中一个 json 文件
{
"id": "blue",
"color": "Blue",
"description": "The color blue is one of trust, honesty and loyalty"
}
我有一个包含 json 文件的文件夹,其中加载了项目的 ID。我的 html 看起来像这样:
<dl class="dl-horizontal" ng-controller="MyController">
<dt>{{ 'item.label.color' | translate }}</dt>
<dd>{{ item.color }}</dd>
<dt>{{ 'item.label.description' | translate }}</dt>
<dd>{{ item.description }}</dd>
</dl>
我能够翻译项目的标签,因为它在所有 json 文件中始终相同,但我真正需要的是翻译值。
有什么解决办法吗?
提前致谢
【问题讨论】:
标签: javascript angularjs json ngroute angular-translate