使用 express 在 node.js 中进行本地化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11645740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Localization in node.js with express
提问by Javier Manzano
Which package do you use for localization with express in node.js?
你使用哪个包在 node.js 中使用 express 进行本地化?
Thanks!
谢谢!
采纳答案by Ruben Verborgh
node-localizecan work together with express.
node-localize可以与 express 一起使用。
Depending on where you want the localization, jqtpl-express-i18ncan do the job for templating.
根据您想要本地化的位置,jqtpl-express-i18n可以完成模板工作。
回答by liangzan
https://github.com/jeresig/i18n-node-2
https://github.com/jeresig/i18n-node-2
John Resig's implementation
John Resig 的实现
His blog post on it
他的博客文章
回答by Alessio Campanelli
Actually I use this NPM Package i18n
实际上我使用这个 NPM Package i18n
It have a very simple usage with Express framework... create locales folder (it.json, en.json, etc...)
它与 Express 框架有一个非常简单的用法...创建语言环境文件夹(it.json、en.json 等...)
// load modules at bootstrap
var app = express();
var i18n = require("i18n");
//set configuration
i18n.configure({
locales:['en', 'de'],
directory: __dirname + '/server/locales'
});
app.use(i18n.init);
// and then, in controller we can use response
res__('YOUR_KEY')
Front End side just set the HTTP header Accept-Languagewith value 'en', 'it', etc.
前端侧只需Accept-Language使用值“en”、“it”等设置 HTTP 标头。
回答by saaaaaaaaasha
I used localizifylibrary in own project, it's very light.
我在自己的项目中使用了localizify库,它非常轻巧。
const localizify = require('localizify');
// ...
app.configure(() => {
app.use((request, response, next) => {
const lang = request.headers['accept-language'] || 'en';
localize.setLocale(lang);
next();
});
});
回答by hkaraoglu
You can use language-translatorlibrary. It uses json files to load texts. You can define languages whatever you want.
您可以使用语言翻译库。它使用 json 文件来加载文本。你可以定义任何你想要的语言。
- It supports parametrized requests.(that contains :parameter)
- It supports output text both of route file and view file.
- It is fully customizable.
- It uses and manage cookie to know user preference.
- It loads language file in middleware function by matching your route path and language file. No needs to require language files in every route file.
- It translates default language's json files' texts by using Yandex translate API. (Free)
- 它支持参数化请求。(包含:参数)
- 它支持路由文件和视图文件的输出文本。
- 它是完全可定制的。
- 它使用和管理 cookie 以了解用户偏好。
- 它通过匹配您的路由路径和语言文件在中间件功能中加载语言文件。不需要在每个路由文件中都需要语言文件。
- 它使用 Yandex translate API 翻译默认语言的 json 文件的文本。(自由)

