簡體   English   中英

node.js i18n:使用 __ 還是導入?

[英]node.js i18n: Use __ or import?

我是i18n新手,並且有一個使用它的現有存儲庫。 我注意到在整個存儲庫中使用 i18n 的不同語法,我想知道什么是最好的方法。

我對下面的結構以及哪種語法選項最好(我認為最好與語法保持一致並僅使用 1 個選項)感到困惑。 有人可以解釋一下嗎?

在控制器中,我發現:

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上使用__的中間件語法似乎是您在中間件中使用此庫的標准方式。

還有其他使用它的方法,在文檔中有詳細說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM