【发布时间】:2017-08-13 20:49:58
【问题描述】:
我们正在使用 angular 1.2.16 和 i18next 0.2.6 进行开发。在我们的应用程序中,当 JSON 来自同一服务器时,本地化工作正常。
现在我们遇到了新要求,即从另一个内容服务器加载资源字符串,即 JSON 文件。假设从“http://mysite/locales/en-us/sample.json”加载 JSON
<head>
<meta charset="utf-8"/>
<title>i18next test</title>
<script src="i18next.js"></script>
<script src="angularjs/1.2.16/angular.min.js"></script>
<script src="ngI18next.js"></script>
<script>
angular.module('jm.i18next').config(function ($i18nextProvider) {
'use strict';
$i18nextProvider.options = {
lng: 'dev',
useCookie: false,
useLocalStorage: false,
fallbackLng: 'dev',
resGetPath: '../locales/__lng__/__ns__.json',
ns: {
namespaces: ['messages', 'options'],
defaultNs: 'messages'
}
};
});
angular.module('MyApp', ['jm.i18next']).controller('MyProviderCtrl', function ($rootScope, $scope, $i18next) {
$rootScope.$on('i18nextLanguageChange', function () {
$scope.hello = $i18next('messages:header.name');
});
});
</script>
</head>
<body ng-app="MyApp">
<div ng-controller="MyProviderCtrl">
<div>{{hello}}</div>
<div ng-i18next="options:moment-i18n"></div>
<div ng-i18next="messages:header.name"></div>
<div ng-i18next="header.name"></div>
</div>
</body>
我尝试将 resGetPath: '../locales/__lng__/__ns__.json' 更改为 resGetPath: 'http://mysite/locales/en-us/sample.json' 但是它从服务器加载 JSON 文件它没有在 UI 上翻译文本。
有什么翻译建议吗?
【问题讨论】:
-
我会说,在您的本地服务器上以某种方式加载了文件,然后事件
i18nextLanguageChange触发,而另一台服务器则发生相反的情况。尝试收听onload(i18next.com/docs/api/#on-loaded) 事件,然后手动触发i18nextLanguageChange事件?
标签: javascript angularjs json internationalization i18next