Skip to main content

langswitch 0.1.0 copy "langswitch: ^0.1.0" to clipboard
langswitch: ^0.1.0 copied to clipboard

One-command per-app language switching for Flutter: Android 13+ and iOS system language settings, persistence, and a ready-made picker.

langswitch #

Per-app language switching for Flutter: Android 13+ and iOS system language settings, persistence, and a ready-made picker widget — set up in one command.

pub version license CI


Why langswitch #

  • One-command setup. dart run langswitch:setup writes the Android locales_config.xml, patches the manifest, and updates Info.plist — the files that make your app appear in the OS per-app language screen. No manual XML editing.
  • Stays in sync with OS settings. When a user changes the app's language in Android Settings or iOS Settings, langswitch detects the change on resume and updates the in-app state to match. Changes made in-app are written back to the OS setting on Android 13+.
  • Ready-made picker. showLangSwitchSheet(context) presents a bottom sheet with each supported language shown in its own name (اردو, Español, Français …), with the current selection highlighted. No design work required.

Quickstart #

Step 1 — Add the dependency:

dependencies:
  langswitch: ^0.1.0

Step 2 — Run setup (from your Flutter project root):

dart run langswitch:setup

This creates android/app/src/main/res/xml/locales_config.xml, adds android:localeConfig to your manifest, and adds CFBundleLocalizations to ios/Runner/Info.plist. See What the setup command changes for the exact diffs.

Step 3 — Wrap MaterialApp:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await LangSwitch.init(supportedLocales: AppLocalizations.supportedLocales);
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return LangSwitchBuilder(
      builder: (context, locale, supported) => MaterialApp(
        locale: locale,
        supportedLocales: supported,
        localizationsDelegates: AppLocalizations.localizationsDelegates,
        home: const HomePage(),
      ),
    );
  }
}

// Anywhere in your app:
ElevatedButton(
  onPressed: () => showLangSwitchSheet(context),
  child: const Text('Language'),
);

What the setup command changes #

Android #

Creates android/app/src/main/res/xml/locales_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- generated by langswitch -->
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
    <locale android:name="en"/>
    <locale android:name="ur"/>
    <locale android:name="ar"/>
    <locale android:name="es"/>
</locale-config>

Patches android/app/src/main/AndroidManifest.xml — adds android:localeConfig to the <application> element:

 <application
     android:label="myapp"
+    android:localeConfig="@xml/locales_config"
     ...>

iOS #

Patches ios/Runner/Info.plist — adds CFBundleLocalizations:

+<key>CFBundleLocalizations</key>
+<array>
+    <string>en</string>
+    <string>ur</string>
+    <string>ar</string>
+    <string>es</string>
+</array>

Note: Flutter's own gen_l10n handles string resources; no .lproj folders are needed.


API overview #

Symbol Kind Description
LangSwitch.init(…) static Future Initialize once before runApp(). Reads persisted and OS language.
LangSwitch.instance static LangSwitch The singleton. Throws StateError if init() was not awaited.
LangSwitch.value Locale? Current override, or null to follow the device. Pass to MaterialApp.locale.
LangSwitch.locale Locale? Alias for value.
LangSwitch.resolvedLocale Locale Actual locale after resolving against supportedLocales. Never null.
LangSwitch.supportedLocales List<Locale> The locales passed to init().
LangSwitch.hasOverride bool true when a language was explicitly chosen (not following system).
LangSwitch.setLocale(locale) Future<void> Set or clear the override. Persists, notifies, and syncs with OS.
LangSwitch.isSystemSettingSupported Future<bool> Whether the OS has a per-app language screen.
LangSwitch.openSystemLanguageSettings() Future<bool> Opens the OS per-app language screen.
LangSwitch.syncWithSystem() Future<void> Re-reads the OS language (called automatically on resume).
LangSwitchBuilder StatelessWidget Rebuilds subtree when language changes.
LanguagePicker StatelessWidget Embeddable list picker.
showLangSwitchSheet(context) Future<void> Shows the picker in a bottom sheet (dialog on wide screens).
LocaleResolver class Pure functions for resolving BCP-47 locales.
LanguageNames.nameFor(locale) static String? Native name for a locale (endonym), or null.

Platform support #

Platform In-app switching Shows in OS language screen Syncs with OS changes
Android 13+ (API 33+) ✅ ✅ ✅
Android 8–12 (API 21–32) ✅ (persisted in app) ❌ (OS has no per-app screen) n/a
iOS 13+ ✅ (persisted in app) ✅ (Settings → App → Language) ✅ (detected on resume/restart)
Web / Desktop ✅ (in-app + persisted) n/a n/a

Customization #

Custom picker UI #

Build your own picker using LangSwitch.instance directly:

