简体   繁体   English

国际化普通Express.js

[英]Internationlisation plain Express.js

Assume I have the following code: 假设我有以下代码:

HTML HTML

<ul id="navMenu">
    <li>{{inter.home}}</li>
    <li>{{inter.aboutUs}}</li>
</ul>

express 表达

router.get('/:lang/news', function (req, res) {
var language = req.params.lang;

var interObjects = {
    ru: russianInterObj,
    en: englishInterObj
};

(interObjects[language]) ? res.render('view.hjs', {inter: interObjects[language], ...}) 
                         : res.sendStatus(404);
});

Would this approach for internationlisation be good? 这种国际化的方法会好吗?

Thank you 谢谢

There are 2 points of view on created localized pages : 在创建的本地化页面上有两种观点:

External (consumer) 外部(消费者)

You will have a domain.com/lang/page.html type of URL, which is what is expected. 您将具有domain.com/lang/page.html URL类型,这是预期的。 In this regard, your approach is undoubtedly good. 在这方面,您的方法无疑是好的。

Internal (provider / developper) 内部(提供者/开发者)

In regard of maintaining code however, while your approach is good, it might be hard to extend it beyond a few lines. 但是,在维护代码方面,虽然您的方法不错,但可能很难将其扩展到几行之外。

If you are sure your locale data structures won't go beyond a hundred lines at most, I'd advise you to keep this. 如果您确定您的语言环境数据结构最多不会超过一百行,那么我建议您保留这一点。 If you might have to extend it later, you should prefer a more extensible method, probably by using a package designed for this (a quick search on npmjs will return you a few) 如果以后可能需要扩展它,则应该使用一种更可扩展的方法,可能是使用为此目的设计的软件包(对npmjs进行快速搜索将返回一些信息)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM