Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Commit

Permalink
Fix #14 Default language does not work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
akexorcist committed Dec 30, 2019
1 parent 2728e37 commit f71ef27
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ open class LocalizationActivityDelegate(val activity: Activity) {
}

fun onCreate() {
currentLanguage = LanguageSetting.getDefaultLanguage(activity)
setupLanguage()
checkBeforeLocaleChanging()
}
Expand All @@ -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 -> {
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.akexorcist.localizationactivity.core

import android.content.Context
import java.util.*

/**
* Created by Aleksander Mielczarek on 03.04.2016.
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit f71ef27

Please sign in to comment.