I have an Android app that contains two languages, I want to add switcher When pressed, the application is in English, When pressed, the application becomes Arabic
Asked
Active
Viewed 123 times
-4
-
1have you tried anything?please add your code – Om Infowave Developers Nov 23 '17 at 07:15
-
I do not have any code and I would like to know how to add it, in MainActivity ?? – Bøy Øf Løndøn Nov 23 '17 at 07:17
-
http://programmerguru.com/android-tutorial/android-localization-at-runtime/ – andreikashin Sep 17 '18 at 11:35
1 Answers
0
Call the below function to update the language.
private void updateLanguage(String languageCode) //languageCode ex. 'en' for english
{
Context context = LocaleHelper.setLocale(MainActivity.this, languageCode);
Log.v("langcode", languageCode);
Resources resources = context.getResources();
}
Create a class named LocaleHelper to do the things.
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);
Log.v("abcdef", Locale.getDefault().getLanguage());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.v("Updatinglocale","Nougat");
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
public 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;
}
}

Rishabh Sharma
- 270
- 5
- 17