Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change locale to use Latin Serbian (instead of Cyrillic Serbian)

The Serbian language has Latin and Cyrillic alphabets. In Android's Date and Time Picker widgets, the displayed alphabet for Serbian locales seems to be Cyrillic, as seen here.

enter image description here

I wanted to change the locale so that the android widgets are using the Latin Serbian alphabet.

The current language/country code (yielding Cyrillic) are sr and RS respectively. Therefore, my setLocale function is called as

setLocale("sr", "RS");

This is the part im not sure about - according to localeplanet.com, the local code for latin serbian is sr_Latn_RS. However, I tried both

setLocale("sr_Latn", "RS");
//and
setLocale("sr_Latn_RS", "RS");

neither of which work (no change occurs, default to english). According to the Android documentation, it looks like setLocale expects two letter codes.

The language codes are two-letter lowercase ISO language codes (such as "en") as defined by ISO 639-1. The country codes are two-letter uppercase ISO country codes (such as "US") as defined by ISO 3166-1. The variant codes are unspecified.

So how do I specify a Latin serbian locale code? Or does it not exist?

like image 664
Eric S. Avatar asked Mar 08 '16 20:03

Eric S.


2 Answers

The previous answer works well if you only support Lollipop or above. However, if you're coding in Serbian a lot of your user base probably won't have it. Here's a solution that works for old and new versions.

private static Locale serbianLatinLocale(){

    Locale locale = null;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        for (Locale checkLocale : Locale.getAvailableLocales()) {
            if (checkLocale.getISO3Language().equals("srp") && checkLocale.getCountry().equals("LATN") && checkLocale.getVariant().equals("")) {
                locale = checkLocale;
            }
        }
    } else {
        locale = new Locale.Builder().setLanguage("sr").setRegion("RS").setScript("Latn").build();
    }

    return locale;
}
like image 174
CGuess Avatar answered Sep 28 '22 05:09

CGuess


For getting latin locale I first used code below.

new Locale.Builder().setLanguage("sr").setRegion("RS").setScript("Latn").build();

But this solution didn't work on my Android 5.1.1 device (it was still in cyrillic). So I removed setting of region like this:

new Locale.Builder().setLanguage("sr").setScript("Latn").build();

And you have to put your string for serbian resources in b+sr+Latn folder.

like image 38
Gregor Ažbe Avatar answered Sep 28 '22 05:09

Gregor Ažbe