I'm working on a native android app (java) which supports different locales: en, fr, de, es, pt, it, ru, ar
I need to display a number of objects to the user
example: You have 7 days left
what I used to do is provide a double-from translation for the same sentence, One for the singluar form and the other for the plural
private static final String SENTENCE_SINGLE = "You have 1 day left";
private static final String SENTENCE_PLURAL = "You have %d days left";
public String stringify(number) {
if (number < 0)
return null;
if (number == 1)
return SENTENCE_SINGLE;
return String.format(Locale.ENGLISH, SENTENCE_PLURAL, number);
}
This works fine with English
and French
. But when I'm dealing with Arabic
for example, I have to consider a sentence for the number 2, and another for every first 10 numbers in each 100th. (Kinda complicated, I know)
for refrence
// The arabic way
private static final String SENTENCE_SINGLE = "You have 1 day left";
private static final String SENTENCE_DOUBLE = "You have two-days left";
private static final String SENTENCE_PLURAL = "You have %d days left";
private static final String SENTENCE_PLURAL_FIRST_10 = "You have %d special-way-day left";
public String stringify(number) {
if (number < 0)
return null;
if (number == 1)
return SENTENCE_SINGLE;
if (number == 2)
return SENTENCE_DOUBLE;
if (number % 100 <= 10 && number % 100 >= 2)
return String.format(Locale.ENGLISH, SENTENCE_PLURAL_FIRST_10, number);
return String.format(Locale.ENGLISH, SENTENCE_PLURAL, number);
}
I should have a unified function for all locales. I still can work with this method, even though I will be using redundant sentences for simple locales like
// for English
private static final String SENTENCE_SINGLE = "You have 1 day left";
private static final String SENTENCE_DOUBLE = "You have 2 days left";
private static final String SENTENCE_PLURAL = "You have %d days left";
private static final String SENTENCE_PLURAL_FIRST_10 = "You have %d days left";
The problem is I have no Idea how the grammar in the other languages work (de|German, es|Spanish, pt|Portugese, it|Italian, ru|Russian), I'm mostly working with google translate :)
Is there an existing library I can use, or can someone explain how numbers work in the other languages,