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.

morten.c
  • 3,414
  • 5
  • 40
  • 45
webjay
  • 5,358
  • 9
  • 45
  • 62
  • as mentioned in accepted solution [i18next - i18n for node.js or javascript](http://i18next.com/node) adds all features needed fpr proper i18n plus has a web ui for translation. – jamuhl Sep 07 '12 at 10:27
  • also see http://stackoverflow.com/questions/20125560/referenceerror-intl-is-not-defined-in-node-js - concerning EcmaScript 402 support in Node. – Steven R. Loomis Apr 25 '14 at 00:05
  • Also there is [an example](https://github.com/efkan/node-intl-polyfill-example) regarding how to implement i18n basically. You might visit . – efkan Nov 05 '16 at 12:58

1 Answers1

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")
alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • 1
    i18n in nodejs with [i18next](http://i18next.com/node) works well. i just added a ui to translate resources in web i18next.com/pages/ext_webtranslate.html – jamuhl Aug 16 '12 at 10:43
  • 1
    But how do you add date/currency translation? Internationalization isn't just about translating text strings :-P – kenyee Apr 22 '13 at 19:05
  • @kanyee those string values can also just as easily be functions. – Dave Sag May 27 '16 at 01:08
  • How do you load i.e. it.js into language_array? Node n00b here sry. – user2723025 Jan 25 '22 at 12:21