I have some issues with changing the language in the app I do not know why it is not working, there is no error, checked the log all clear.
I want my app to have Spanish and English languages. I have created strings.xml for Spanish and I have set LocaleHelper also I have created app class which contains override method "attachBaseContext" but still not working...
my locale helper
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); 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; resources.updateConfiguration(configuration, resources.getDisplayMetrics()); return context; }}
my string.xml which is translated for Spanish
<resources><string name="item1">Leer Mas Tarde</string><string name="item2">Ajustes</string><string name="item3">Acerca De</string><string name="item4">Términos y Condiciones</string><string name="read_later">Leer Mas Tarde</string><string name="delete">Borrar</string><string name="feedback">Realimentación</string><string name="change_language">Cambiar Idioma</string></resources>
my app class which extends Application
public class App extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(LocaleHelper.onAttach(base, "en")); }}
and this is my button click which opens a listed alertdialog to select which language to use
private void changeLanguage(){ btn_changelanguage = findViewById(R.id.changelanguage_button); btn_changelanguage.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Select Language"); //add a list builder.setItems(name_languages, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case 0: LocaleHelper.setLocale(context, "en"); Toast.makeText(context, "English selected", Toast.LENGTH_SHORT).show(); recreate(); break; case 1: LocaleHelper.setLocale(context, "ca-rES"); Toast.makeText(context, "Spanish selected", Toast.LENGTH_SHORT).show(); recreate(); break; } } }) ; AlertDialog dialog = builder.create(); dialog.show(); } } );}
where name_language is:
String[] name_languages = {"English", "Spanish"};