How to make an application with really multilingual strings?

What should be the best approach for creating strings for different languages? I have this problem, I am trying to display strings such as "month", "months", "year", "years". I currently work in three languages ​​that I know: Spanish, English and Polish. For English and Spanish, this is straightforward. But, for example, in the Polish years can become "lata" (after the numbers 2 - 4) or "lat" (after the numbers from 5). I thought about adding an extra line for this and made it empty in other languages. However, this made me think of other languages ​​that I don’t know, which may have even more differences. What should be the best approach in this case if I plan to add more languages ​​in the future?

+6
java android multilingual
source share
4 answers

It looks like you want a ChoiceFormat or at least use one via MessageFormat :

 public static void main(String... args) { String[] formats = { // NOTE - In a real app, you'd fetch the format strings from a language, // file, not hard-code them in your program. Obviously. "{0,number} {0,choice,0#years|1#year|1<years}", // english "{0,number} {0,choice,0#años|1#año|1<años}", // spanish "{0,number} {0,choice,1#[fewer than 2]|2#lata|4<lat}", // polish "{0,number} år", // swedish - singular and plural forms look the same! }; int[] years = {0, 1, 2, 3, 4, 5, 6}; for (int year : years) { for (String format : formats) { System.out.println(MessageFormat.format(format, year)); } System.out.println(); } } 

In your program, you naturally get the format string from the string file.

+3
source share

Thanks to the answers I am writing 2 Android-based solutions. The first one I used was plural. At first glance, when checking Plurals documents / examples, you might think that there is a number = "several" (for 2-4 plurals) that check sources only works for locale 'cs'. For other locales only "one" and "another" work. So, in your strings.xml files:

 <plurals name ="years"> <item quantity="one">1 year</item> <item quantity="other"><xliff:g id="number">%d</xliff:g> years</item> </plurals> 

So, for polishing I would:

 <plurals name ="years"> <item quantity="one">1 rok</item> <item quantity="other"><xliff:g id="number">%d</xliff:g> lat</item> </plurals> 

Then I would have the code:

 int n = getYears(...); if (Locale.getDefault().getLanguage().equalsIgnoreCase("pl") && n >= 2 && n <= 4) { return getString(R.string.years_pl, n); } else { return getResources().getQuantityString(R.plurals.years, n, n); } 

And in my strings.xml file to localize polish, I would add the missing line:

 <string name="years_pl"><xliff:g id="number">%d</xliff:g> lata</string> 

My second solution has a plural element for English, Spanish, and other languages ​​that have too many plural variations. Then for other languages ​​that have such changes, I would use ChoiceFormat. So in my code:

 ... private static final int LANG_PL = 0; // add more languages here if needed ... String formats[] = { "{0,number} {0,choice,1#" + getString(R.string.year_1) + "|2#" + getString(R.string.years_2_4) + "|4<" + getString(R.string.years_lots) +"}", // polish // more to come }; ... // Here I would add more for certain languages if (Locale.getDefault().getLanguage().equalsIgnoreCase("pl")) { return MessageFormat.format(formats[LANG_PL], n); } else { return getResources().getQuantityString(R.plurals.years, n, n); } 

I don't know if these methods are the best, but for now, or until Google does something better, this works for me.

+2
source share

There is built-in plural support that is not well documented.
Mentioned here and you can see it in Browser Sources .

+1
source share

Note that I do not have a specific experience for Android ... but I usually prefer to have separate view files for each language that I am going to support, this allows for wider localization than the language alone, now you can also slightly modified layouts. if you know the text in some other language, you may need, for example, wider buttons or even different images.

0
source share

All Articles