我们使用 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 上的文本。
有什么翻译建议吗?
最佳答案
您似乎使用的是旧版本的 http://i18next.com 。所以我不确定是否已经允许从其他服务器(CORS)加载。
我建议升级到当前版本并在后端启用“跨域”访问:https://github.com/i18next/i18next-xhr-backend
关于javascript - AngularJS + i18n 本地化从另一台服务器加载 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42939802/