javascript Node.js、Express、i18n 中的国际化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8873851/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:50:38  来源:igfitidea点击:

internationalization in Node.js, Express, i18n

javascriptnode.jsinternationalization

提问by freakish

I'm using Node.js together with Express framework and i18n module. So I use

我将 Node.js 与 Express 框架和i18n 模块一起使用。所以我用

var i18n = require('i18n');
app.configure(function() {
    [...]
    app.use(i18n.init);
    app.use(app.router);
});

in my app settings. Everything works fine, but what I really need is to force i18n to use the langauage I want. The scenario is as follows: when user is not logged in, then i18n searches for the langauage in accept-languageheader and it is ok. But when user is logged in, then I want to hold chosen langauage somewhere in user settings, retrieve it and force i18n module to use this langauage. How to do this (assuming I already know how to save/retrieve the langauge into/from db)?

在我的应用程序设置中。一切正常,但我真正需要的是强制 i18n 使用我想要的语言。场景如下:当用户未登录时,i18n在accept-languageheader中搜索该语言就可以了。但是当用户登录时,我想在用户设置的某处保存所选的语言,检索它并强制 i18n 模块使用该语言。如何做到这一点(假设我已经知道如何在数据库中保存/检索语言)?

回答by freakish

Ach, sorry. I should wait a bit and inspect the i18n module. It seems that module provides to functions

啊,对不起。我应该稍等一下并检查 i18n 模块。似乎模块提供了功能

i18n.getLocale();

which retrieves current language and

检索当前语言和

i18n.setLocale('en');

which sets locale as we want. The documentation really should mention this. It is important, I hope the answer helps someone. :)

根据我们的需要设置语言环境。文档确实应该提到这一点。这很重要,我希望答案对某人有所帮助。:)

回答by staackuser2

This was just released yesterday, but it sounds like it will hit the issue you are running into: locale

这是昨天刚刚发布的,但听起来它会遇到您遇到的问题:语言环境