diff --git a/src/main/kotlin/com/looker/droidify/Common.kt b/src/main/kotlin/com/looker/droidify/Common.kt index a943ba42..446a2f83 100644 --- a/src/main/kotlin/com/looker/droidify/Common.kt +++ b/src/main/kotlin/com/looker/droidify/Common.kt @@ -9,5 +9,8 @@ object Common { const val NOTIFICATION_ID_UPDATES = 2 const val NOTIFICATION_ID_DOWNLOADING = 3 + const val PREFS_LANGUAGE = "languages" + const val PREFS_LANGUAGE_DEFAULT = "system" + const val JOB_ID_SYNC = 1 } diff --git a/src/main/kotlin/com/looker/droidify/utility/Utils.kt b/src/main/kotlin/com/looker/droidify/utility/Utils.kt index b3c92c15..9bdd0d20 100644 --- a/src/main/kotlin/com/looker/droidify/utility/Utils.kt +++ b/src/main/kotlin/com/looker/droidify/utility/Utils.kt @@ -3,7 +3,11 @@ package com.looker.droidify.utility import android.content.Context import android.content.pm.PackageInfo import android.content.pm.Signature +import android.content.res.Configuration import android.graphics.drawable.Drawable +import android.os.Build +import com.looker.droidify.BuildConfig +import com.looker.droidify.Common.PREFS_LANGUAGE_DEFAULT import com.looker.droidify.R import com.looker.droidify.content.Preferences import com.looker.droidify.entity.InstalledItem @@ -110,4 +114,51 @@ object Utils { ) } else Unit } + + fun Context.setLanguage(): Configuration { + var setLocalCode = Preferences[Preferences.Key.Language] + if (setLocalCode == PREFS_LANGUAGE_DEFAULT) { + setLocalCode = Locale.getDefault().language + } + val config = resources.configuration + val sysLocale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + config.locales[0] + } else { + config.locale + } + if (setLocalCode != sysLocale.language || setLocalCode != "${sysLocale.language}-r${sysLocale.country}") { + val newLocale = getLocaleOfCode(setLocalCode) + Locale.setDefault(newLocale) + config.setLocale(newLocale) + } + return config + } + + val languagesList: List + get() { + val entryVals = arrayOfNulls(1) + entryVals[0] = PREFS_LANGUAGE_DEFAULT + return entryVals.plus(BuildConfig.DETECTED_LOCALES.sorted()).filterNotNull() + } + + fun translateLocale(locale: Locale): String { + val country = locale.getDisplayCountry(locale) + val language = locale.getDisplayLanguage(locale) + return (language.replaceFirstChar { it.uppercase(Locale.getDefault()) } + + (if (country.isNotEmpty() && country.compareTo(language, true) != 0) + "($country)" else "")) + } + + fun Context.getLocaleOfCode(localeCode: String): Locale = when { + localeCode.isEmpty() -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + resources.configuration.locales[0] + } else { + resources.configuration.locale + } + localeCode.contains("-r") -> Locale( + localeCode.substring(0, 2), + localeCode.substring(4) + ) + else -> Locale(localeCode) + } }