From cc9fd53abbad401d13f23e093f16abb6fbd06cfc Mon Sep 17 00:00:00 2001 From: Ken Swenson Date: Tue, 13 Dec 2016 14:47:46 -0500 Subject: [PATCH] Implement language switcher (#563) * Implement language switching using BaseActivity * Add requested changes * Cleanup App.kt Imports and add pref_language_key * Acutally use @string for key * Use string resource for language preference title --- app/src/main/java/eu/kanade/tachiyomi/App.kt | 9 ++++ .../data/preference/PreferenceKeys.kt | 2 + .../data/preference/PreferencesHelper.kt | 2 + .../ui/base/activity/BaseActivity.kt | 4 ++ .../kanade/tachiyomi/ui/main/MainActivity.kt | 2 + .../tachiyomi/ui/setting/SettingsActivity.kt | 1 + .../ui/setting/SettingsGeneralFragment.kt | 13 +++++ .../eu/kanade/tachiyomi/util/LocaleHelper.kt | 48 +++++++++++++++++++ app/src/main/res/values/arrays.xml | 16 +++++++ app/src/main/res/values/keys.xml | 1 + app/src/main/res/values/strings.xml | 8 ++++ app/src/main/res/xml/pref_general.xml | 8 ++++ 12 files changed, 114 insertions(+) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index ca06fc382e..62806323c5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -2,10 +2,12 @@ package eu.kanade.tachiyomi import android.app.Application import android.content.Context +import android.content.res.Configuration import android.support.multidex.MultiDex import com.evernote.android.job.JobManager import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.data.updater.UpdateCheckerJob +import eu.kanade.tachiyomi.util.LocaleHelper import org.acra.ACRA import org.acra.annotation.ReportsCrashes import timber.log.Timber @@ -31,6 +33,8 @@ open class App : Application() { setupAcra() setupJobManager() + + LocaleHelper.updateCfg(this, baseContext.resources.configuration) } override fun attachBaseContext(base: Context) { @@ -40,6 +44,11 @@ open class App : Application() { } } + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + LocaleHelper.updateCfg(this, newConfig) + } + protected open fun setupAcra() { ACRA.init(this) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 4e23025f3f..f51f1dcb9b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -101,4 +101,6 @@ class PreferenceKeys(context: Context) { val libraryAsList = context.getString(R.string.pref_display_library_as_list) + val lang = context.getString(R.string.pref_language_key) + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index e2c61388c3..9c79c4218b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -138,4 +138,6 @@ class PreferencesHelper(context: Context) { fun downloadNew() = prefs.getBoolean(keys.downloadNew, false) + fun lang() = prefs.getInt(keys.lang, 0) + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt index 999b64ba1f..8aac221881 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt @@ -1,9 +1,13 @@ package eu.kanade.tachiyomi.ui.base.activity import android.support.v7.app.AppCompatActivity +import eu.kanade.tachiyomi.util.LocaleHelper abstract class BaseActivity : AppCompatActivity(), ActivityMixin { override fun getActivity() = this + init { + LocaleHelper.updateCfg(this) + } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index ff269b63b7..58dc090d89 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -110,6 +110,8 @@ class MainActivity : BaseActivity() { } else if (resultCode and SettingsActivity.FLAG_THEME_CHANGED != 0) { // Delay activity recreation to avoid fragment leaks. nav_view.post { recreate() } + } else if (resultCode and SettingsActivity.FLAG_LANG_CHANGED != 0) { + nav_view.post { recreate() } } } else { super.onActivityResult(requestCode, resultCode, data) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt index 331e63d0b4..476b9659e3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsActivity.kt @@ -78,6 +78,7 @@ class SettingsActivity : BaseActivity(), companion object { const val FLAG_THEME_CHANGED = 0x1 const val FLAG_DATABASE_CLEARED = 0x2 + const val FLAG_LANG_CHANGED = 0x4 } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt index 87b94fd5c5..df2a125382 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt @@ -9,6 +9,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.util.LocaleHelper import eu.kanade.tachiyomi.util.plusAssign import eu.kanade.tachiyomi.widget.preference.IntListPreference import eu.kanade.tachiyomi.widget.preference.LibraryColumnsDialog @@ -17,6 +18,7 @@ import net.xpece.android.support.preference.MultiSelectListPreference import rx.Observable import rx.android.schedulers.AndroidSchedulers import uy.kohesive.injekt.injectLazy +import java.util.* class SettingsGeneralFragment : SettingsFragment(), PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback { @@ -44,6 +46,8 @@ class SettingsGeneralFragment : SettingsFragment(), val categoryUpdate: MultiSelectListPreference by bindPref(R.string.pref_library_update_categories_key) + val langPreference: IntListPreference by bindPref(R.string.pref_language_key) + override fun onViewCreated(view: View, savedState: Bundle?) { super.onViewCreated(view, savedState) @@ -101,6 +105,15 @@ class SettingsGeneralFragment : SettingsFragment(), activity.recreate() true } + + langPreference.setOnPreferenceChangeListener { preference, newValue -> + (activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_LANG_CHANGED + LocaleHelper.setLocale(Locale(LocaleHelper.intToLangCode(newValue.toString().toInt()))) + LocaleHelper.updateCfg(activity.application, activity.baseContext.resources.configuration) + activity.recreate() + true + } + } override fun onPreferenceDisplayDialog(p0: PreferenceFragmentCompat?, p: Preference): Boolean { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt new file mode 100644 index 0000000000..70ade9b7d3 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt @@ -0,0 +1,48 @@ +package eu.kanade.tachiyomi.util + +import android.app.Application +import android.content.res.Configuration +import android.os.Build +import android.view.ContextThemeWrapper +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import uy.kohesive.injekt.injectLazy +import java.util.Locale + + +object LocaleHelper { + + private val preferences: PreferencesHelper by injectLazy() + private var pLocale = Locale(LocaleHelper.intToLangCode(preferences.lang())) + + fun setLocale(locale: Locale) { + pLocale = locale + Locale.setDefault(pLocale) + } + + fun updateCfg(wrapper: ContextThemeWrapper) { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){ + val config = Configuration() + config.setLocale(pLocale) + wrapper.applyOverrideConfiguration(config) + } + } + + fun updateCfg(app: Application, config: Configuration){ + if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { + config.locale = pLocale + app.baseContext.resources.updateConfiguration(config, app.baseContext.resources.displayMetrics) + } + } + + fun intToLangCode(i: Int): String { + return when(i){ + 1 -> "en" + 2 -> "es" + 3 -> "it" + 4 -> "pt" + // System Language + else -> "" + } + } + +} diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 1568c1453a..db8a510e71 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -188,4 +188,20 @@ 2 + + @string/system_default + @string/english + @string/spanish + @string/italian + @string/portuguese + + + + 0 + 1 + 2 + 3 + 4 + + \ No newline at end of file diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 52e687c777..fd93e4f054 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -21,6 +21,7 @@ pref_theme_key library_update_restriction start_screen + language pref_default_viewer_key pref_image_scale_type_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 463bdb7ebd..66e9110da3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -111,6 +111,14 @@ Main theme Dark theme Start screen + Language + + + System Default + English + Spanish + Italian + Portuguese Fullscreen diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index d716809639..d5d934ba70 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -56,6 +56,14 @@ android:key="@string/pref_update_only_non_completed_key" android:title="@string/pref_update_only_non_completed" /> + + \ No newline at end of file