0

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,

K-Galalem
  • 300
  • 1
  • 10
  • 1
    So yet another generation of developers re-invents [`MessageFormat`](https://developer.android.com/reference/java/text/MessageFormat). That won't help you solve the question of "how do I phrase this in russian", you'll still need translators for that, but it provides the technical base. – Joachim Sauer Dec 22 '22 at 14:51
  • 1
    Does this answer your question? [How to use Android quantity strings (plurals)](https://stackoverflow.com/questions/41950952/how-to-use-android-quantity-strings-plurals). Android has a tool for this already - called a "quantity string". You'd made a version for each locale in that local's resource XML file that uses the appropriate grammar. – Tyler V Dec 22 '22 at 15:44
  • Unfortunately , this fails to deal with arabic because I can't define a way for the `number % 100` condition, and that's base of my question: How to deal with all (weird) locales – K-Galalem Dec 22 '22 at 15:57

0 Answers0