问题
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