locale_chain 0.1.0
locale_chain: ^0.1.0 copied to clipboard
Smart locale fallback chains for Flutter. Your pt-BR users deserve pt-PT, not English.
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:locale_chain/locale_chain.dart';
import 'package:locale_chain/easy_localization.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
LocaleChain.configure();
runApp(
EasyLocalization(
supportedLocales: const [
Locale('en'),
Locale('pt'),
Locale('pt', 'PT'),
Locale('pt', 'BR'),
],
path: 'assets/translations',
assetLoader: const LocaleChainAssetLoader(
baseLoader: RootBundleAssetLoader(),
),
fallbackLocale: const Locale('en'),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('home.title'.tr())),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('greeting'.tr(),
style: Theme.of(context).textTheme.headlineMedium),
const SizedBox(height: 8),
Text('farewell'.tr(),
style: Theme.of(context).textTheme.bodyLarge),
const SizedBox(height: 8),
Text('welcome'.tr(),
style: Theme.of(context).textTheme.bodyLarge),
const SizedBox(height: 8),
Text('home.subtitle'.tr(),
style: Theme.of(context).textTheme.bodyMedium),
const SizedBox(height: 24),
Text(
'Current: ${context.locale}',
style: Theme.of(context).textTheme.bodySmall,
),
const SizedBox(height: 16),
Wrap(
spacing: 8,
children: [
ElevatedButton(
onPressed: () => context.setLocale(const Locale('en')),
child: const Text('English'),
),
ElevatedButton(
onPressed: () =>
context.setLocale(const Locale('pt', 'BR')),
child: const Text('pt-BR'),
),
ElevatedButton(
onPressed: () =>
context.setLocale(const Locale('pt', 'PT')),
child: const Text('pt-PT'),
),
ElevatedButton(
onPressed: () => context.setLocale(const Locale('pt')),
child: const Text('pt'),
),
],
),
],
),
),
);
}
}