1

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

  • Hello. Let's see if you can help me. I am translating my android app to Serbian but it is not displayed in Serbian, it is still displayed in English. I have the strings.xml file inside the folder "values-sr" and the android device is set to language "Srpski (latinica, Srbija)". Could you tell me what I am doing wrong? Thanks – Ton Dec 09 '20 at 12:48

3 Answers3

0

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.

Arjun Gurung
  • 431
  • 3
  • 17
0

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;
}
CGuess
  • 395
  • 5
  • 9
0

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
    }
Nemanja
  • 211
  • 6
  • 16