问题
I am creating an app which will support Hindi and Gujrati language. I am setting the application language from my app settings screen. like i given a option to user to select a language among English/Hindi/Gujrati.
I am setting Locale on radio button selection basis. I am saving the selection in persistence and on that basis i am changing the typeface of all of textviews in my application.
EVERYTHING IS WORKING FINE.. but it changes the language to english in between the app running. suppose i selected the hindi language from my settings screen and running my app. suddenly after 10-15 min it takes text values from "values" directory, not from "values-hi". I really don't understand why its taking from default values directory. my applications dynamic data is working fine. its coming in hindi and even my app drawables are also working fine but the problem is only that it takes the values from "values" directory.
THIS METHOD IS USED WHEN USER SELECT THE LANGUAGE FROM MY APP SETTINGS SCREEN.
public void setLocale(Context context, String lang) {
Locale myLocale = new Locale(lang);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
THIS METHOD IS USED TO SETTYPEFACE OF TEXTVIEW IN ONCREATE METHOD
public static void setTypeface(TextView textView, Context context) {
SharedPreferences sp = context.getSharedPreferences("language_selection", context.MODE_PRIVATE);
String language = sp.getString("language", "English");
if (language != null) {
if (language.equalsIgnoreCase("Hindi")) {
textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "gargi.ttf"));
}
if (language.equalsIgnoreCase("Gujrati")) {
textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "SHRUTI.TTF"));
}
}
}
回答1:
Try to set your selected Language
on this way:
Locale locale = new Locale("YourSelectedLang");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
来源:https://stackoverflow.com/questions/22111764/android-gujrati-and-hindi-supported-application