-
Notifications
You must be signed in to change notification settings - Fork 710
Support Per-app language preferences #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
99f1b5a
943a4dd
83771c1
96ec463
6d5cc0a
4758f5d
730dc4f
402dcca
dc91a1e
a411b65
c3f3466
8ce2941
feaa2c8
286c209
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,80 +20,100 @@ package com.vrem.util | |
| import java.util.Locale | ||
| import java.util.SortedMap | ||
|
|
||
| private object SyncAvoid { | ||
| val defaultLocale: Locale = Locale.getDefault() | ||
| val countryCodes: Set<String> = Locale.getISOCountries().toSet() | ||
| val availableLocales: List<Locale> = Locale.getAvailableLocales().filter { countryCodes.contains(it.country) } | ||
|
|
||
| val countriesLocales: SortedMap<String, Locale> = | ||
| availableLocales | ||
| .associateBy { it.country.toCapitalize(Locale.getDefault()) } | ||
| .toSortedMap() | ||
| val supportedLocales: List<Locale> = | ||
| setOf( | ||
| BULGARIAN, | ||
| DUTCH, | ||
| GREEK, | ||
| HUNGARIAN, | ||
| Locale.SIMPLIFIED_CHINESE, | ||
| Locale.TRADITIONAL_CHINESE, | ||
| Locale.ENGLISH, | ||
| Locale.FRENCH, | ||
| Locale.GERMAN, | ||
| Locale.ITALIAN, | ||
| Locale.JAPANESE, | ||
| POLISH, | ||
| PORTUGUESE_BRAZIL, | ||
| PORTUGUESE_PORTUGAL, | ||
| SPANISH, | ||
| RUSSIAN, | ||
| TURKISH, | ||
| UKRAINIAN, | ||
| defaultLocale, | ||
| ).toList() | ||
| } | ||
| private val currentLocale: Locale get() = Locale.getDefault() | ||
| private val countryCodes: Set<String> = Locale.getISOCountries().toSet() | ||
| private val availableLocales: List<Locale> = Locale.getAvailableLocales().filter { countryCodes.contains(it.country) } | ||
| private val countriesLocales: SortedMap<String, Locale> = | ||
| availableLocales | ||
| .associateBy { it.country.toCapitalize(currentLocale) } | ||
| .toSortedMap() | ||
|
|
||
| val BULGARIAN: Locale = Locale.forLanguageTag("bg") | ||
| val CHINESE: Locale = Locale.forLanguageTag("zh") | ||
| val CHINESE_SIMPLIFIED: Locale = Locale.forLanguageTag("zh-Hans") | ||
| val CHINESE_TRADITIONAL: Locale = Locale.forLanguageTag("zh-Hant") | ||
| val DUTCH: Locale = Locale.forLanguageTag("nl") | ||
| val ENGLISH: Locale = Locale.forLanguageTag("en") | ||
| val FRENCH: Locale = Locale.forLanguageTag("fr") | ||
| val GERMAN: Locale = Locale.forLanguageTag("de") | ||
| val GREEK: Locale = Locale.forLanguageTag("el") | ||
| val HUNGARIAN: Locale = Locale.forLanguageTag("hu") | ||
| val ITALIAN: Locale = Locale.forLanguageTag("it") | ||
| val JAPANESE: Locale = Locale.forLanguageTag("ja") | ||
| val POLISH: Locale = Locale.forLanguageTag("pl") | ||
| val PORTUGUESE_PORTUGAL: Locale = Locale.forLanguageTag("pt-PT") | ||
| val PORTUGUESE_BRAZIL: Locale = Locale.forLanguageTag("pt-BR") | ||
| val SPANISH: Locale = Locale.forLanguageTag("es") | ||
| val PORTUGUESE_PORTUGAL: Locale = Locale.forLanguageTag("pt-PT") | ||
| val RUSSIAN: Locale = Locale.forLanguageTag("ru") | ||
| val SPANISH: Locale = Locale.forLanguageTag("es") | ||
| val TURKISH: Locale = Locale.forLanguageTag("tr") | ||
| val UKRAINIAN: Locale = Locale.forLanguageTag("uk") | ||
|
|
||
| private const val SEPARATOR: String = "_" | ||
| val baseSupportedLocales: List<Locale> = | ||
| listOf( | ||
| BULGARIAN, | ||
| CHINESE_SIMPLIFIED, | ||
| CHINESE_TRADITIONAL, | ||
| DUTCH, | ||
| ENGLISH, | ||
| FRENCH, | ||
| GERMAN, | ||
| GREEK, | ||
| HUNGARIAN, | ||
| ITALIAN, | ||
| JAPANESE, | ||
| POLISH, | ||
| PORTUGUESE_BRAZIL, | ||
| PORTUGUESE_PORTUGAL, | ||
| RUSSIAN, | ||
| SPANISH, | ||
| TURKISH, | ||
| UKRAINIAN, | ||
| ) | ||
|
|
||
| fun findByCountryCode(countryCode: String): Locale = | ||
| SyncAvoid.availableLocales.firstOrNull { countryCode.toCapitalize(Locale.getDefault()) == it.country } | ||
| ?: SyncAvoid.defaultLocale | ||
| availableLocales.firstOrNull { countryCode.uppercase(Locale.ROOT) == it.country } | ||
| ?: currentLocale | ||
|
|
||
| fun allCountries(): List<Locale> = SyncAvoid.countriesLocales.values.toList() | ||
| fun allCountries(): List<Locale> = countriesLocales.values.toList() | ||
|
|
||
| fun findByLanguageTag(languageTag: String): Locale { | ||
| val languageTagPredicate: (Locale) -> Boolean = { | ||
| val locale: Locale = fromLanguageTag(languageTag) | ||
| it.language == locale.language && it.country == locale.country | ||
| } | ||
| return SyncAvoid.supportedLocales.firstOrNull(languageTagPredicate) ?: SyncAvoid.defaultLocale | ||
| } | ||
| fun supportedLanguages(): List<Locale> = (baseSupportedLocales + currentLocale).distinct() | ||
|
|
||
| fun supportedLanguages(): List<Locale> = SyncAvoid.supportedLocales | ||
| fun supportedLanguageTags(): List<String> = listOf("") + baseSupportedLocales.map { it.toLanguageTag() } | ||
|
|
||
| fun defaultCountryCode(): String = SyncAvoid.defaultLocale.country | ||
| private fun normalizeLanguageTag(languageTag: String): String = languageTag.replace('_', '-').trim() | ||
|
|
||
| fun defaultLanguageTag(): String = toLanguageTag(SyncAvoid.defaultLocale) | ||
| private val chineseCountryToLocale: Map<String, Locale> = | ||
| mapOf( | ||
| "CN" to CHINESE_SIMPLIFIED, | ||
| "SG" to CHINESE_SIMPLIFIED, | ||
| "TW" to CHINESE_TRADITIONAL, | ||
| "HK" to CHINESE_TRADITIONAL, | ||
| "MO" to CHINESE_TRADITIONAL, | ||
| ) | ||
|
|
||
| fun toLanguageTag(locale: Locale): String = locale.language + SEPARATOR + locale.country | ||
| fun findByLanguageTag(languageTag: String): Locale { | ||
| val normalizedLanguageTag = normalizeLanguageTag(languageTag) | ||
| if (normalizedLanguageTag.isEmpty()) return currentLocale | ||
|
|
||
| val target = Locale.forLanguageTag(normalizedLanguageTag) | ||
| if (target.language.isEmpty()) return currentLocale | ||
|
|
||
| private fun fromLanguageTag(languageTag: String): Locale { | ||
| val codes: Array<String> = languageTag.split(SEPARATOR).toTypedArray() | ||
| return when (codes.size) { | ||
| 1 -> Locale.forLanguageTag(codes[0]) | ||
| 2 -> Locale.forLanguageTag("${codes[0]}-${codes[1].toCapitalize(Locale.getDefault())}") | ||
| else -> SyncAvoid.defaultLocale | ||
| if (target.language == "zh" && target.script.isEmpty()) { | ||
| if (target.country.isEmpty()) return CHINESE | ||
| return chineseCountryToLocale[target.country] ?: CHINESE | ||
| } | ||
|
|
||
| return baseSupportedLocales.find { it == target } | ||
| ?: baseSupportedLocales.find { it.language == target.language && it.script == target.script } | ||
| ?: baseSupportedLocales.find { it.language == target.language && it.country == target.country } | ||
| ?: baseSupportedLocales.find { it.language == target.language } | ||
| ?: currentLocale | ||
| } | ||
|
|
||
| fun currentCountryCode(): String = currentLocale.country | ||
|
|
||
| fun currentLanguageTag(): String = currentLocale.toLanguageTag() | ||
|
|
||
| fun toLanguageTag(locale: Locale): String = locale.toLanguageTag() | ||
|
|
||
| fun Locale.toSupportedLocaleTag(): String = findByLanguageTag(this.toLanguageTag()).toLanguageTag() | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,3 +27,5 @@ fun String.Companion.nullToEmpty(value: String?): String = value ?: String.EMPTY | |
| fun String.specialTrim(): String = this.trim { it <= ' ' }.replace(" +".toRegex(), String.SPACE_SEPARATOR) | ||
|
|
||
| fun String.toCapitalize(locale: Locale): String = this.replaceFirstChar { word -> word.uppercase(locale) } | ||
|
|
||
| fun String.titlecaseFirst(locale: Locale): String = replaceFirstChar { it.titlecase(locale) } | ||
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,7 +17,6 @@ | |||||||||||||
| */ | ||||||||||||||
| package com.vrem.wifianalyzer | ||||||||||||||
|
|
||||||||||||||
| import android.content.Context | ||||||||||||||
| import android.content.SharedPreferences | ||||||||||||||
| import android.content.SharedPreferences.OnSharedPreferenceChangeListener | ||||||||||||||
| import android.content.res.Configuration | ||||||||||||||
|
|
@@ -26,18 +25,17 @@ import android.view.Menu | |||||||||||||
| import android.view.MenuItem | ||||||||||||||
| import android.view.View | ||||||||||||||
| import androidx.appcompat.app.AppCompatActivity | ||||||||||||||
| import androidx.appcompat.app.AppCompatDelegate | ||||||||||||||
| import androidx.core.os.LocaleListCompat | ||||||||||||||
| import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen | ||||||||||||||
| import androidx.core.view.GravityCompat | ||||||||||||||
| import androidx.drawerlayout.widget.DrawerLayout | ||||||||||||||
| import com.google.android.material.navigation.NavigationView | ||||||||||||||
| import com.vrem.annotation.OpenClass | ||||||||||||||
| import com.vrem.util.createContext | ||||||||||||||
| import com.vrem.wifianalyzer.navigation.NavigationMenu | ||||||||||||||
| import com.vrem.wifianalyzer.navigation.NavigationMenuControl | ||||||||||||||
| import com.vrem.wifianalyzer.navigation.NavigationMenuController | ||||||||||||||
| import com.vrem.wifianalyzer.navigation.options.OptionMenu | ||||||||||||||
| import com.vrem.wifianalyzer.settings.Repository | ||||||||||||||
| import com.vrem.wifianalyzer.settings.Settings | ||||||||||||||
| import com.vrem.wifianalyzer.wifi.accesspoint.ConnectionView | ||||||||||||||
| import com.vrem.wifianalyzer.wifi.scanner.ScannerService | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -52,15 +50,13 @@ class MainActivity : | |||||||||||||
| internal lateinit var optionMenu: OptionMenu | ||||||||||||||
| internal lateinit var connectionView: ConnectionView | ||||||||||||||
|
|
||||||||||||||
| override fun attachBaseContext(newBase: Context) = | ||||||||||||||
| super.attachBaseContext(newBase.createContext(Settings(Repository(newBase)).languageLocale())) | ||||||||||||||
|
|
||||||||||||||
| override fun onCreate(savedInstanceState: Bundle?) { | ||||||||||||||
| val mainContext = MainContext.INSTANCE | ||||||||||||||
| mainContext.initialize(this, largeScreen) | ||||||||||||||
|
|
||||||||||||||
| val settings = mainContext.settings | ||||||||||||||
| settings.initializeDefaultValues() | ||||||||||||||
| settings.syncLanguage() | ||||||||||||||
| setTheme(settings.themeStyle().themeNoActionBar) | ||||||||||||||
|
|
||||||||||||||
| mainReload = MainReload(settings) | ||||||||||||||
|
|
@@ -120,6 +116,18 @@ class MainActivity : | |||||||||||||
| sharedPreferences: SharedPreferences, | ||||||||||||||
| key: String?, | ||||||||||||||
| ) { | ||||||||||||||
| val languageKey = getString(R.string.language_key) | ||||||||||||||
| if (key == languageKey) { | ||||||||||||||
| val languageTag = sharedPreferences.getString(languageKey, "") | ||||||||||||||
| val locales = | ||||||||||||||
| languageTag | ||||||||||||||
| ?.takeIf { it.isNotEmpty() } | ||||||||||||||
| ?.let(LocaleListCompat::forLanguageTags) | ||||||||||||||
| ?: LocaleListCompat.getEmptyLocaleList() | ||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| // When the language preference changes, update the application locales. | |
| // This call triggers a configuration change and causes the activity to be recreated. | |
| // After recreation, onCreate() is invoked again and settings.syncLanguage() is called, | |
| // which syncs the effective language value back to shared preferences. |
Copilot
AI
Jan 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new language preference change handling logic in onSharedPreferenceChanged (lines 119-129) lacks test coverage. This is critical functionality that calls AppCompatDelegate.setApplicationLocales() when the language preference changes. Consider adding test cases that verify the locale is correctly set when the language_key preference changes, including edge cases like empty strings and null values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initialization of countriesLocales on line 28 uses currentLocale which calls Locale.getDefault() at initialization time. This means the map keys will be capitalized using the default locale at app startup. If the user later changes the app's language preference, the map keys will still be in the original locale's capitalization, which could cause inconsistencies in country name display. Consider making countriesLocales a function that computes the map dynamically, or ensure this is the intended behavior and document it.