backbone.js - i18next 未加载翻译文件

标签 backbone.js requirejs i18next

我正在开发一些基于 Backbone 的项目,在该项目中我将 i18next 用于语言环境。

以下是我的 app.js 代码:

    /*
    This file is used to initialize your application.
*/
require(['i18n','application','handlebars_Helpers'], function(i18n, Application) {

    i18n.init({
        lng: 'en',
        debug: true,
        fallbackLng: false,
        load:'unspecific',
        resGetPath: "locales/__lng__/__ns__.json",
        ns: {
            namespaces: ['translation']
        }
    });

    (new Application()).initialize();
});

翻译文件:
{
    "loginModule": {
        "signin": "Sign In"
    }
}

以下是我的帮助文件:
/**
 * Set of generic handlebars helpers
 */
define(['i18n'], function(i18n) {
    /**
     * This helper provides i18Next in templates
     *
     *
     * Usage: span {{t "my.key" }}
     */
    Handlebars.registerHelper('t', function(i18n_key) {
        var result = i18n.t(i18n_key);
        return new Handlebars.SafeString(result);
    });

    return Handlebars;

});

当我通过 localhost 加载我的页面时,它会在控制台中显示以下消息:
currentLng set to: en i18n.js:490
GET http://localhost:8000/locales/en/translation.json?_=1374495189376 404 (Not Found) i18n.js:376
failed loading: locales/en/translation.json

不明白我错过了什么?或者为什么会显示这个错误?

最佳答案

您将翻译文件存储在哪个文件夹中? i18n 的默认行为是,它尝试在特定路径中查找本地化文件:/locales/{lang-code}/{namespace}.json

如果您将文件保存在 root 中,请尝试将初始化代码更改为以下内容:

    i18n.init({
    lang: 'en',
    debug: true,
    fallbackLng: false,
    load:'unspecific',
    resGetPath: "__ns__-__lng__.json",
    ns: {
        namespaces: ['translation'],
        defaultNs: 'translation'
    }
});

这将尝试从以下网址加载文件:http://localhost:8000/translation-en.json
基本上,尝试检查翻译文件的位置,翻译文件的名称并相应地构建'regGenPath',更多信息可以在i18n文档http://i18next.com/node/pages/doc_init.html中找到。

关于backbone.js - i18next 未加载翻译文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17787326/

相关文章:

javascript - 以编程方式更改初始化选项

reactjs - 如何找到 i18next 丢失的键

javascript - 除了自定义节点服务器之外,还有替代方法 next-i18next 来获取语言包吗?

javascript - 永久链接集合中所有模型的 Backbone.js 唯一属性名称

javascript - 使用 jQuery.load 在另一个模板中显示一个模板

node.js - 在 node.js 服务器上重用主干路由、模型和 View

javascript - RequireJS:加载库模块后自动加载配置脚本

cordova - 如何在文件 :///protocol (Cordova/Phonegap) 下使用 dojo 的自定义构建

javascript - 让我的主干 View 加载测试数据?

javascript - 如何以编程方式将 html 注入(inject) qunit-fixture