【问题标题】:AngularJS + i18n localization loading JSON from another serverAngularJS + i18n 本地化从另一台服务器加载 JSON
【发布时间】: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


【解决方案1】:

您使用的似乎是旧版本的http://i18next.com。所以我不确定是否已经允许从其他服务器(CORS)加载。

我建议升级到当前版本并在后端启用“跨域”访问:https://github.com/i18next/i18next-xhr-backend

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多