Created
December 9, 2018 13:14
-
-
Save smarteist/1faf792e6b672c50ed0686adf8176f02 to your computer and use it in GitHub Desktop.
Android Locale Changer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rayana.himeapp.Utility; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.content.res.Configuration; | |
import android.content.res.Resources; | |
import android.os.Build; | |
import android.os.LocaleList; | |
import android.preference.PreferenceManager; | |
import java.util.Locale; | |
/** | |
* Created by Ali Hosseini on 9/2/17. | |
* this class provides changing locale compulsively on application resources | |
*/ | |
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 language) { | |
return setLocale(context, language); | |
} | |
public static String getLanguage(Context context) { | |
return getPersistedData(context, Locale.getDefault().getLanguage()); | |
} | |
private static Context setLocale(Context context, String language) { | |
persist(context, language); | |
return updateResources(context, language); | |
} | |
private static String getPersistedData(Context context, String defaultLanguage) { | |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); | |
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage); | |
} | |
@SuppressLint("ApplySharedPref") | |
private static boolean persist(Context context, String language) { | |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); | |
SharedPreferences.Editor editor = preferences.edit(); | |
editor.putString(SELECTED_LANGUAGE, language); | |
return editor.commit(); | |
} | |
private static Context updateResources(Context context, String language) { | |
Locale locale = new Locale(language); | |
Locale.setDefault(locale); | |
Resources resources = context.getResources(); | |
Configuration configuration = resources.getConfiguration(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
configuration.setLocale(locale); | |
LocaleList localeList = new LocaleList(locale); | |
LocaleList.setDefault(localeList); | |
configuration.setLocales(localeList); | |
return context.createConfigurationContext(configuration); | |
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
configuration.setLocale(locale); | |
return context.createConfigurationContext(configuration); | |
} else { | |
configuration.locale = locale; | |
resources.updateConfiguration(configuration, resources.getDisplayMetrics()); | |
} | |
return context; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment