feat(mobile): run a device app in another language with app.locale (ENG-1252)

Draft
KD
10h ago
+1143-69

What

A device target can run its app in another language and region with app.locale, a BCP 47 tag. device.openApp takes the same option for one launch:

// e2e.config.ts
{
  name: 'ios-fr',
  engine: mobile({ platform: 'ios' }),
  app: { bundleId: 'com.example.app', locale: 'fr-FR' },
}

// a test
await device.openApp('com.example.app', { relaunch: true, locale: 'ar-EG' });
  • On iOS the locale becomes the launch's AppleLanguages and AppleLocale arguments. The device keeps its own language.
  • On Android the engine sets the app's own language (Android 13+) with this machine's adb before the launch, and puts the app's earlier language back afterwards.
  • The engine refuses a locale it can't apply. That includes Android devices from a device provider, iOS devices on testmu() and browserstack() (they drop launch arguments), and a device behind a remote agent-device daemon.

Why

ENG-1252. Before this, a test could set a language only on iOS, by writing launchArguments: { AppleLanguages: ['fr'], AppleLocale: 'fr_FR' } by hand. On a local Android device the only way was to change the device's language before the run. Detox (languageAndLocale), Maestro (--device-locale), and Appium (appium:language) each have an option for this. The migrate guides could map it only to hand-written Apple arguments on iOS, and to nothing on Android.

The locale is a property of the app under test, so it goes on the target's app, with an engine capability, rather than a flat mobile({ locale }) option (the design in ENG-1296).

How it works

  1. Core. app.locale is a new TargetApp field. At config load it must be a language tag (packages/core/src/config/app.ts:201), and the target's engine must declare the new appLocale capability (packages/core/src/engine/index.ts:856). An engine without the capability fails with INVALID_CONFIG. On a web target, the error points at web({ locale }). The capability travels through the engine manifest and protocol (engine/manifest.ts, engine/protocol/*, engine-manifest-v1.schema.json with both fixtures). The locale reaches the engine as EngineAppInfo.locale, and launch() accepts it too (packages/core/src/core/launch.ts:123).
  2. Config check in the mobile engine (packages/mobile/src/engine.ts:131). The engine declares appLocale: true. validateApp accepts a tag with a language, a script, and a region only (packages/mobile/src/locale.ts:49), because a variant or an extension would not survive the AppleLocale form. On iOS it refuses a target whose launchArguments already set AppleLanguages or AppleLocale. On Android it refuses a bundleId that isn't a package name, because adb sets the locale per package. assertLocaleReachable (locale.ts:94) refuses a device this machine's adb can't reach: an Android device from a provider, and an iOS device from a provider that drops launch arguments.
  3. Provider flag. DeviceProvider gets launchArguments?: false (packages/mobile/src/provider.ts:151). testmu() and browserstack() set it, because agent-device's WebDriver backend drops launchArgs without an error. Without the flag, the app would run in the device's own language and the test would pass in the wrong language. Expo, Revyl, and Limrun pass launch arguments through, so they don't set the flag.
  4. device.openApp (packages/mobile/src/surface.ts:924).
    • A locale needs relaunch: true. Without it the call fails with INVALID_ARGUMENT, because a running app on Android would switch language at once while iOS ignores it.
    • The locale is the option's value, or else the target's app.locale on a relaunch of the pinned app.
    • On iOS, withLocaleArguments (locale.ts:303) appends -AppleLanguages (zh-Hans-CN) -AppleLocale zh-Hans_CN to the launch.
  5. Android: AndroidAppLocales (locale.ts:141).
    • Before the launch, and before the permissions so a refusal changes nothing, it checks for API 33 or newer. The first time it changes an app, it reads that app's own locales (cmd locale get-app-locales). Then it runs cmd locale set-app-locales <package> --locales <tag>.
    • It puts the saved locales back on the app's next fresh launch without a locale, on device.closeApp() (surface.ts:1104), and at dispose (surface.ts:877). The dispose restore runs before the session closes and gets half the cleanup budget.
    • Every set, put-back, and restore runs one at a time, through serialized (locale.ts:202). Without it, overlapping launches could leave a locale set.
    • adb is found the way agent-device finds it: ANDROID_SDK_ROOT, ANDROID_HOME, the SDK's default folder, then PATH (runAdb, locale.ts:264).
    • With AGENT_DEVICE_DAEMON_BASE_URL set, the device is on another machine, so a launch with a locale fails with UNSUPPORTED_CAPABILITY.
  6. Docs. docs/mobile.mdx ("Run the app in another language") and the DeviceProvider contract; the config, core, engine, engine-protocol, and mobile reference pages; the Detox, Appium, and Maestro migrate tables; skills/e2e/references/setup.md. A changeset patch-bumps core, e2e, every engine (for the contract field), testmu, and browserstack. @e2e-dev/mobile now needs core and e2e 0.25.1 or newer.

