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

梦想的初衷 提交于 2019-12-05 14:07:52

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;
}

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.

Mohsin Khan

Please search for your query before posting a question. It may be answered in some other related form.

    Locale newLocale = new Locale("sr","RS");
    Configuration config = new Configuration();
    config.setLocale(newLocale);
    // using this to reference my Activity
    this.getBaseContext().getResources().updateConfiguration(config, this.getBaseContext().getResources().getDisplayMetrics());

i found these two answers suitable to your query android custom date-picker SO and locale from english to french.

EDIT

    Locale[] locales = Locale.getAvailableLocales();
    for(Locale locale : locales){
        if(locale.getCountry().equalsIgnoreCase("RS")
                && locale.getScript().equalsIgnoreCase("Latn"))
        {
            Configuration config = new Configuration();
            config.setLocale(locale);
            // using this to reference my Activity
            this.getBaseContext().getResources().updateConfiguration(config, this.getBaseContext().getResources().getDisplayMetrics());
            break;
        }
    }

I know there will be an efficient way to do it, however you may get the direction that you need to get the list of available locales and get the locale you desire. Hope it helps

EDIT-2 (Final)

you can construct the locale using:

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

Can you please use below one ?

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        Resources res = this.getResources();
        Configuration conf = res.getConfiguration();
        boolean isLatinAlphabet = PreferenceManager.getDefaultSharedPreferences(this);

        if(conf.locale.getLanguage().equals("sr") && isLatinAlphabet) {
            conf.locale = new Locale("sr", "YourContryCode");
            res.updateConfiguration(conf, res.getDisplayMetrics());
        }
    }
}

Note: Replace your YourContryCode in conf.locale = new Locale("sr", "YourContryCode"); line.

Manifest.xml:

<application
        android:name=".MyApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/application_name"
        android:theme="@style/AppTheme">

   ...

</application>

Hope this will help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!