mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 14:27:57 +01:00 
			
		
		
		
	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:
		| @@ -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) | ||||
|     } | ||||
|   | ||||
| @@ -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) | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -138,4 +138,6 @@ class PreferencesHelper(context: Context) { | ||||
|  | ||||
|     fun downloadNew() = prefs.getBoolean(keys.downloadNew, false) | ||||
|  | ||||
|     fun lang() = prefs.getInt(keys.lang, 0) | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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 | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
							
								
								
									
										48
									
								
								app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								app/src/main/java/eu/kanade/tachiyomi/util/LocaleHelper.kt
									
									
									
									
									
										Normal 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 -> "" | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user