Alternative not taken: change the whole Android device's language. That changes every app on the device, and the device for any other session sharing it. The per-app locale touches only the app under test.

The guide section is shorter than the first draft. Some details moved to the reference page's "Launch arguments and permissions", which the guide now links: the XCTest phone refusal, the remote-daemon refusal, the extension example, and the exact restore points.

Review findings not taken:

  • Write locales per Android user (--user). Nothing in e2e or agent-device switches users during a run.
  • Add app.locale to the replay cache key. The key already has the target name, and the docs use one target per language. launchArguments and permissions aren't in the key either. A stale recording misses its locators instead of answering wrong.

Issue

ENG-1252

Verified

Ran it locally: yes

A scratch project ran the built CLI against the Settings app. Android ran on a dedicated API 36 emulator (Pixel 6) and iOS on a new iPhone 17 Pro simulator (iOS 27.2) whose own language is Korean.

CheckAndroidiOS
app.locale: 'fr-FR'"Rechercher dans les paramètres""Réglages"
openApp({ relaunch: true, locale: 'ja-JP' }), then app.open()"設定を検索", then French again"設定", then "Réglages"
No app.locale; openApp with fr-FR, closeApp(), app.open()French, then "Search Settings"French, then "설정" (device language)
openApp({ locale }) without relaunchINVALID_ARGUMENTINVALID_ARGUMENT
Settings had its own de-DE before the run[de-DE] after the runn/a
before: Locales for com.android.settings for user 0 are [de-DE]
   ✓ |android| tests/locale.e2e.ts > Settings opens in the target locale 4.56s
   ✓ |android| tests/override.e2e.ts > openApp locale overrides app.locale for one launch 6.71s
   ✓ |ios| tests/locale.e2e.ts > Settings opens in the target locale 5.36s
   ✓ |ios| tests/override.e2e.ts > openApp locale overrides app.locale for one launch 11.00s
      Tests  4 passed (4)
after: Locales for com.android.settings for user 0 are [de-DE]

refused: INVALID_ARGUMENT device.openApp({ locale }) needs relaunch: true: a locale takes effect when the open launches the app fresh

The refusals and the restore races were checked with unit tests only, because they need a provider, a remote daemon, or precisely timed adb calls. These include the provider refusals, the remote daemon, Android older than 13, an adb that hangs at dispose, overlapping launches, and a launch by display name. The tests use a scripted adb, and the overlap test fails without the serialization fix (expected 'ja-JP' to be 'de-DE').

Benchmark: not measured. pnpm bench:ab runs only the web benchmark, and a launch without a locale runs no new device command (presetAndroidLocale returns at once, surface.ts:968).

The videos below are the override test (ja-JP for one launch, then back to fr-FR) on Android and on iOS.

https://github.com/user-attachments/assets/c20cd08e-e89c-4dc3-a62e-c7bf3763d004

https://github.com/user-attachments/assets/00769db9-fff4-49a5-9559-11ec7118bb40

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md). Not breaking: the field and the capability are optional.
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.

🤖 Generated with Claude Code