airglot_l10n 1.0.0
airglot_l10n: ^1.0.0 copied to clipboard
Over-the-air translations for Flutter apps that use gen-l10n: fetches signed Airglot releases, caches them and overlays them on the compiled strings.
airglot_l10n #
Over-the-air translations for Flutter apps that use flutter gen-l10n. It
fetches the app's signed release from the Airglot CDN, verifies and caches it,
and overlays it on the compiled strings. Existing
AppLocalizations.of(context) call sites do not change.
The overlay class and delegate are generated per app by
airglot_l10n_gen.
Set up #
final airglot = AirglotL10n(
generated: airglotGenerated, // from airglot_localizations.g.dart
config: AirglotConfig(
pointerUrl: Uri.parse('https://cdn.airglot.com/i18n/<app>/public.json'),
// Debug and internal builds start on the internal channel.
defaultChannelUrl: kDebugMode ? Uri.parse('<internal channel URL>') : null,
publicKeys: {'<kid>': '<base64 Ed25519 public key>'},
onReport: (report) => log('$report'),
),
);
await airglot.load(); // disk only, safe before runApp
runApp(App(airglot: airglot));
// after the first frame
unawaited(airglot.refresh());
ListenableBuilder(
listenable: airglot,
builder: (context, _) => MaterialApp(
locale: airglot.activeLocale,
supportedLocales: airglot.supportedLocales,
localizationsDelegates: airglotLocalizationsDelegates(airglot),
),
)
airglotLocalizationsDelegates replaces AppLocalizations.localizationsDelegates.
Flutter uses the first delegate it finds for a type, so an app that still lists
AppLocalizations.delegate ahead of the overlay gets no remote strings. Debug
builds report that as an error.
First launch #
On a fresh install nothing is cached, so a device whose language arrives only
over the air would draw its first screen in the source language and switch a
moment later. To avoid that, wait for the first release before runApp:
await airglot.load();
await airglot.awaitFirstRelease(); // at most 1.5 s, first launch only
runApp(App(airglot: airglot));
unawaited(airglot.refresh());
-
Until the first frame the platform keeps its launch screen up, so the app needs no spinner.
-
It returns at once when a release is already cached, so an install waits at most once, including when the release turns out not to have the language. It also returns at once when the most wanted language is compiled.
-
Otherwise it refreshes and downloads the wanted language's strings. It returns
appliedwhen they are ready,failedas soon as the fetch fails (for example offline), ortimedOutwhentimeout(1.5 seconds by default) passes first. After a timeout the refresh carries on and the release applies when it lands, as it would without the wait. -
On
applied, an app that follows the device's languages starts in the new language:activeLocaleis picked again before the first frame. -
wanteddefaults to the device's preferred locales. An app that chooses its own language passes its candidates, most wanted first, then callsuseLocale, which then needs no download:await airglot.awaitFirstRelease(wanted: [?pickedLocale, ...deviceLocales]); await airglot.useLocale(appLocale); -
Each wait is reported through
onReportasfirstReleaseWait, at info level, with its outcome and how long it took.
Language picker #
The catalog can keep the user's language choice itself. availableLocales
lists the languages to offer: the compiled ones plus every language in the
applied release, so a language shipped after the app was built appears after
the next refresh, with no app release.
for (final info in airglot.availableLocales)
ListTile(
title: Text(info.nativeName),
subtitle: Text(info.englishName),
trailing: info.ready ? null : const Icon(Icons.download),
selected: info.locale == airglot.selectedLocale,
onTap: () => airglot.selectLocale(info.locale),
),
ListTile(
title: const Text('Device language'),
selected: airglot.selectedLocale == null,
onTap: airglot.followDevice,
),
selectLocale(locale, timeout: ...)downloads the language's strings when they are not cached, applies it with one notification, and saves the choice.load()restores it on later launches. It returnsappliedwhen the strings were ready,appliedFromFallbackwhen they did not arrive within the timeout (5 seconds by default) or could not be fetched, andunavailable, changing nothing, for a locale no supported locale matches.- After
appliedFromFallbackthe language is active over compiled and fallback layers. Listeners are notified when its strings land: when the download finishes, or with the next refresh if it failed. followDevice()clears the choice, so the device's languages decide again.- At
load(),activeLocaleis the saved choice if it is still offered, else the first of the device's preferred languages that matches a supported locale (exactly, then by language and script, then by language alone, so apt-PTdevice getspt), else the source locale. A device language the app does not offer is never used. - A refresh never changes
activeLocalewhile the app runs. A language that ships for the device's language is used from the next launch, or from the first frame whenawaitFirstReleaseapplied it. - A saved choice that a release retires stays in use until the app restarts.
The next
load()falls back by the rules above, clears the choice and callsAirglotConfig.onLocaleUnavailable(previous, now)once, so the app can tell the user. It runs duringload(), before any UI, so keep the locales and show the notice once the app is up. readyis true when selecting needs no download. Show progress whileselectLocaleis pending.- The release names the languages it ships. Name the others, such as the
source locale, with
AirglotConfig.localeNames; a language named by neither shows its tag. - Each entry's
flagEmojiis the flag the release chose for that language in Airglot, elseAirglotLocaleName.flagRegionfromlocaleNames, else null. Show a neutral icon for null: some languages have no single country.
Apps that choose their own language #
By default activeLocale is the best match for the device's languages, and a
refresh downloads only that language. An app that keeps the language choice
itself, for example in a user profile, tells the catalog with useLocale:
await airglot.load();
await airglot.useLocale(appLocale); // before the first refresh
runApp(App(airglot: airglot));
// When the user picks another language:
await airglot.useLocale(picked);
// Back to the device's languages:
await airglot.useLocale(null);
- The locale becomes
activeLocale, and every later refresh downloads its strings with the release, so a new release never lands without them. - When the applied release has strings for the locale that are not cached,
useLocaledownloads them first, then applies the locale and the strings together with one notification. DriveMaterialApp.localefromairglot.activeLocale, or awaituseLocalebefore changing your own locale, and the screen never shows compiled strings in between. - It returns false, changing nothing, when no supported locale matches. It also returns false when a download failed: the locale is applied over the layers available, and the next refresh fetches the rest.
- The catalog does not save the choice. Call
useLocaleagain after everyload(), or useselectLocale, which does save it.
The AirglotL10n example in spec §9.6 leaves out the required generated:
argument; it is the airglotGenerated constant from the generated overlay, as
above.
Testers: internal channel via --dart-define #
The internal channel's URL contains its token, so keep it out of the source. Read it from a compile-time define and use it only for the builds testers install:
const internalChannel = String.fromEnvironment('AIRGLOT_INTERNAL_CHANNEL');
AirglotConfig(
pointerUrl: Uri.parse('https://cdn.airglot.com/i18n/<app>/public.json'),
defaultChannelUrl: internalChannel.isEmpty
? null
: Uri.parse(
'https://cdn.airglot.com/i18n/<app>/ch/$internalChannel.json',
),
publicKeys: {'<kid>': '<base64 Ed25519 public key>'},
)
flutter run --dart-define=AIRGLOT_INTERNAL_CHANNEL=<token>
flutter build ipa --dart-define=AIRGLOT_INTERNAL_CHANNEL=<token>
Builds without the define use public. Add AirglotReleaseBadge so testers can
see which release and channel they are on.
Behaviour #
load()reads the cached pointer, verifies its Ed25519 signature and each bundle's sha256, and never touches the network.refresh()runs at most once per cold start and per resume, never more often than every 15 minutes on the same channel, with a 5 second timeout and no retries. A build whose default channel changed refreshes the new channel at once. A failed refresh keeps the current release and is reported.- A remote string is used only when its source hash equals the one compiled into the app and it evaluates with the arguments given; otherwise the next layer answers, ending in the compiled source locale.
joinTestGroup(link)andleaveTestGroup()switch between a test group's channel and the build default.AirglotReleaseBadgeshows the release on any channel other thanpublic; passtextAlignto centre it.refresh(force: true)skips the once-per-session and 15-minute limits, so a tester sees a release just published tointernalor a test group. A long press onAirglotReleaseBadgedoes the same, and itsonRefreshcallback receives the result. Release builds onpublicignoreforce.- Every report has a
level.updatedis the oneinforeport: a refresh applied a release, with its version, stage and channel indetail, for examplev44 · testing · internal. Every other kind is awarning, so filter onlevelbefore sending reports to crash reporting. - Reports name channels as
/ch/…: a channel's path token is a test group's enrolment capability, so reports are safe to send to logs and crash reporting. - Debug builds report
flutterVersionChangedwhen the app is built with a different Flutter version from the one the overlay was generated against. - Network access goes through
AirglotClient; pass your own to pin certificates or route requests.