Hi I have an App which is a loads a webview and there's a FLOWING DRAWER in it.
I am trying to change the locale.
I have a localeHelper
java class and an AppClass
which extends Application
But its just not happening.
What happens is the locale changes only when I restart the app super.Onbackpressed()
but when I kills it ~ the locale turns back to english; it resets
I have been going mad from around 4 days.
Please help me, I'm a beginner;
localeHelp.java
public class LocaleHelper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
return setLocale(context, lang);
}
public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static Context setLocale(Context context, String language) {
persist(context, language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
simply calling setLocale("fr"); with this function in MainActivity
private void setLocale() {
Locale locale;
locale = new Locale("hi");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
Intent intent = getIntent();
finish();
startActivity(intent);}
The problem: activity was recreated but the language was still the same.. Okay so after much trial and error this is what I got to work..
I put this code in OnResume
// todo load locale
Locale current = getResources().getConfiguration().locale;
// todo get the current locale
//Toast.makeText(MainActivity.this, ""+current, Toast.LENGTH_SHORT).show();
String currentLanginString = current.toString(); // convert current language to string
if(!currentLanginString.contains(wholeAppLanguage)) {
recreate();
// Toast.makeText(this, "recreatingResume", Toast.LENGTH_SHORT).show();
}
loadLocale();
and then I created the Loadlocale function
public void loadLocale (){
int langcode=0;
String a="a";
if (wholeAppLanguage.contains("hi")) {
langcode = 0;
a="hi";
}
else if (wholeAppLanguage.contains("en"))
{
langcode=1;
a="en";
}
setLocale2(langcode);
}
then it worked perfectly~