2

I am looking for some help (and some tools) for advanced localization. I am using a Node backend, so I would prefer tools/libraries in JS.

I currently do some simple string replacement using a properties file that are localized to different languages.

home_en.properties

page.title=My Site
page.header=Hello %s

home_de.properties

page.title=Meine Seite
page.header=Hallo %s

So far so good... but here are a few more advanced examples I am trying to solve:

Multiples

I need to be able to translate multiples correctly, for example:

You have 1 message

vs

You have 2 messages

I do not want to have logic in my code that says

if (i == 1) { // use page.messages.singular }
else { // use page.messages.plural }

because I do not believe that check belongs in my business logic and I know that different languages treat singular/plural differently (i.e. singular includes 0)

Multiple multiples

Some issue as above, but now with multiple combinations of multiples, for example:

You have %d unread messages and %d read messages

or

You have %d unread messages and %d read messages, as well as %d private notifications

Gender

Here is an example I would like to solve:

I love my %s

and possible values are cat, dog, horse

No problem in English, but in German, this would require:

home_de.properties

animals.like.male=Ich liebe meinen %s
animals.like.female=Ich liebe meine %s
animals.like.neutral=Ich liebe mein %s

which would then lead to unnecessary entries for English:

home_en.properties

animals.like.male=I love my %s
animals.like.female=I love my %s
animals.like.neutral=I love my %s

Question

This was all a long winded setup for the following question:

What tools/libraries are out there that would solve advanced string localization like this?

Steve
  • 8,609
  • 6
  • 40
  • 54
  • I deleted the answer because it looks like it can't be done with opensource version of Closure Templates: https://groups.google.com/forum/#!topic/closure-templates-discuss/rbki3JX3lRo – sanchez May 25 '14 at 12:22

1 Answers1

0

The below answer is for generic translation approach and not restricted to particular localization framework. However, for the sake of simplicty I will pick i18next to illustrate how those ideas can be done.

Multiples (Pluralization)

Generally speaking, the "Mutliples" that you are referring to is a concept called "pluralization" in many localization framework.

A pluralized string means there is a variable affecting the output content of the string.

The below code shows how a string with counting variable can fulfill your need. (ref: http://i18next.com/pages/sample.html)

So that, you can create a string like this:

{
   "app": {
        "new_messgae": "You have __count__ message.",
        "new_messgae_plural": "You have __count__ messages."
    }
}

Multiple multiples (plural strings with multiple count)

The approach of dealing with this kind of string is tricky, but usually we separate the single string with >1 variables into 2 / more plural strings with 1 variable.

Gender (Strings with context)

For strings with specific context (e.g.: gender), you can make use of "context" feature in localization framework to make it works. (Just visit the "context" part of http://i18next.com/pages/sample.html for the information)

Cowcow02
  • 561
  • 1
  • 4
  • 5
  • Thank you very much for the answer. I haven't heard of `i18next` before, so I will certainly look into it. I have two questions about your answer though: 1) I understand the pluralization approach, but where do you define when to use the plural? For example, French, where 0 is considered singular. Or other languages that have more than just 2 forms. 2) How would you "separate" a sentence with >1 variables into several strings of 1 variable. It is usually the first rule of localization to not separate strings and reassemble later. – Steve May 28 '14 at 02:56
  • 1) The pluralization rules for different language is well defined here http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html. – Cowcow02 May 28 '14 at 03:07
  • 2) For further explanation of this approach, I hope this works for you http://stackoverflow.com/questions/1888487/internationalising-sentences-with-two-plural-words – Cowcow02 May 28 '14 at 03:08