gpt4 book ai didi

javascript - node.js i18n : Use __ or import?

转载 作者:行者123 更新时间:2023-12-02 22:11:56 24 4
gpt4 key购买 nike

我是 i18n 的新手,并且有一个使用它的现有存储库。我注意到在整个存储库中使用 i18n 的语法不同,我想知道最好的方法是什么。

我对下面的结构以及哪种语法选项最好感到困惑(我认为最好与语法保持一致并仅使用 1 个选项)。有人可以解释一下吗?

在 Controller 中我发现:

var responses = require('../../locales/en.json');

let message = responses.authorisation.flashes['welcome'];
return res.status(200).json({
success: true,
message,
token,
user: userData
});

在中间件中,语法如下:

req.flash('error', req.__('organisation.not-found'));

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 包含(我不确定它的作用):

const flashMiddleware = (req, res, next) => {
res.locals.flashes = req.flash();

next();
};

module.exports = flashMiddleware;

最佳答案

您的第一个和最后一个代码块似乎根本没有使用i18n。相反,第一个代码块正在加载 en 的 JSON 并直接使用它。目前尚不清楚 Flash 的内容正在使用什么,我在该代码中没有看到任何进行本地化的内容。

中间件语法(在 req 上使用 __)似乎是在中间件中使用此库的标准方式。

还有其他使用方法,the documentation中有详细介绍。 .

关于javascript - node.js i18n : Use __ or import?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59529862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com