Serbian latin and Serbian Cyrillic in android

半腔热情 提交于 2019-12-13 00:22:54

问题


Is it possible to change programmatically Locale in the app to Serbian Latin in android app? As i understand i can do it in this way:

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

But it is available only in 21 API. But i have minsdk = 16


回答1:


You can use Locale local=new Locale("sr","RS"); where first parameter is language and second is region/country. About the script i have know idea how to set script.




回答2:


Here's the answer to this question. With various forms for the newer versions of the OS.

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



回答3:


Hope I'm not too late :)
This is how I solved it, you need to make different values folder with corresponding strings.xml in it, keep in mind that I wanted to have latin as default so I use en (just plain values folder) for that and cyrillic as a choice so its in ca (values-ca folder) and so on...

public static void language(String s) {
        switch (s) {
            case "Srpski":
                setLocale("en");
                break;
            case "Српски":
                setLocale("ca");
                break;
            case "Hrvatski":
                setLocale("hr");
                break;
            case "Македонски":
                setLocale("mk");
                break;
            case "Slovenski":
                setLocale("sl");
                break;
        }
    }

    public static void setLocale(String lang) {
        // ctx is my Context
        if (ctx == null) {
            ctx = MainActivity.getContext();
        }
        Locale mLocale = new Locale(lang);
        Locale.setDefault(mLocale);
        Configuration mConfiguration = new Configuration();
        mConfiguration.locale = mLocale;    //DEPRECATED .locale
        ctx.getResources().updateConfiguration(mConfiguration, ctx.getResources().getDisplayMetrics());     //DEPRECATED updateConfiguration
    }


来源:https://stackoverflow.com/questions/45717376/serbian-latin-and-serbian-cyrillic-in-android

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