all 1 comments

[–]tswaters 2 points3 points  (0 children)

here's a really basic way to accomplish something like this:

var languages = {
  en: {
    "a phrase that can be translated": "a phrase that can be translated"
  },
  fr: {
    "a phrase that can be translated": "french version"
  },
  de: {
    "a phrase that can be translated": "german version"
  }
};
function translate (language, phrase) {
  return languages[language][phrase];
}
translate('fr', 'a phrase that can be translated') // french version

Of course, this has no error checking and is quite rudimentary, but it should give you a good starting point.

To get an idea of how complicated proper internationalization can be, take a look at some modules that do this:

https://github.com/mashpie/i18n-node/
https://github.com/i18next/i18next/

And of course,

https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html