简体   繁体   中英

Translate english country name into french (API 19)

So I have a little problem that I can't resolve. I need to translate a country name in english to french but I can't do that with API 19. I have read a similar topic but the answer only works for API > 19.

I have checked libraries but I can't find one that fits to my problem and there's not so many about that.

EDIT : Code that doesn't work under API 21 :

String country = "Japonia";
Locale outLocale = Locale.forLanguageTag("en_GB");
Locale inLocale = Locale.forLanguageTag("pl-PL");
for (Locale l : Locale.getAvailableLocales()) {
    if (l.getDisplayCountry(inLocale).equals(country)) {
        System.out.println(l.getDisplayCountry(outLocale));
        break;
    }
}

Taken from : Translate country name into other language

EDIT 2 :

I add the dependenccy from apache but how to use ?

LocaleUtils.toLocale(localeString)

EDIT 3

I used the line above but this doesnt work... I used it with "brazil" but it returned an exception.

Thanks in advance for your help !

It is because Locale.forLanguageTag() was added in API level 21.

Try this:

  1. Add Apache Commons Lang packet by adding this to build.gradle:

    compile 'org.apache.commons:commons-lang3:3.6'

  2. Use this function instead:

    LocaleUtils.toLocale(localeString)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM