【发布时间】:2015-06-19 05:13:12
【问题描述】:
好吧,我创建了一个独立的简单项目来测试它(代码片段如下),因为我在主项目中遇到了完全相同的问题,所以我认为其他组件之一可能会导致这个问题,但我也有同样的问题孤立项目中的问题,现在我不知道如何使它工作。 初始化和配置是这样的:
i18n.init({
ns: {
namespaces: ['ns.common', 'ns.special'],
defaultNs: 'ns.special'
},
resSetPath: 'locales/__lng__/new.__ns__.json',
saveMissing: true,
debug: true,
sendMissingTo: 'fallback',
preload: ['en', 'de'],
detectLngFromPath: 0,
ignoreRoutes: ['img/', 'img', 'img/', '/img/', 'css/', 'i18next/']
});
// Configuration
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(i18n.handle); // have i18n befor app.router
app.set('view engine', 'ejs');
app.set('views', __dirname);
i18n.registerAppHelper(app)
.serveClientScript(app)
.serveDynamicResources(app)
.serveMissingKeyRoute(app);
i18n.serveWebTranslate(app, {
i18nextWTOptions: {
languages: ['de-DE', 'en-US', 'dev'],
namespaces: ['ns.common', 'ns.special'],
resGetPath: "locales/resources.json?lng=__lng__&ns=__ns__",
resChangePath: 'locales/change/__lng__/__ns__',
resRemovePath: 'locales/remove/__lng__/__ns__',
fallbackLng: "dev",
dynamicLoad: true
}
});
ejs 是这样的:
html
<body>
<span>Should show a hello world</span></br>
<span>;<%t('hello.world')%></span>
</body>
我像这样添加了 t 函数(更正了 t 未定义的错误):
app.locals.t = function(key){
return i18n.t(key);
};
一切都应该在网站上显示下一个视图:
https://cloud.githubusercontent.com/assets/2654171/7738735/bef1a7ce-ff25-11e4-89f8-257502b27396.PNG
但它只是显示以下内容:
https://cloud.githubusercontent.com/assets/2654171/7738741/d0685f84-ff25-11e4-9c3d-1a39ca7c1bb1.PNG
我非常感谢一些建议或使用 i18n 的 ejs 示例。 顺便说一句,我正在使用: -快递 4 -ejs 1 -i18next 1.7.10
【问题讨论】: