diff --git a/app/src/main/java/com/akexorcist/localizationapp/MainActivity.java b/app/src/main/java/com/akexorcist/localizationapp/MainActivity.java index 332958e..75e4c60 100644 --- a/app/src/main/java/com/akexorcist/localizationapp/MainActivity.java +++ b/app/src/main/java/com/akexorcist/localizationapp/MainActivity.java @@ -16,8 +16,6 @@ public class MainActivity extends LocalizationActivity { @Override public void onCreate(Bundle savedInstanceState) { - // You can set default language when first time running. Must to setup before onCreate was called. - setDefaultLanguage("th"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); diff --git a/app/src/main/java/com/akexorcist/localizationapp/MainApplication.java b/app/src/main/java/com/akexorcist/localizationapp/MainApplication.java index 71afff5..bb5a3c9 100644 --- a/app/src/main/java/com/akexorcist/localizationapp/MainApplication.java +++ b/app/src/main/java/com/akexorcist/localizationapp/MainApplication.java @@ -13,10 +13,13 @@ */ public class MainApplication extends Application { + LocalizationApplicationDelegate localizationDelegate = new LocalizationApplicationDelegate(); @Override protected void attachBaseContext(Context base) { + // You can set default language when first time running. + localizationDelegate.setDefaultLanguage(base, "th"); super.attachBaseContext(localizationDelegate.attachBaseContext(base)); } diff --git a/app/src/main/java/com/akexorcist/localizationapp/customactivity/CustomActivity.java b/app/src/main/java/com/akexorcist/localizationapp/customactivity/CustomActivity.java index 698a5c7..b571121 100644 --- a/app/src/main/java/com/akexorcist/localizationapp/customactivity/CustomActivity.java +++ b/app/src/main/java/com/akexorcist/localizationapp/customactivity/CustomActivity.java @@ -53,14 +53,6 @@ public final void setLanguage(Locale locale) { localizationDelegate.setLanguage(this, locale); } - public final void setDefaultLanguage(String language) { - localizationDelegate.setDefaultLanguage(language); - } - - public final void setDefaultLanguage(Locale locale) { - localizationDelegate.setDefaultLanguage(locale); - } - public final Locale getCurrentLanguage() { return localizationDelegate.getLanguage(this); } diff --git a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LanguageSetting.kt b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LanguageSetting.kt index 7fd32ab..12b7d74 100644 --- a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LanguageSetting.kt +++ b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LanguageSetting.kt @@ -5,7 +5,6 @@ import androidx.annotation.VisibleForTesting import java.util.* - object LanguageSetting { private const val PREFERENCE_LANGUAGE = "pref_language" private const val KEY_CURRENT_LANGUAGE = "key_language" @@ -36,17 +35,27 @@ object LanguageSetting { } @JvmStatic - fun getLanguage(context: Context): Locale = + fun getLanguage(context: Context): Locale? = getPreference(context, KEY_CURRENT_LANGUAGE)?.let { locale: String -> val info = locale.split("_") when (info.size) { 1 -> Locale(info[0]) 2 -> Locale(info[0], info[1]) - else -> Locale.ENGLISH + else -> null } } ?: run { - Locale.ENGLISH + null + } + + fun getLanguageWithDefault(context: Context, default: Locale): Locale { + return getLanguage(context)?.let { locale -> + locale + } ?: run { + setLanguage(context, default) + default } + } + private fun setPreference(context: Context, key: String, value: String) { context.getSharedPreferences(PREFERENCE_LANGUAGE, Context.MODE_PRIVATE) diff --git a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationActivityDelegate.kt b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationActivityDelegate.kt index 4e02181..7860867 100644 --- a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationActivityDelegate.kt +++ b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationActivityDelegate.kt @@ -28,7 +28,6 @@ open class LocalizationActivityDelegate(val activity: Activity) { } fun onCreate() { - currentLanguage = LanguageSetting.getDefaultLanguage(activity) setupLanguage() checkBeforeLocaleChanging() } @@ -42,7 +41,7 @@ open class LocalizationActivityDelegate(val activity: Activity) { } fun attachBaseContext(context: Context): Context { - val locale = LanguageSetting.getLanguage(context) + val locale = LanguageSetting.getLanguageWithDefault(context, LanguageSetting.getDefaultLanguage(context)) val config = context.resources.configuration return when { Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { @@ -98,29 +97,16 @@ open class LocalizationActivityDelegate(val activity: Activity) { } fun setLanguage(context: Context, newLocale: Locale) { - if (!isCurrentLanguageSetting(context, newLocale)) { + val oldLocale = LanguageSetting.getLanguageWithDefault(context, LanguageSetting.getDefaultLanguage(context)) + if (!isCurrentLanguageSetting(newLocale, oldLocale)) { LanguageSetting.setLanguage(activity, newLocale) notifyLanguageChanged() } } - fun setDefaultLanguage(language: String) { - val locale = Locale(language) - setDefaultLanguage(locale) - } - - fun setDefaultLanguage(language: String, country: String) { - val locale = Locale(language, country) - setDefaultLanguage(locale) - } - - fun setDefaultLanguage(locale: Locale) { - LanguageSetting.setDefaultLanguage(activity, locale) - } - // Get current language fun getLanguage(context: Context): Locale { - return LanguageSetting.getLanguage(context) + return LanguageSetting.getLanguageWithDefault(context, LanguageSetting.getDefaultLanguage(context)) } // Check that bundle come from locale change. @@ -136,15 +122,16 @@ open class LocalizationActivityDelegate(val activity: Activity) { // Setup language to locale and language preference. // This method will called before onCreate. private fun setupLanguage() { - val locale = LanguageSetting.getLanguage(activity) - currentLanguage = locale - LanguageSetting.setLanguage(activity, locale) + LanguageSetting.getLanguage(activity)?.let { locale -> + currentLanguage = locale + } ?: run { + checkLocaleChange(activity) + } } // Avoid duplicated setup - private fun isCurrentLanguageSetting(context: Context, newLocale: Locale): Boolean { - val oldLocale = LanguageSetting.getLanguage(context).toString() - return newLocale.toString() == oldLocale + private fun isCurrentLanguageSetting(newLocale: Locale, currentLocale: Locale): Boolean { + return newLocale.toString() == currentLocale.toString() } // Let's take it change! (Using recreate method that available on API 11 or more. @@ -157,7 +144,8 @@ open class LocalizationActivityDelegate(val activity: Activity) { // Check if locale has change while this activity was run to back stack. private fun checkLocaleChange(context: Context) { - if (!isCurrentLanguageSetting(context, currentLanguage)) { + val oldLanguage = LanguageSetting.getLanguageWithDefault(context, LanguageSetting.getDefaultLanguage(context)) + if (!isCurrentLanguageSetting(currentLanguage, oldLanguage)) { sendOnBeforeLocaleChangedEvent() isLocalizationChanged = true callDummyActivity() diff --git a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationApplicationDelegate.kt b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationApplicationDelegate.kt index a01a5f1..0440da6 100644 --- a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationApplicationDelegate.kt +++ b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationApplicationDelegate.kt @@ -1,6 +1,7 @@ package com.akexorcist.localizationactivity.core import android.content.Context +import java.util.* /** * Created by Aleksander Mielczarek on 03.04.2016. @@ -12,4 +13,18 @@ class LocalizationApplicationDelegate { fun attachBaseContext(context: Context): Context = LocalizationUtility.applyLocalizationContext(context) fun getApplicationContext(applicationContext: Context): Context = LocalizationUtility.applyLocalizationContext(applicationContext) + + fun setDefaultLanguage(context: Context, language: String) { + val locale = Locale(language) + setDefaultLanguage(context, locale) + } + + fun setDefaultLanguage(context: Context, language: String, country: String) { + val locale = Locale(language, country) + setDefaultLanguage(context, locale) + } + + fun setDefaultLanguage(context: Context, locale: Locale) { + LanguageSetting.setDefaultLanguage(context, locale) + } } diff --git a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationContext.kt b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationContext.kt index eff8f83..c7b4f39 100644 --- a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationContext.kt +++ b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationContext.kt @@ -13,11 +13,12 @@ import android.util.DisplayMetrics class LocalizationContext(base: Context) : ContextWrapper(base) { override fun getResources(): Resources { + val locale = LanguageSetting.getLanguageWithDefault(this, LanguageSetting.getDefaultLanguage(this)) val configuration = super.getResources().configuration if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - configuration.setLocales(LocaleList(LanguageSetting.getLanguage(this))) + configuration.setLocales(LocaleList(locale)) } else { - configuration.setLocale(LanguageSetting.getLanguage(this)) + configuration.setLocale(locale) } val metrics: DisplayMetrics = super.getResources().displayMetrics return Resources(assets, metrics, configuration) diff --git a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationUtility.kt b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationUtility.kt index 410916f..66b17a7 100644 --- a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationUtility.kt +++ b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/core/LocalizationUtility.kt @@ -13,7 +13,7 @@ import java.util.* object LocalizationUtility { fun applyLocalizationContext(baseContext: Context): Context { val baseLocale = getLocaleFromConfiguration(baseContext.resources.configuration) - val currentLocale = LanguageSetting.getLanguage(baseContext) + val currentLocale = LanguageSetting.getLanguageWithDefault(baseContext, LanguageSetting.getDefaultLanguage(baseContext)) if (!baseLocale.toString().equals(currentLocale.toString(), ignoreCase = true)) { val context = LocalizationContext(baseContext) val config = context.resources.configuration diff --git a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/ui/LocalizationActivity.kt b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/ui/LocalizationActivity.kt index f69dbfa..53632c1 100644 --- a/localizationActivity/src/main/java/com/akexorcist/localizationactivity/ui/LocalizationActivity.kt +++ b/localizationActivity/src/main/java/com/akexorcist/localizationactivity/ui/LocalizationActivity.kt @@ -72,14 +72,6 @@ abstract class LocalizationActivity : AppCompatActivity(), OnLocaleChangedListen localizationDelegate.setLanguage(this, locale) } - fun setDefaultLanguage(language: String) { - localizationDelegate.setDefaultLanguage(language) - } - - fun setDefaultLanguage(locale: Locale) { - localizationDelegate.setDefaultLanguage(locale) - } - fun getCurrentLanguage(): Locale { return localizationDelegate.getLanguage(this) }