Skip to content

A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.

Languages: the language picker (Android's per-app language from 13, the app's own before), Languages list, manifest

Claude0f9695375cfc
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 60ea42df..95dc9518 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -24,6 +24,8 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:allowBackup="false"
+ android:supportsRtl="true"
+ android:localeConfig="@xml/locales_config"
android:enableOnBackInvokedCallback="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
diff --git a/android/app/src/main/kotlin/com/coucou/android/MainActivity.kt b/android/app/src/main/kotlin/com/coucou/android/MainActivity.kt
index ddb948c2..6b66ede3 100644
--- a/android/app/src/main/kotlin/com/coucou/android/MainActivity.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/MainActivity.kt
@@ -1,6 +1,7 @@
package com.coucou.android
import android.Manifest
+import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
@@ -142,8 +143,13 @@ class MainActivity : ComponentActivity() {
private var closedApproval by mutableStateOf<String?>(null)
private var questionPill by mutableStateOf<String?>(null)
private var confirming = false
+ override fun attachBaseContext(newBase: Context) {
+ super.attachBaseContext(com.coucou.android.app.AppLanguage.wrap(newBase))
+ }
+
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
+ model.languageChanged(com.coucou.android.app.AppLanguage.current(this))
enableEdgeToEdge()
if (Build.VERSION.SDK_INT >= 33 && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1)
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/AppLanguage.kt b/android/app/src/main/kotlin/com/coucou/android/app/AppLanguage.kt
new file mode 100644
index 00000000..36b47f77
--- /dev/null
+++ b/android/app/src/main/kotlin/com/coucou/android/app/AppLanguage.kt
@@ -0,0 +1,72 @@
+package com.coucou.android.app
+
+import android.app.Activity
+import android.app.LocaleManager
+import android.content.Context
+import android.content.res.Configuration
+import android.content.res.Resources
+import android.os.Build
+import android.os.LocaleList
+import com.coucou.android.core.Languages
+import com.coucou.android.core.Texts
+import java.util.Locale
+
+/**
+ * The language of the app: the phone's own, or one of [Languages.ALL]. On Android 13 and later the choice is Android's per-app language
+ * (LocaleManager), so it also shows in Android's own settings (Apps, Coucou for Android, Language). On Android 11 and 12 there is no such
+ * thing, so the choice is kept in the app and applied to the app's resources (and to the activity when it starts).
+ */
+object AppLanguage {
+ private const val PREFS = "coucou_ui"
+ private const val KEY = "app_language"
+
+ /** The language in force: a tag of [Languages.ALL], or [Languages.SYSTEM] for the phone's own. */
+ fun current(context: Context): String =
+ if (Build.VERSION.SDK_INT >= 33) {
+ val locales = context.getSystemService(LocaleManager::class.java).applicationLocales
+ if (locales.isEmpty) Languages.SYSTEM else Languages.match(locales[0].toLanguageTag())
+ } else {
+ Languages.match(context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString(KEY, ""))
+ }
+
+ /** Chooses [tag] ([Languages.SYSTEM] for the phone's own). An activity is recreated so its text changes at once. */
+ fun set(context: Context, tag: String) {
+ val language = Languages.match(tag)
+ if (Build.VERSION.SDK_INT >= 33) {
+ // The system saves the choice, shows it in its own settings and restarts the activity with the new language.
+ context.getSystemService(LocaleManager::class.java).applicationLocales =
+ if (language.isEmpty()) LocaleList.getEmptyLocaleList() else LocaleList.forLanguageTags(language)
+ } else {
+ context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().putString(KEY, language).apply()
+ applyToApp(context.applicationContext)
+ (context as? Activity)?.recreate()
+ }
+ }
+
+ /** Before Android 13: the context an activity or the application is built on, in the language chosen. Later: unchanged. */
+ fun wrap(base: Context): Context {
+ if (Build.VERSION.SDK_INT >= 33) return base
+ val language = current(base)
+ if (language.isEmpty()) return base
+ val locale = Locale.forLanguageTag(language)
+ Locale.setDefault(locale)
+ val config = Configuration(base.resources.configuration).apply { setLocale(locale); setLayoutDirection(locale) }
+ return base.createConfigurationContext(config)
+ }
+
+ /** Before Android 13: puts the language in force in the application's resources (they are shared by what the app builds text with). */
+ private fun applyToApp(app: Context) {
+ val language = current(app)
+ val locale = if (language.isEmpty()) Resources.getSystem().configuration.locales[0] else Locale.forLanguageTag(language)
+ Locale.setDefault(locale)
+ val config = Configuration(app.resources.configuration).apply { setLocale(locale); setLayoutDirection(locale) }
+ @Suppress("DEPRECATION")
+ app.resources.updateConfiguration(config, app.resources.displayMetrics)
+ }
+
+ /** The texts of the pure rules ([Texts]), looked up in the string resources of the application, in whatever language is in force. */
+ fun texts(app: Context): Texts.Source = object : Texts.Source {
+ override fun string(key: String): String? = TextIds.strings[key]?.let { app.resources.getString(it) }
+ override fun plural(key: String, count: Int): String? = TextIds.plurals[key]?.let { app.resources.getQuantityString(it, count) }
+ }
+}
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/AppModel.kt b/android/app/src/main/kotlin/com/coucou/android/app/AppModel.kt
index c265d8ec..b72830e3 100644
--- a/android/app/src/main/kotlin/com/coucou/android/app/AppModel.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/app/AppModel.kt
@@ -133,6 +133,20 @@ class AppModel(private val context: Context) : LinkListener {
}
}
+ /** The language the surfaces (Widgets, the ongoing notification) were last drawn in. */
+ private var surfacesLanguage: String? = null
+
+ /** The app's language is [tag] now: what the Widgets and the notification say is written again in it (once per change). */
+ fun languageChanged(tag: String) {
+ if (surfacesLanguage == tag) return
+ val first = surfacesLanguage == null
+ surfacesLanguage = tag
+ if (first) return // the first look at it, at start: nothing was drawn in another language
+ notifier.forgetOngoing()
+ WidgetHub.refreshAll(context)
+ pushGlance()
+ }
+
/** What the user chose in Settings (sound, notices, quiet hours). */
var settings by mutableStateOf(UserSettings.load(kv)); private set
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/CoucouApp.kt b/android/app/src/main/kotlin/com/coucou/android/app/CoucouApp.kt
index e1ee7763..425cc4d9 100644
--- a/android/app/src/main/kotlin/com/coucou/android/app/CoucouApp.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/app/CoucouApp.kt
@@ -1,7 +1,20 @@
package com.coucou.android.app
import android.app.Application
+import android.content.Context
+import com.coucou.android.core.Texts
class CoucouApp : Application() {
val model: AppModel by lazy { AppModel(this) }
+
+ override fun attachBaseContext(base: Context) {
+ // Before Android 13 the language chosen in the app is applied here (from 13 on the system does it).
+ super.attachBaseContext(AppLanguage.wrap(base))
+ }
+
+ override fun onCreate() {
+ super.onCreate()
+ // The words of the pure rules follow the app's language like every other text.
+ Texts.source = AppLanguage.texts(this)
+ }
}
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/Notifications.kt b/android/app/src/main/kotlin/com/coucou/android/app/Notifications.kt
index 91e1313f..2ec25f2d 100644
--- a/android/app/src/main/kotlin/com/coucou/android/app/Notifications.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/app/Notifications.kt
@@ -138,6 +138,9 @@ class Notifications(private val context: Context) {
return b.build()
}
+ /** The language changed: the next push posts the ongoing notification again, whatever it said before. */
+ fun forgetOngoing() = ongoingGate.forget()
+
/** Only posted again when what it shows changes ([OngoingGate]); the service's own first post counts as the last one. */
private val ongoingGate = OngoingGate()
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/WidgetHub.kt b/android/app/src/main/kotlin/com/coucou/android/app/WidgetHub.kt
index cac73c88..14e9952b 100644
--- a/android/app/src/main/kotlin/com/coucou/android/app/WidgetHub.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/app/WidgetHub.kt
@@ -52,10 +52,11 @@ object WidgetHub {
runCatching {
val manager = AppWidgetManager.getInstance(context)
var pushed = false
+ val localized = AppLanguage.wrap(context) // before Android 13 the language chosen in the app
for (kind in widgets) {
val ids = manager.getAppWidgetIds(ComponentName(context, kind.provider))
if (ids.isEmpty()) continue
- val rendered = kind.views(context, content)
+ val rendered = kind.views(localized, content)
for (id in ids) manager.updateAppWidget(id, rendered)
pushed = true
}
@@ -66,7 +67,7 @@ object WidgetHub {
/** Draws the widgets the system just created or resized, from what the app says now. */
fun refresh(context: Context, manager: AppWidgetManager, ids: IntArray, views: (Context, WidgetContent) -> RemoteViews) {
- val rendered = views(context, current(context))
+ val rendered = views(AppLanguage.wrap(context), current(context))
for (id in ids) runCatching { manager.updateAppWidget(id, rendered) }
}
diff --git a/android/app/src/main/kotlin/com/coucou/android/core/Languages.kt b/android/app/src/main/kotlin/com/coucou/android/core/Languages.kt
new file mode 100644
index 00000000..bbbcbf43
--- /dev/null
+++ b/android/app/src/main/kotlin/com/coucou/android/core/Languages.kt
@@ -0,0 +1,30 @@
+package com.coucou.android.core
+
+/**
+ * The languages of the app: the BCP 47 tag, the name in the language itself (so a person can find it whatever the phone speaks) and whether it
+ * is written right to left. [ALL] is what the picker offers; res/xml/locales_config.xml (Android's own per-app language screen) lists the same
+ * tags, and a test checks that they match the translation files.
+ */
+object Languages {
+ data class Language(val tag: String, val name: String, val rtl: Boolean = false)
+
+ /** The empty tag: follow the phone. */
+ const val SYSTEM = ""
+
+ val ALL = listOf(
+ Language("fr", "Français"),
+ Language("es", "Español"),
+ Language("pt-BR", "Português (Brasil)"),
+ )
+
+ /** The tag of the language that [tag] (as Android reports it, e.g. "zh-Hans-CN" or "pt-PT") stands for in the app; [SYSTEM] if there is none. */
+ fun match(tag: String?): String {
+ if (tag.isNullOrBlank()) return SYSTEM
+ ALL.firstOrNull { it.tag.equals(tag, ignoreCase = true) }?.let { return it.tag }
+ val language = tag.substringBefore('-').substringBefore('_').lowercase()
+ return ALL.firstOrNull { it.tag.substringBefore('-').equals(language, ignoreCase = true) }?.tag ?: SYSTEM
+ }
+
+ /** True when the language chosen reads right to left (the picker's own order and the icons follow Android's layout direction). */
+ fun isRtl(tag: String): Boolean = ALL.firstOrNull { it.tag == tag }?.rtl ?: false
+}
diff --git a/android/app/src/main/kotlin/com/coucou/android/ui/SettingsScreens.kt b/android/app/src/main/kotlin/com/coucou/android/ui/SettingsScreens.kt
index 4e79b582..497b547b 100644
--- a/android/app/src/main/kotlin/com/coucou/android/ui/SettingsScreens.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/ui/SettingsScreens.kt
@@ -1,5 +1,14 @@
package com.coucou.android.ui
+import com.coucou.android.core.Languages
+import com.coucou.android.app.AppLanguage
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.runtime.setValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.material3.AlertDialog
+import androidx.compose.foundation.verticalScroll
+import androidx.compose.foundation.rememberScrollState
+import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
@@ -147,6 +156,45 @@ private fun <T> ChoiceRow(title: String, options: List<Pair<T, String>>, selecte
}
}
+/** The language of the app: the row shows the one in force; a tap opens the list (the phone's own first, then each language by its own name). */
+@Composable
+private fun LanguageRow() {
+ val context = LocalContext.current
+ val t = tokens()
+ var open by remember { mutableStateOf(false) }
+ val current = AppLanguage.current(context)
+ val system = stringResource(R.string.language_system)
+ val currentName = Languages.ALL.firstOrNull { it.tag == current }?.name ?: system
+ ListRow(
+ stringResource(R.string.settings_language), hint = currentName, onClick = { open = true },
+ trailing = { CoucouIcon(IconKind.CHEVRON, tint = t.textDim.c(), size = 20.dp) },
+ )
+ if (open) {
+ AlertDialog(
+ onDismissRequest = { open = false },
+ confirmButton = { TextButton(onClick = { open = false }) { Text(stringResource(R.string.action_cancel)) } },
+ title = { Text(stringResource(R.string.settings_language)) },
+ text = {
+ Column(Modifier.verticalScroll(rememberScrollState())) {
+ Text(stringResource(R.string.language_picker_hint), Modifier.padding(bottom = 8.dp), style = TypeScale.SECONDARY.style(t.textDim.c()))
+ val options = listOf(Languages.SYSTEM to system) + Languages.ALL.map { it.tag to it.name }
+ for ((tag, label) in options) {
+ Row(
+ Modifier.fillMaxWidth().heightIn(min = Spacing.MIN_TOUCH.dp)
+ .selectable(selected = tag == current, role = Role.RadioButton) { open = false; AppLanguage.set(context, tag) }
+ .padding(vertical = 8.dp),
+ verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp),
+ ) {
+ Text(label, Modifier.weight(1f), style = TypeScale.BODY.style(t.text.c()))
+ if (tag == current) CoucouIcon(IconKind.CHECK, tint = t.online.c(), size = 22.dp)
+ }
+ }
+ }
+ },
+ )
+ }
+}
+
/** Room under a list so its last row can scroll above the floating bar (64 + 16 gap + 16 margin). */
val BarClearance = 96.dp
@@ -201,6 +249,8 @@ fun SettingsScreen(model: AppModel, onHistory: () -> Unit, onGallery: () -> Unit
item { SectionHeading(stringResource(R.string.settings_section_display)) }
item {
Panel {
+ LanguageRow()
+ RowDivider()
ChoiceRow(
stringResource(R.string.settings_theme),
listOf(
diff --git a/android/app/src/test/kotlin/com/coucou/android/EnglishOnlyTest.kt b/android/app/src/test/kotlin/com/coucou/android/EnglishOnlyTest.kt
deleted file mode 100644
index 392bbb48..00000000
--- a/android/app/src/test/kotlin/com/coucou/android/EnglishOnlyTest.kt
+++ /dev/null
@@ -1,41 +0,0 @@
-package com.coucou.android
-
-import java.io.File
-import org.junit.Assert.assertEquals
-import org.junit.Assert.assertFalse
-import org.junit.Assert.assertTrue
-import org.junit.Test
-
-/** The app is English only, whatever the phone's language is. */
-class EnglishOnlyTest {
- private fun read(path: String) = File(path).readText()
-
- @Test fun theBuildKeepsOnlyEnglishResources() {
- val gradle = read("build.gradle.kts")
- assertTrue(Regex("""resourceConfigurations\s*\+=\s*listOf\("en"\)""").containsMatchIn(gradle))
- }
-
- @Test fun thereIsNoLanguageChoiceAnywhere() {
- assertFalse(read("src/main/AndroidManifest.xml").contains("localeConfig"))
- assertFalse(File("src/main/res/xml/locales_config.xml").exists())
- assertFalse(File("src/main/kotlin/com/coucou/android/core/Languages.kt").exists())
- val settings = read("src/main/kotlin/com/coucou/android/ui/SettingsScreens.kt")
- assertFalse(settings.contains("LocaleManager"))
- assertFalse(settings.contains("applicationLocales"))
- }
-
- @Test fun englishStringsAreTheSingleSourceOfTruth() {
- val source = read("../i18n/app-strings.json")
- val english = Regex("""name="([a-z_0-9]+)"""").findAll(read("src/main/res/values/strings.xml")).map { it.groupValues[1] }.toSet()
- val declared = Regex("""^\s{4}"([a-z_0-9]+)":""", RegexOption.MULTILINE).findAll(source.substringAfter(""""strings"""")).map { it.groupValues[1] }.toSet()
- assertEquals(declared, english)
- }
-
- @Test fun thereAreNoOtherLanguagesInTheRepo() {
- val folders = File("src/main/res").listFiles().orEmpty().map { it.name }.filter { it.startsWith("values") }
- // values-notnight is not a language: it is the light variant of the Widgets' colours, and holds colours only.
- assertEquals(listOf("values", "values-notnight"), folders.sorted())
- assertEquals(listOf("colors.xml"), File("src/main/res/values-notnight").listFiles().orEmpty().map { it.name })
- assertFalse(File("../i18n/extra.json").exists())
- }
-}
diff --git a/android/app/src/test/kotlin/com/coucou/android/TextsSourceTest.kt b/android/app/src/test/kotlin/com/coucou/android/TextsSourceTest.kt
new file mode 100644
index 00000000..8b38f1ef
--- /dev/null
+++ b/android/app/src/test/kotlin/com/coucou/android/TextsSourceTest.kt
@@ -0,0 +1,65 @@
+package com.coucou.android
+
+import java.io.File
+import org.json.JSONObject
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+/** The English written next to a key in the code is the English of i18n/app-strings.json, so a translation always translates what the code says. */
+class TextsSourceTest {
+ private val source = JSONObject(File("../i18n/app-strings.json").readText())
+ private val strings = source.getJSONObject("strings")
+ private val plurals = source.getJSONObject("plurals")
+ private fun unescape(s: String) = s.replace("\\$", "$").replace("\\\"", "\"").replace("\\\\", "\\")
+ private val kotlinFiles = File("src/main/kotlin").walkTopDown().filter { it.extension == "kt" }.toList()
+ private val lit = "\"((?:[^\"\\\\]|\\\\.)*)\""
+
+ @Test fun everyTextInTheCodeIsInTheEnglishSourceWithTheSameWords() {
+ var seen = 0
+ for (f in kotlinFiles) {
+ val text = f.readText()
+ for (m in Regex("""Texts\.t\("([a-z0-9_]+)",\s*$lit""").findAll(text)) {
+ seen++
+ assertTrue("${f.name}: key ${m.groupValues[1]} is not in app-strings.json", strings.has(m.groupValues[1]))
+ assertEquals("${f.name}: ${m.groupValues[1]}", strings.getString(m.groupValues[1]), unescape(m.groupValues[2]))
+ }
+ for (m in Regex("""Texts\.n\("([a-z0-9_]+)",\s*[^,]+,\s*$lit,\s*$lit""").findAll(text)) {
+ seen++
+ val p = plurals.optJSONObject(m.groupValues[1])
+ assertTrue("${f.name}: plural ${m.groupValues[1]} is not in app-strings.json", p != null)
+ assertEquals("${f.name}: ${m.groupValues[1]} one", p!!.getString("one"), unescape(m.groupValues[2]))
+ assertEquals("${f.name}: ${m.groupValues[1]} other", p.getString("other"), unescape(m.groupValues[3]))
+ }
+ for (block in Regex("""private val \w+_ENGLISH = mapOf\(\n((?:.*\n)*?)\s*\)""").findAll(text)) {
+ for (m in Regex(""""([a-z0-9_]+)" to $lit""").findAll(block.groupValues[1])) {
+ seen++
+ assertEquals("${f.name}: ${m.groupValues[1]}", strings.getString(m.groupValues[1]), unescape(m.groupValues[2]))
+ }
+ }
+ }
+ assertTrue("the guard found the texts ($seen)", seen > 60)
+ }
+
+ @Test fun noPureRuleKeepsAnEnglishSentenceOutOfTexts() {
+ // The rules that make Home, the Widgets and the notification say something go through Texts.t / Texts.n.
+ for (name in listOf("core/HomeText.kt", "core/Glance.kt", "core/ToolLabels.kt", "link/LinkHealth.kt")) {
+ val text = File("src/main/kotlin/com/coucou/android/$name").readText()
+ val code = text.lines().filterNot { it.trimStart().startsWith("*") || it.trimStart().startsWith("//") || it.trimStart().startsWith("/**") }
+ for (line in code) {
+ if (line.contains("Texts.") || line.contains("_ENGLISH") || line.trim().matches(Regex(""""[a-z0-9_]+" to .*"""))) continue
+ assertTrue("$name keeps an English sentence in code: $line", !Regex("""(?<![\w.])"[A-Z][a-z]+ [a-z]+[^"]*"""").containsMatchIn(line))
+ }
+ }
+ }
+
+ @Test fun theTextsSourceReadsStaticReferencesSoTheShrinkerKeepsEveryString() {
+ val lang = File("src/main/kotlin/com/coucou/android/app/AppLanguage.kt").readText()
+ assertTrue(lang.contains("TextIds.strings[key]") && lang.contains("TextIds.plurals[key]"))
+ assertTrue("no lookup by name", !lang.contains("getIdentifier"))
+ val ids = File("src/main/kotlin/com/coucou/android/app/TextIds.kt").readText()
+ for (k in strings.keys()) assertTrue("TextIds has $k", ids.contains("\"$k\" to R.string.$k,"))
+ for (k in plurals.keys()) assertTrue("TextIds has plural $k", ids.contains("\"$k\" to R.plurals.$k,"))
+ assertTrue(File("src/main/kotlin/com/coucou/android/app/CoucouApp.kt").readText().contains("Texts.source = AppLanguage.texts(this)"))
+ }
+}