Skip to content

Commit

Permalink
Language Selection improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatApo committed Apr 25, 2019
1 parent 07b2298 commit 8794851
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
String tag = (String) radioGroup.getChildAt(checkedId).getTag();
Logger.debug("Selected: " + tag);
LocaleUtils.setLocale(this, tag);
LocaleUtils.persist(tag);
});
}

Expand Down
66 changes: 31 additions & 35 deletions app/src/main/java/com/edotassi/amazmod/util/LocaleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.content.res.Resources;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.os.ConfigurationCompat;
import android.util.Log;

import com.pixplicity.easyprefs.library.Prefs;

Expand All @@ -27,10 +29,6 @@ public static Context onAttach(Context context, String defaultLanguage) {
return setLocale(context, lang);
}

public static String getLanguage() {
return getPersistedData(Locale.getDefault().getLanguage());
}

public static Locale getLocale() {
String currentLanguage = getPersistedData(Locale.getDefault().getLanguage());
//TODO: commented line below because it was making TinyLog config not to work (is any log is done before configuration, nothing works)
Expand All @@ -51,50 +49,48 @@ private static Locale getLocaleByLanguageCode(@NonNull String languageCode) {

}

public static Context setLocale(Context context, String language) {
persist(language);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, getLocale());
}

return updateResourcesLegacy(context, getLocale());
// Save new language
public static void persist(String language) {
Prefs.putString(Constants.PREF_LANGUAGE, language);
}

// Get saved language
private static String getPersistedData(String defaultLanguage) {
return Prefs.getString(Constants.PREF_LANGUAGE, defaultLanguage);
}

private static void persist(String language) {
Prefs.putString(Constants.PREF_LANGUAGE, language);
public static String getLanguage() {
return getPersistedData(Locale.getDefault().getLanguage());
}

@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, Locale locale) {
Locale.setDefault(locale);

Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);

return context.createConfigurationContext(configuration);
}
// Change language
private static Context setLocale(Context context, String language) {
Log.d("Amazmod","Change language - System: "+Locale.getDefault().getLanguage()+", To: "+language+", Device: "+ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).toLanguageTags());

Locale locale;
// If AUTO get the system Locale
if (language.equals(Constants.PREF_LANGUAGE_AUTO)) {
//language = Locale.getDefault().getLanguage();
locale = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0);
}else{
locale = getLocaleByLanguageCode(language);
}

@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, Locale locale) {
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) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}else{
configuration.locale = locale;
// Min APP SDK is above
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
configuration.setLayoutDirection(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}

resources.updateConfiguration(configuration, resources.getDisplayMetrics());

return context;
}

public static String getDisplayLanguage(@NonNull String languageCode) {
Expand Down

0 comments on commit 8794851

Please sign in to comment.