AngularJS + i18n 本地化从另一台服务器加载 JSON



我们使用 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

相关内容

最新更新