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
This commit is contained in:
Ken Swenson 2016-12-13 14:47:46 -05:00 committed by inorichi
parent 4061c7450b
commit cc9fd53abb
12 changed files with 114 additions and 0 deletions

View File

@ -2,10 +2,12 @@ package eu.kanade.tachiyomi
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import android.content.res.Configuration
import android.support.multidex.MultiDex import android.support.multidex.MultiDex
import com.evernote.android.job.JobManager import com.evernote.android.job.JobManager
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.updater.UpdateCheckerJob import eu.kanade.tachiyomi.data.updater.UpdateCheckerJob
import eu.kanade.tachiyomi.util.LocaleHelper
import org.acra.ACRA import org.acra.ACRA
import org.acra.annotation.ReportsCrashes import org.acra.annotation.ReportsCrashes
import timber.log.Timber import timber.log.Timber
@ -31,6 +33,8 @@ open class App : Application() {
setupAcra() setupAcra()
setupJobManager() setupJobManager()
LocaleHelper.updateCfg(this, baseContext.resources.configuration)
} }
override fun attachBaseContext(base: Context) { 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() { protected open fun setupAcra() {
ACRA.init(this) ACRA.init(this)
} }

View File

@ -101,4 +101,6 @@ class PreferenceKeys(context: Context) {
val libraryAsList = context.getString(R.string.pref_display_library_as_list) val libraryAsList = context.getString(R.string.pref_display_library_as_list)
val lang = context.getString(R.string.pref_language_key)
} }

View File

@ -138,4 +138,6 @@ class PreferencesHelper(context: Context) {
fun downloadNew() = prefs.getBoolean(keys.downloadNew, false) fun downloadNew() = prefs.getBoolean(keys.downloadNew, false)
fun lang() = prefs.getInt(keys.lang, 0)
} }

View File

@ -1,9 +1,13 @@
package eu.kanade.tachiyomi.ui.base.activity package eu.kanade.tachiyomi.ui.base.activity
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import eu.kanade.tachiyomi.util.LocaleHelper
abstract class BaseActivity : AppCompatActivity(), ActivityMixin { abstract class BaseActivity : AppCompatActivity(), ActivityMixin {
override fun getActivity() = this override fun getActivity() = this
init {
LocaleHelper.updateCfg(this)
}
} }

View File

@ -110,6 +110,8 @@ class MainActivity : BaseActivity() {
} else if (resultCode and SettingsActivity.FLAG_THEME_CHANGED != 0) { } else if (resultCode and SettingsActivity.FLAG_THEME_CHANGED != 0) {
// Delay activity recreation to avoid fragment leaks. // Delay activity recreation to avoid fragment leaks.
nav_view.post { recreate() } nav_view.post { recreate() }
} else if (resultCode and SettingsActivity.FLAG_LANG_CHANGED != 0) {
nav_view.post { recreate() }
} }
} else { } else {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)

View File

@ -78,6 +78,7 @@ class SettingsActivity : BaseActivity(),
companion object { companion object {
const val FLAG_THEME_CHANGED = 0x1 const val FLAG_THEME_CHANGED = 0x1
const val FLAG_DATABASE_CLEARED = 0x2 const val FLAG_DATABASE_CLEARED = 0x2
const val FLAG_LANG_CHANGED = 0x4
} }
} }

View File

@ -9,6 +9,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.LocaleHelper
import eu.kanade.tachiyomi.util.plusAssign import eu.kanade.tachiyomi.util.plusAssign
import eu.kanade.tachiyomi.widget.preference.IntListPreference import eu.kanade.tachiyomi.widget.preference.IntListPreference
import eu.kanade.tachiyomi.widget.preference.LibraryColumnsDialog import eu.kanade.tachiyomi.widget.preference.LibraryColumnsDialog
@ -17,6 +18,7 @@ import net.xpece.android.support.preference.MultiSelectListPreference
import rx.Observable import rx.Observable
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.util.*
class SettingsGeneralFragment : SettingsFragment(), class SettingsGeneralFragment : SettingsFragment(),
PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback { PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback {
@ -44,6 +46,8 @@ class SettingsGeneralFragment : SettingsFragment(),
val categoryUpdate: MultiSelectListPreference by bindPref(R.string.pref_library_update_categories_key) 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?) { override fun onViewCreated(view: View, savedState: Bundle?) {
super.onViewCreated(view, savedState) super.onViewCreated(view, savedState)
@ -101,6 +105,15 @@ class SettingsGeneralFragment : SettingsFragment(),
activity.recreate() activity.recreate()
true 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 { override fun onPreferenceDisplayDialog(p0: PreferenceFragmentCompat?, p: Preference): Boolean {

View File

@ -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 -> ""
}
}
}

View File

@ -188,4 +188,20 @@
<item>2</item> <item>2</item>
</string-array> </string-array>
<string-array name="languages">
<item>@string/system_default</item>
<item>@string/english</item>
<item>@string/spanish</item>
<item>@string/italian</item>
<item>@string/portuguese</item>
</string-array>
<string-array name="languages_values">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
</resources> </resources>

View File

@ -21,6 +21,7 @@
<string name="pref_theme_key">pref_theme_key</string> <string name="pref_theme_key">pref_theme_key</string>
<string name="pref_library_update_restriction_key">library_update_restriction</string> <string name="pref_library_update_restriction_key">library_update_restriction</string>
<string name="pref_start_screen_key">start_screen</string> <string name="pref_start_screen_key">start_screen</string>
<string name="pref_language_key">language</string>
<string name="pref_default_viewer_key">pref_default_viewer_key</string> <string name="pref_default_viewer_key">pref_default_viewer_key</string>
<string name="pref_image_scale_type_key">pref_image_scale_type_key</string> <string name="pref_image_scale_type_key">pref_image_scale_type_key</string>

View File

@ -111,6 +111,14 @@
<string name="light_theme">Main theme</string> <string name="light_theme">Main theme</string>
<string name="dark_theme">Dark theme</string> <string name="dark_theme">Dark theme</string>
<string name="pref_start_screen">Start screen</string> <string name="pref_start_screen">Start screen</string>
<string name="pref_language">Language</string>
<!-- Languages -->
<string name="system_default">System Default</string>
<string name="english">English</string>
<string name="spanish">Spanish</string>
<string name="italian">Italian</string>
<string name="portuguese">Portuguese</string>
<!-- Reader section --> <!-- Reader section -->
<string name="pref_fullscreen">Fullscreen</string> <string name="pref_fullscreen">Fullscreen</string>

View File

@ -56,6 +56,14 @@
android:key="@string/pref_update_only_non_completed_key" android:key="@string/pref_update_only_non_completed_key"
android:title="@string/pref_update_only_non_completed" /> android:title="@string/pref_update_only_non_completed" />
<eu.kanade.tachiyomi.widget.preference.IntListPreference
android:defaultValue="0"
android:entries="@array/languages"
android:entryValues="@array/languages_values"
android:key="@string/pref_language_key"
android:summary="%s"
android:title="@string/pref_language" />
</PreferenceScreen> </PreferenceScreen>
</PreferenceScreen> </PreferenceScreen>