Engineering Machine
Engineering Machine

Reputation: 642

Internationlisation plain Express.js

Assume I have the following code:

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

Upvotes: 0

Views: 85

Answers (1)

sdumetz
sdumetz

Reputation: 552

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. 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)

Upvotes: 1

Related Questions