[英]node.js i18n: Use __ or import?
I am new to i18n
and have an existing repository that uses it.我是
i18n
新手,并且有一个使用它的现有存储库。 I noticed different syntax for using i18n throughout the repository and am wondering what is the best way.我注意到在整个存储库中使用 i18n 的不同语法,我想知道什么是最好的方法。
I am confused about the structure below and which syntax option is best (I think it's better to be consistent with the syntax and use only 1 option).我对下面的结构以及哪种语法选项最好(我认为最好与语法保持一致并仅使用 1 个选项)感到困惑。 Could someone perhaps explain?
有人可以解释一下吗?
In controllers I find:在控制器中,我发现:
var responses = require('../../locales/en.json');
let message = responses.authorisation.flashes['welcome'];
return res.status(200).json({
success: true,
message,
token,
user: userData
});
In middleware the syntax is as follows:在中间件中,语法如下:
req.flash('error', req.__('organisation.not-found'));
And in app.js
I find:在
app.js
我发现:
const flash = require('connect-flash');
const flashMiddleware = require('./middleware/flashMiddleware');
const i18n = require('i18n');
i18n.configure({
locales: [
'en', 'nl'
],
register: global,
directory: path.join(__dirname, 'locales'),
defaultLocale: 'en',
objectNotation: true,
updateFiles: false
});
flashMiddleware.js
contains (I'm not sure what this does): flashMiddleware.js
包含(我不确定这是做什么的):
const flashMiddleware = (req, res, next) => {
res.locals.flashes = req.flash();
next();
};
module.exports = flashMiddleware;
Both your first and last code blocks don't appear to be using i18n
at all.您的第一个和最后一个代码块似乎根本没有使用
i18n
。 Instead, the first code block is loading the JSON for en
and using it directly.相反,第一个代码块是为
en
加载 JSON 并直接使用它。 It's not clear what the flash stuff is using, I don't see anything in that code doing localization.不清楚 flash 的东西正在使用什么,我在该代码中没有看到任何进行本地化的内容。
The middleware syntax, using __
on req
, appears to be the standard way you use this library in middleware.在
req
上使用__
的中间件语法似乎是您在中间件中使用此库的标准方式。
There are other ways to use it as well that are detailed in the documentation .还有其他使用它的方法,在文档中有详细说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.