How do you use i18n with Node.js?
Asked Answered
U

1

13

We are about to start building our web app in Node.js and I would like to be ready for i18n so I'm looking for your experience with building Node.js apps where the text is translatable.

Preferably I'd like to use a tool like Pootle via Git or other if you have any recommendations.

Unassuming answered 23/1, 2012 at 13:47 Comment(3)
as mentioned in accepted solution i18next - i18n for node.js or javascript adds all features needed fpr proper i18n plus has a web ui for translation.Dialecticism
also see #20126060 - concerning EcmaScript 402 support in Node.Tameratamerlane
Also there is an example regarding how to implement i18n basically. You might visit .Riplex
K
15

There are a number of i18n modules you can use in your application, but you can create your own if you want.

For example create a folder /languages and inside it create en.js, fr.js etc

it.js

module.exports = {
  "name": "nome",
  "age": "eta",
  .. etc
}

The important thing is to set a default language and make a language select bar somewhere in your site. When the user chooses another language (and not English) in your app you do something like this:

app.get('/lang/:ln', function (req, res, next) {
  // remember the user's chosen language
  req.session.language = req.params.ln;
});

Then you can have a language helper function like so:

translate = function (language, text) {
  // language array contains all the languages
  return language_array[language].text;
}
// example: translate(req.session.language, "age")
Killion answered 23/1, 2012 at 16:9 Comment(4)
i18n in nodejs with i18next works well. i just added a ui to translate resources in web i18next.com/pages/ext_webtranslate.htmlDialecticism
But how do you add date/currency translation? Internationalization isn't just about translating text strings :-PLiverwurst
@kanyee those string values can also just as easily be functions.Neufer
How do you load i.e. it.js into language_array? Node n00b here sry.Ingressive

© 2022 - 2024 — McMap. All rights reserved.