0

I am having trouble with supporting multiple languages. I have a list of strings in multiple languages on a sever and I am downloading them on runtime. For every string there is a list of supported languages formatted like "en-US", "nl-NL" etc.

I want my app to pick the best matching translation for the default Locale. If there is no best, it should pick en-US

An example for iOS is here

Do you know a solution or a library that can help me?

R. Adang
  • 593
  • 3
  • 13

1 Answers1

0

You could use the built-in Locale class:

Locale l = new Locale("", "CH");
System.out.println(l.getDisplayCountry());

prints "Switzerland" for example. Note that I have not provided a language.

So what you can do for the reverse lookup is build a map from the available countries:

public static void main(String[] args) throws InterruptedException {
Map<String, String> countries = new HashMap<>();
for (String iso : Locale.getISOCountries()) {
Locale l = new Locale("", iso);
countries.put(l.getDisplayCountry(), iso);
  }


 System.out.println(countries.get("Switzerland"));
 System.out.println(countries.get("Andorra"));
 System.out.println(countries.get("Japan"));
 }

Here you can see more.here

Community
  • 1
  • 1
Kristo
  • 1,339
  • 12
  • 22
  • How does this give me the best matching Locale? If my Locale is nl-BE and there is no nl-BE it should return nl-NL, because thats the best matching. with the country code i cant match BE with NL – R. Adang Feb 19 '16 at 15:56