我是i18next的初学者,我想在使用Nodejs + Pugjs开发的小应用程序上管理多种语言。
尝试在pugjs中声明密钥时出现以下错误:Cannot read property 't' of undefined
我的代码:
app.js
i18n
.use(i18nFsBackend)
.use(i18nMiddleware.LanguageDetector)
.init({
fallbackLng: 'en',
debug: true,
ns: ['translation', 'traduction'],
defaultNS: 'translation',
backend: {
loadPath: __dirname + '/locales/{{lng}}/translation.json',
addPath: __dirname + '/locales/{{lng}}.missing.json'
},
interpolation: {
format: function(value, format, lng) {
if (format === 'uppercase') return value.toUpperCase();
if(value instanceof Date) return moment(value).format(format);
return value;
}},
lowerCaseLng: true,
preload: ['en', 'fr'],
sendMissingTo: 'en',
useCookie: false,
detectLngFromHeaders: false,
saveMissing: true}
});
//Routes that should be ignored
app.use(i18nMiddleware.handle(i18n, {
ignoreRoutes: ['images/', 'public/', 'css/', 'js/'],
removeLngFromUrl: false
}));
routes.users.js
//Access i18n inside your route:
app.get("/", function(req, res) {
// current language
var currentLng = req.locale;
// access i18n
var i18n = req.i18n;
// translate
var t = i18n.t("applog.login"); // Comment renseigner my.key
res.send(t);
});
default.pug
doctype
html(lang="fr")
head
meta(charset="utf-8")
link(rel="stylesheet", href="/css/bootstrap.css")
link(rel="stylesheet", href="/css/style.css")
block title
title i18n.t(login)
body
.container
block index
block login
block register
footer
p Login App - JPM2017
script(src="/js/bootstrap.js")
script(src="https://unpkg.com/i18next/i18next.js")
login.pug
extends default
block title
title i18n.t('applog.logapp')
block login
include nav
include flashmess
h2.page-header #{i18n.t('applog.aclog')}
form(method="post", action="/users/login")
.form-group
label Username
input.form-control(type="text", name="username", placeholder="+ #{i18n.t('applog.Uname')}+")
.form-group
label Password
input.form-control(type="password", name="password", placeholder="+ #{i18n.t('applog.Pword')}+")
button(type="submit").btn.btn-default="+ #{i18n.t('applog.submit')}"