Node.js 如何在Pug模板文件中使用i18n变量?
我是Node.js的新手,我正在尝试学习如何在我的帕格模板中使用,但在任何地方都找不到我的答案 文件上说 在模板中(取决于模板引擎) 这些语法都不起作用,正确的语法是什么 我知道这是很好的配置,因为当使用Node.js 如何在Pug模板文件中使用i18n变量?,node.js,internationalization,pug,Node.js,Internationalization,Pug,我是Node.js的新手,我正在尝试学习如何在我的帕格模板中使用,但在任何地方都找不到我的答案 文件上说 在模板中(取决于模板引擎) 这些语法都不起作用,正确的语法是什么 我知道这是很好的配置,因为当使用 i18n.__('Hello') 并将其以一个变量的形式发送到我的模板中。答案在文档中是正确的,只需将其添加到我的配置中即可 app.use(function(req, res, next) { // express helper for natively supported eng
i18n.__('Hello')
并将其以一个变量的形式发送到我的模板中。答案在文档中是正确的,只需将其添加到我的配置中即可
app.use(function(req, res, next) {
// express helper for natively supported engines
res.locals.__ = res.__ = function() {
return i18n.__.apply(req, arguments);
};
next();
});
除了Sam的答案之外,请记住您应该使用
{{(“Hello”)}
模板语法来使用此i18n帮助程序。不确定这是否有用。只是想分享。我遇到了同样的问题,我可以使用req.i18n._uu()查看翻译后的文本,但无法使用i18n-2查看pug中的文本。我是如何解决的:
1.确保i18n配置在app.use(cookieParser())之后
你只需要npm我新的一个和注册全球如下。然后,您可以使用
\uuu('Hello')
祝你好运 这里的“论点”是什么?
i18n.__('Hello')
app.use(function(req, res, next) {
// express helper for natively supported engines
res.locals.__ = res.__ = function() {
return i18n.__.apply(req, arguments);
};
next();
});
// add i18n config after app used the cookieParser
var i18n = require('i18n-2');
// Attach the i18n property to the express request object
// And attach helper methods for use in templates
i18n.expressBind(app, {
// setup some locales - other locales default to en silently
locales: ['en', 'zh'],
defaultLocale: 'en',
// change the cookie name from 'lang' to 'locale'
cookieName: 'locale'
});
app.use(function(req, res, next) {
req.i18n.setLocaleFromQuery();
req.i18n.setLocaleFromCookie();
next();
});
const i18n = require("i18n");
app.use(i18n.init);
i18n.configure({
locales: ['en', 'de', 'vi'],
directory: './locales',
register: global
});
i18n.setLocale('vi');