Node.js i18n开发疑问:使用__方法还是直接导入JSON文件?
Great question—consistency is key when working with i18n, so you’re absolutely right to push for a single, standardized approach. Let’s break down the two methods you’re seeing and why one is clearly better suited for your setup:
The Problem with Directly Requiring Locale JSON Files
Your controller’s current approach:
var responses = require('../../locales/en.json'); let message = responses.authorisation.flashes['welcome'];
has several critical drawbacks:
- Hardcoded language: You’re explicitly loading
en.json, so this code will never serve translations for other locales (likenl) without manual, error-prone changes to load the right file. - No i18n library benefits: You’re bypassing all the features your configured
i18nlibrary provides—like automatic locale detection, fallback to the default locale, and dynamic language switching based on user preferences or request headers. - Redundant, messy code: Every time you need a translation, you’ll have to import the correct JSON file, which leads to repeated boilerplate and increases the chance of path errors.
Why Using req.__() is the Superior Approach
The middleware’s use of req.__('organisation.not-found') aligns perfectly with how your i18n library is designed to work, and it solves all the issues above:
- Automatic locale handling: Since you’ve configured
i18nwithlocales: ['en', 'nl']andobjectNotation: true, thereq.__()method will automatically pull the correct translation based on the current request’s locale (e.g., ifreq.localeis set tonl, it will usenl.jsoninstead ofen.json). - Seamless integration with existing tools: It works naturally with
connect-flash(as shown in your middleware), keeping your flash message logic consistent across the codebase. - Clean, maintainable code: No more manual file imports—just call
req.__('path.to.your.translation')(using dot notation for nested keys, thanks toobjectNotation: true) and the library handles the rest. - Scalable: Adding new locales later only requires creating a new JSON file in your
localesdirectory—no changes to business logic needed.
How to Standardize Your Codebase
To bring everything into alignment:
- Replace all direct JSON imports with
req.__(). For your controller example, rewrite it to:
The dot notation matches the nested structure of your JSON files exactly, so this will work the same way as your original code—but with all the benefits of the i18n library.let message = req.__('authorisation.flashes.welcome'); return res.status(200).json({ success: true, message, token, user: userData }); - Ensure
req.__()is available everywhere: Youri18nconfiguration already registers the library globally, but in Express, the method is automatically attached toreqandresobjects for request-specific locale handling. Usingreq.__()is the safest way to ensure translations respect the current user’s language preference. - Enforce the standard: Update your project’s style guide or documentation to specify that all translations must use
req.__()(orres.__()if rendering templates) to keep the codebase consistent.
Final Verdict
Stick exclusively with the req.__() syntax. It’s the idiomatic way to use your i18n library, integrates seamlessly with your existing middleware stack, and makes your codebase more maintainable and scalable long-term.
内容的提问来源于stack exchange,提问作者Marty

