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.
example/lib/main.dart
import 'dart:async';
import 'package:airglot_l10n/airglot_l10n.dart';
import 'package:flutter/material.dart';
import 'l10n/airglot_localizations.g.dart';
import 'l10n/app_localizations.dart';
/// The local fake CDN from tool/fake_cdn.dart. Its key is the package tests'
/// signing key, so never trust it outside this example.
const _cdn = 'http://localhost:8787/i18n/airglot-example';
const _internalToken =
'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
final _messenger = GlobalKey<ScaffoldMessengerState>();
/// Set by onLocaleUnavailable during load(), shown once the app is up.
(Locale, Locale)? _unavailable;
final airglot = AirglotL10n(
generated: airglotGenerated,
config: AirglotConfig(
pointerUrl: Uri.parse('$_cdn/public.json'),
defaultChannelUrl: Uri.parse('$_cdn/ch/$_internalToken.json'),
publicKeys: const {
'2026-10': 'iojj3XQJ8ZX9UtstPLpdcspnCb8dlBIb83SIAbQPb1w=',
},
localeNames: const {
'en': AirglotLocaleName('English', 'English'),
'es': AirglotLocaleName('Español', 'Spanish'),
'fr': AirglotLocaleName('Français', 'French'),
},
onReport: (report) => debugPrint('Airglot: $report'),
onLocaleUnavailable: (previous, now) => _unavailable = (previous, now),
),
);
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await airglot.load();
runApp(const ExampleApp());
WidgetsBinding.instance.addPostFrameCallback((_) {
unawaited(airglot.refresh());
});
}
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) => ListenableBuilder(
listenable: airglot,
builder: (context, _) => MaterialApp(
scaffoldMessengerKey: _messenger,
locale: airglot.activeLocale,
supportedLocales: airglot.supportedLocales,
localizationsDelegates: airglotLocalizationsDelegates(airglot),
home: const PickerScreen(),
),
);
}
class PickerScreen extends StatefulWidget {
const PickerScreen({super.key});
@override
State<PickerScreen> createState() => _PickerScreenState();
}
class _PickerScreenState extends State<PickerScreen> {
String? _status;
Locale? _pending;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _tellUnavailable());
}
void _tellUnavailable() {
final unavailable = _unavailable;
if (unavailable == null) return;
_unavailable = null;
final (previous, now) = unavailable;
debugPrint('onLocaleUnavailable($previous, $now)');
final l10n = airglotLookupAppLocalizations(airglot, now);
_messenger.currentState?.showSnackBar(
SnackBar(
duration: const Duration(days: 1),
content: Text(
l10n.languageUnavailable(
previous.toLanguageTag(),
now.toLanguageTag(),
),
),
action: SnackBarAction(label: 'OK', onPressed: () {}),
),
);
}
Future<void> _select(Locale locale) async {
setState(() => _pending = locale);
final result = await airglot.selectLocale(locale);
if (!mounted) return;
setState(() {
_pending = null;
_status = 'selectLocale(${locale.toLanguageTag()}): ${result.name}';
});
}
Future<void> _followDevice() async {
await airglot.followDevice();
if (mounted) setState(() => _status = 'followDevice()');
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final selected = airglot.selectedLocale;
return Scaffold(
appBar: AppBar(title: Text(l10n.appTitle)),
body: ListView(
children: [
ListTile(
title: Text(l10n.greeting('Ada')),
subtitle: Text(
'${l10n.activeLanguage(airglot.activeLocale.toLanguageTag())}'
' · saved: ${selected?.toLanguageTag() ?? 'device'}',
),
),
if (_status != null) ListTile(dense: true, title: Text(_status!)),
const Divider(),
for (final info in airglot.availableLocales)
ListTile(
title: Text(info.nativeName),
subtitle: Text(
info.ready
? info.englishName
: '${info.englishName} · download',
),
selected: info.locale == selected,
trailing: _pending == info.locale
? const SizedBox.square(
dimension: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: info.locale == selected
? const Icon(Icons.check)
: null,
onTap: () => _select(info.locale),
),
ListTile(
leading: const Icon(Icons.phone_iphone),
title: Text(l10n.followDevice),
selected: selected == null,
onTap: _followDevice,
),
const Divider(),
Padding(
padding: const EdgeInsets.all(16),
child: AirglotReleaseBadge(
airglot: airglot,
textAlign: TextAlign.center,
onRefresh: (result) {
if (!mounted) return;
_messenger.currentState?.showSnackBar(
SnackBar(content: Text('refresh: ${result.name}')),
);
},
),
),
],
),
);
}
}