2016-12-13 14:47:46 -05:00
|
|
|
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
|
2016-12-20 18:42:46 -05:00
|
|
|
import java.util.*
|
2016-12-13 14:47:46 -05:00
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* Utility class to change the application's language in runtime.
|
|
|
|
*/
|
|
|
|
@Suppress("DEPRECATION")
|
2016-12-13 14:47:46 -05:00
|
|
|
object LocaleHelper {
|
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* Preferences helper.
|
|
|
|
*/
|
2016-12-13 14:47:46 -05:00
|
|
|
private val preferences: PreferencesHelper by injectLazy()
|
2016-12-13 15:07:48 -05:00
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* In API 16 and below the application's configuration has to be changed, so we need a copy of
|
|
|
|
* the initial locale. The only problem is that if the system locale changes while the app is
|
|
|
|
* running, it won't change until an application restart.
|
|
|
|
*/
|
|
|
|
private var v16SystemLocale = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
|
|
|
|
preferences.context.resources.configuration.locale else null
|
2016-12-13 14:47:46 -05:00
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* The application's locale. When it's null, the system locale is used.
|
|
|
|
*/
|
2016-12-26 09:44:59 -05:00
|
|
|
private var appLocale = getLocaleFromString(preferences.lang())
|
2016-12-20 18:42:46 -05:00
|
|
|
|
|
|
|
/**
|
2016-12-26 09:44:59 -05:00
|
|
|
* Returns the locale for the value stored in preferences, or null if it's system language.
|
2016-12-20 18:42:46 -05:00
|
|
|
*
|
2016-12-26 09:44:59 -05:00
|
|
|
* @param pref the string value stored in preferences.
|
2016-12-20 18:42:46 -05:00
|
|
|
*/
|
2016-12-26 09:44:59 -05:00
|
|
|
fun getLocaleFromString(pref: String): Locale? {
|
|
|
|
if (pref.isNullOrEmpty()) {
|
|
|
|
return null
|
2016-12-20 18:42:46 -05:00
|
|
|
}
|
2016-12-26 09:44:59 -05:00
|
|
|
val parts = pref.split("_", "-")
|
|
|
|
val lang = parts[0]
|
|
|
|
val country = parts.getOrNull(1) ?: ""
|
|
|
|
return Locale(lang, country)
|
2016-12-13 14:47:46 -05:00
|
|
|
}
|
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* Changes the application's locale with a new preference.
|
|
|
|
*
|
|
|
|
* @param pref the new value stored in preferences.
|
|
|
|
*/
|
2016-12-26 09:44:59 -05:00
|
|
|
fun changeLocale(pref: String) {
|
|
|
|
appLocale = getLocaleFromString(pref)
|
2016-12-20 18:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the app's language from API 17.
|
|
|
|
*/
|
2016-12-13 14:47:46 -05:00
|
|
|
fun updateCfg(wrapper: ContextThemeWrapper) {
|
2016-12-20 18:42:46 -05:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && appLocale != null) {
|
2016-12-18 16:31:20 -05:00
|
|
|
val config = Configuration(preferences.context.resources.configuration)
|
2016-12-20 18:42:46 -05:00
|
|
|
config.setLocale(appLocale)
|
2016-12-13 14:47:46 -05:00
|
|
|
wrapper.applyOverrideConfiguration(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 18:42:46 -05:00
|
|
|
/**
|
|
|
|
* Updates the app's language for API 16 and lower.
|
|
|
|
*/
|
2016-12-15 12:51:12 -05:00
|
|
|
fun updateCfg(app: Application, config: Configuration) {
|
2016-12-13 15:07:48 -05:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
2016-12-20 18:42:46 -05:00
|
|
|
val configCopy = Configuration(config)
|
|
|
|
val displayMetrics = app.baseContext.resources.displayMetrics
|
|
|
|
configCopy.locale = appLocale ?: v16SystemLocale
|
|
|
|
app.baseContext.resources.updateConfiguration(configCopy, displayMetrics)
|
2016-12-13 14:47:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|