feat(i18n): data-driven language picker + locale config
Make the supported-language list a single source of truth so community translations show up with no code change: add res/xml/locales_config.xml (en, de) and reference it via android:localeConfig, which also surfaces the per-app language entry in Android 13+ system settings. Rewrite AppLanguage to parse locales_config.xml for the supported BCP-47 tags and expose currentTag/apply/displayName (autonyms), dropping the hardcoded LanguagePref enum; the Settings picker is now built from that list. Remove the now-unused settings_language_german/english strings. Adding a language is now: drop in values-<tag>/strings.xml and add one <locale> line. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:localeConfig="@xml/locales_config"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.Calendula"
|
||||
|
||||
@@ -1,36 +1,78 @@
|
||||
package de.jeanlucmakiola.calendula.ui.settings
|
||||
|
||||
import android.content.Context
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.core.os.LocaleListCompat
|
||||
import de.jeanlucmakiola.calendula.R
|
||||
import org.xmlpull.v1.XmlPullParser
|
||||
import java.util.Locale
|
||||
|
||||
/** UI-facing language choice. AUTO follows the system languages. */
|
||||
enum class LanguagePref { AUTO, GERMAN, ENGLISH }
|
||||
private const val ANDROID_NS = "http://schemas.android.com/apk/res/android"
|
||||
|
||||
/**
|
||||
* Per-app language via AppCompatDelegate. On API 33+ this delegates to the
|
||||
* platform per-app-languages API; below that the appcompat backport persists
|
||||
* the choice itself (manifest `autoStoreLocales` service), so we don't mirror
|
||||
* it in DataStore. Setting a locale recreates the activity, which re-reads the
|
||||
* current value for the dropdown.
|
||||
* Per-app language via AppCompatDelegate, driven by res/xml/locales_config.xml.
|
||||
*
|
||||
* That file is the single source of truth for which languages we ship: dropping
|
||||
* in a values-<tag> translation and adding a matching `<locale>` entry makes the
|
||||
* language show up here and in the system per-app-language settings, with no
|
||||
* other code change. The system-default choice is represented as `null`.
|
||||
*
|
||||
* On API 33+ this delegates to the platform per-app-languages API; below that
|
||||
* the appcompat backport persists the choice itself (manifest `autoStoreLocales`
|
||||
* service), so we don't mirror it in DataStore. Setting a locale recreates the
|
||||
* activity, which re-reads the current value for the picker.
|
||||
*/
|
||||
object AppLanguage {
|
||||
|
||||
fun current(): LanguagePref {
|
||||
val locales = AppCompatDelegate.getApplicationLocales()
|
||||
if (locales.isEmpty) return LanguagePref.AUTO
|
||||
return when (locales[0]?.language) {
|
||||
"de" -> LanguagePref.GERMAN
|
||||
"en" -> LanguagePref.ENGLISH
|
||||
else -> LanguagePref.AUTO
|
||||
/**
|
||||
* The BCP-47 tags the app ships translations for, in declaration order, as
|
||||
* listed in locales_config.xml. Returns whatever could be parsed; a missing
|
||||
* or malformed config yields an empty list (the picker then offers only the
|
||||
* system-default entry rather than crashing).
|
||||
*/
|
||||
fun supportedTags(context: Context): List<String> {
|
||||
val tags = mutableListOf<String>()
|
||||
val parser = context.resources.getXml(R.xml.locales_config)
|
||||
try {
|
||||
var event = parser.eventType
|
||||
while (event != XmlPullParser.END_DOCUMENT) {
|
||||
if (event == XmlPullParser.START_TAG && parser.name == "locale") {
|
||||
parser.getAttributeValue(ANDROID_NS, "name")?.let(tags::add)
|
||||
}
|
||||
event = parser.next()
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
// Fall back to whatever was parsed before the failure.
|
||||
} finally {
|
||||
parser.close()
|
||||
}
|
||||
return tags
|
||||
}
|
||||
|
||||
fun apply(pref: LanguagePref) {
|
||||
val locales = when (pref) {
|
||||
LanguagePref.AUTO -> LocaleListCompat.getEmptyLocaleList()
|
||||
LanguagePref.GERMAN -> LocaleListCompat.forLanguageTags("de")
|
||||
LanguagePref.ENGLISH -> LocaleListCompat.forLanguageTags("en")
|
||||
/** The applied app language as a BCP-47 tag, or `null` when following the system. */
|
||||
fun currentTag(): String? {
|
||||
val locales = AppCompatDelegate.getApplicationLocales()
|
||||
return if (locales.isEmpty) null else locales[0]?.toLanguageTag()
|
||||
}
|
||||
|
||||
/** Apply a BCP-47 tag, or `null` to follow the system languages. */
|
||||
fun apply(tag: String?) {
|
||||
val locales = if (tag == null) {
|
||||
LocaleListCompat.getEmptyLocaleList()
|
||||
} else {
|
||||
LocaleListCompat.forLanguageTags(tag)
|
||||
}
|
||||
AppCompatDelegate.setApplicationLocales(locales)
|
||||
}
|
||||
|
||||
/**
|
||||
* The autonym for a tag — the language's own name in its own script, e.g.
|
||||
* "Deutsch", "English", "Français" — so users find their language regardless
|
||||
* of the current UI language. Capitalised per the language's own rules.
|
||||
*/
|
||||
fun displayName(tag: String): String {
|
||||
val locale = Locale.forLanguageTag(tag)
|
||||
return locale.getDisplayName(locale)
|
||||
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(locale) else it.toString() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,11 +188,15 @@ private fun SettingsHub(
|
||||
|
||||
@Composable
|
||||
private fun LanguageRow(position: Position) {
|
||||
val context = LocalContext.current
|
||||
// Setting a locale recreates the activity; mirror the choice locally so the
|
||||
// row updates instantly even before the recreation lands.
|
||||
var current by remember { mutableStateOf(AppLanguage.current()) }
|
||||
var current by remember { mutableStateOf(AppLanguage.currentTag()) }
|
||||
var showDialog by remember { mutableStateOf(false) }
|
||||
|
||||
// null = follow the system; the rest are BCP-47 tags from locales_config.xml.
|
||||
val options = remember { listOf<String?>(null) + AppLanguage.supportedTags(context) }
|
||||
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.settings_language),
|
||||
summary = languageLabel(current),
|
||||
@@ -204,7 +208,7 @@ private fun LanguageRow(position: Position) {
|
||||
if (showDialog) {
|
||||
OptionPickerDialog(
|
||||
title = stringResource(R.string.settings_language),
|
||||
options = LanguagePref.entries,
|
||||
options = options,
|
||||
selected = current,
|
||||
label = { languageLabel(it) },
|
||||
onSelect = {
|
||||
@@ -598,10 +602,5 @@ private fun weekStartLabel(pref: WeekStartPref): String = stringResource(
|
||||
)
|
||||
|
||||
@Composable
|
||||
private fun languageLabel(pref: LanguagePref): String = stringResource(
|
||||
when (pref) {
|
||||
LanguagePref.AUTO -> R.string.settings_language_auto
|
||||
LanguagePref.GERMAN -> R.string.settings_language_german
|
||||
LanguagePref.ENGLISH -> R.string.settings_language_english
|
||||
},
|
||||
)
|
||||
private fun languageLabel(tag: String?): String =
|
||||
if (tag == null) stringResource(R.string.settings_language_auto) else AppLanguage.displayName(tag)
|
||||
|
||||
@@ -254,8 +254,6 @@
|
||||
<string name="settings_section_language">Sprache</string>
|
||||
<string name="settings_language">App-Sprache</string>
|
||||
<string name="settings_language_auto">Systemstandard</string>
|
||||
<string name="settings_language_german">Deutsch</string>
|
||||
<string name="settings_language_english">English</string>
|
||||
<!-- Hub category subtitles -->
|
||||
<string name="settings_appearance_subtitle">Design, dynamische Farben, Wochenstart</string>
|
||||
<string name="settings_event_form_subtitle">Standardfelder für neue Termine</string>
|
||||
|
||||
@@ -251,8 +251,6 @@
|
||||
<string name="settings_section_language">Language</string>
|
||||
<string name="settings_language">App language</string>
|
||||
<string name="settings_language_auto">System default</string>
|
||||
<string name="settings_language_german">Deutsch</string>
|
||||
<string name="settings_language_english">English</string>
|
||||
<!-- Hub category subtitles -->
|
||||
<string name="settings_appearance_subtitle">Theme, dynamic colour, week start</string>
|
||||
<string name="settings_event_form_subtitle">Default fields for new events</string>
|
||||
|
||||
13
app/src/main/res/xml/locales_config.xml
Normal file
13
app/src/main/res/xml/locales_config.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
The languages Calendula ships translations for. This is the single source of
|
||||
truth: each entry must have a matching res/values-<tag>/strings.xml, and is
|
||||
surfaced automatically in both the in-app language picker (parsed at runtime
|
||||
by AppLanguage) and the system per-app language settings (Android 13+, via
|
||||
android:localeConfig in the manifest). To add a community translation, drop
|
||||
in the values-<tag> folder and add one <locale> line here.
|
||||
-->
|
||||
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<locale android:name="en" />
|
||||
<locale android:name="de" />
|
||||
</locale-config>
|
||||
Reference in New Issue
Block a user