ValueListenableBuilder<Locale?>(
  valueListenable: LangSwitch.instance,
  builder: (context, locale, _) {
    return DropdownButton<Locale?>(
      value: locale,
      items: [
        const DropdownMenuItem(value: null, child: Text('System default')),
        for (final l in LangSwitch.instance.supportedLocales)
          DropdownMenuItem(
            value: l,
            child: Text(LanguageNames.nameFor(l) ?? l.toLanguageTag()),
          ),
      ],
      onChanged: LangSwitch.instance.setLocale,
    );
  },
);

Custom display names #

Override specific labels by passing displayNames to LanguagePicker or showLangSwitchSheet:

showLangSwitchSheet(
  context,
  displayNames: {'pt-BR': 'Português (Brasil)', 'zh-Hans': '简体中文'},
);

Using with other localization packages #

langswitch works with any Flutter localization setup. It manages the locale value and syncs it with the OS; you wire it to MaterialApp.locale via LangSwitchBuilder.

With gen_l10n (recommended):

await LangSwitch.init(supportedLocales: AppLocalizations.supportedLocales);
// then in your MaterialApp builder:
locale: locale,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: supported,

With flutter_localizations only:

await LangSwitch.init(
  supportedLocales: const [Locale('en'), Locale('ur'), Locale('ar')],
);

langswitch does not read or write ARB files, translation memory, or any localized strings — it only manages which locale is active.


CLI reference #

dart run langswitch:setup [options]

Options:
  -l, --locales   Comma-separated BCP-47 tags (e.g. en,ur,ar,es,pt-BR).
                  Default: auto-detected from l10n.yaml and ARB files.
  -p, --project   Path to the Flutter project root. Default: current directory.
      --android-only   Skip iOS setup.
      --ios-only       Skip Android setup.
      --dry-run   Print what would change; write nothing to disk.
      --check     Exit non-zero (code 3) if setup is missing or outdated (useful in CI).
  -y, --yes       Skip confirmation prompt.
  -h, --help      Print this help.

Exit codes:
  0  Success or no-op (all files already up to date).
  1  Error (bad arguments, missing files, parse failure).
  2  Manual action needed (a file exists that langswitch should not overwrite).
  3  Setup incomplete (--check mode only).

Auto-detecting locales: The command looks for locales in this order:

  1. The --locales flag.
  2. l10n.yaml → arb-dir (default lib/l10n) → each *.arb file's @@locale value or the filename suffix (app_ur.arb → ur).
  3. If nothing is found, it exits with an error asking you to pass --locales.

Running in CI (verify setup is complete):

dart run langswitch:setup --check --locales en,ur,ar,es

FAQ / Troubleshooting #

My app doesn't appear in the Android "App languages" list. Your app must target API 33+ (compileSdkVersion 33 or higher). Run dart run langswitch:setup --check to verify that locales_config.xml is present and the manifest attribute is set. Also confirm that minSdkVersion is at least 21 and that you are testing on Android 13+.

iOS Settings shows no "Language" row for my app. CFBundleLocalizations is missing from Info.plist. Run dart run langswitch:setup (or check that the iOS step completed without an error) and hot restart.

RTL languages (Urdu, Arabic) are not flipping the layout. Ensure the locale is correctly set in MaterialApp.locale and that Flutter's GlobalMaterialLocalizations and GlobalWidgetsLocalizations delegates are included in localizationsDelegates. Check resolvedLocale in LangSwitch.instance to confirm the right locale is active.

Changes don't take effect until I hot restart. You need a full restart, not a hot reload, after changing MaterialApp.locale for platform-level changes to propagate. LangSwitch already handles this for in-app changes.

LangSwitch.instance throws StateError. Call and await LangSwitch.init() in main() before runApp().


Limitations #

  • iOS may restart the app when the user changes the app's language from iOS Settings (this is iOS behavior, not specific to langswitch). The override is persisted, so the correct locale is restored on the next launch.
  • Android below API 33 has no per-app language screen in the OS. In-app switching and persistence work, but the user cannot change the language from the OS Settings.
  • Desktop and web have no native per-app language setting concept. In-app switching and SharedPreferences persistence work correctly on those platforms.
  • gen_l10n auto-detection requires either a l10n.yaml file or ARB files in lib/l10n. Monorepo or unusual project structures may need the --locales flag.

Contributing #

See CONTRIBUTING.md for how to run tests, the code style, and the PR checklist.

License #

MIT — 2026 langswitch contributors.

1
likes
150
points
73
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

One-command per-app language switching for Flutter: Android 13+ and iOS system language settings, persistence, and a ready-made picker.

Repository (GitHub)
View/report issues
Contributing

Topics

#localization #i18n #language #locale

License

MIT (license)

Dependencies

args, flutter, path, plugin_platform_interface, shared_preferences, xml, yaml

More

Packages that depend on langswitch

Packages that implement langswitch