我在让 i18next 正确初始化并提取适当的翻译字符串时遇到了一些问题。有几个库:
- 电话间隙 3.x
- 骨干网.js
- 需要.js
- i18next.js
- 车把.js
我的 app.js 具有以下 document.ready 功能:
$(document).ready(function () {
var lang = "",
locale = "en-AU"; // default
// get the user's locale - mobile or web
if (typeof navigator.globalization !== 'undefined') {
// on mobile phone
navigator.globalization.getLocaleName(
function (loc) {locale = loc.value; },
function () {console.log('Error getting locale\n'); }
);
} else {
// in web browser
lang = navigator.language.split("-");
locale = lang[0];
}
console.log("locale: %s", locale);
i18n.init({
lng: locale,
debug: true,
fallbackLng: 'en'
}, function () {
// i18next is done asynchronously; this is the callback function
$("body").i18n();
});
不幸的是,代码甚至没有达到我设置的 document.ready 断点。相反,router.js 定义首先调用 View 类中的初始化代码:
define(function (require) {
"use strict";
var $ = require('jquery'),
Backbone = require('backbone'),
Handlebars = require('handlebars'),
i18next = require('i18next'),
HomeView = require('app/views/HomeView'),
homeView = new HomeView(); // <<-- this line
... Homeview.js:
define(function (require) {
"use strict";
var $ = require('jquery'),
Handlebars = require('handlebars'),
Backbone = require('backbone'),
i18next = require('i18next'),
tplText = require('text!tpl/Home.html'),
template = Handlebars.compile(tplText);
return Backbone.View.extend({
initialize: function () {
this.render();
},
render: function () {
this.$el.i18n(); // << causes an error
this.$el.html(template());
return this;
}
});
});
在页面加载期间,会引发 TypeError:
'Undefined' is not a function (evaluating 'this.$el.i18n()')
知道我在这里做错了什么吗?
编辑: app.js 中的 require.config 块(包括垫片):
require.config({
baseUrl: 'lib',
paths: {
app: '../js',
'i18next': 'i18next.amd-1.7.2',
tpl: '../tpl'
},
map: {
'*': {
'app/models': 'app/models/memory'
}
},
shim: {
'handlebars': {
exports: 'Handlebars'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'underscore': {
exports: '_'
},
'i18next': ['jquery']
}
});