i18nu 0.0.1+1 i18nu: ^0.0.1+1 copied to clipboard
Simple internationalization intermediary classes and delegates
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:i18nu/i18nu.dart';
import 'localization_delegate.dart';
import 'localizations.dart';
void main() => runApp(I18n(child: MyApp()));
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
const supported = [
Locale('en', 'US'),
Locale('pt', 'BR'),
// TODO: add multi plural language(s)
];
final i18n = I18n.of(context);
return MaterialApp(
title: 'I18nu Demo',
theme: ThemeData(),
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
const MyL10nDelegate(
supported: supported,
dataSource: AssetBundleL10nDataSource(bundlePath: 'l10n/'),
),
],
home: const MyHomePage(supportedLocales: supported),
locale: i18n.locale,
supportedLocales: supported,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key key,
@required this.supportedLocales,
}) : super(key: key);
final List<Locale> supportedLocales;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String _gender = '';
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final homepageL10n = HomePageL10n.of(context);
final counterL10n = CounterL10n.of(context);
return Scaffold(
appBar: AppBar(title: Text(homepageL10n.title)),
body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
CupertinoSegmentedControl<Locale>(
children: Map.fromIterable(
widget.supportedLocales,
key: (dynamic loc) => loc,
value: (dynamic locale) => Text(locale.toString()),
),
groupValue: Localizations.localeOf(context),
onValueChanged: (locale) => updateLocale(context, locale),
),
const Divider(height: 32),
Text(
homepageL10n.hello(name: 'Name', world: 'Flutter'),
style: theme.textTheme.title,
),
Text(
homepageL10n.today(DateTime.now()),
style: theme.textTheme.subtitle,
),
CupertinoSegmentedControl<String>(
padding: const EdgeInsets.symmetric(vertical: 8),
children: const {
'male': Text('MALE'),
'female': Text('FEMALE'),
'': Text('OTHER'),
},
groupValue: _gender,
onValueChanged: updateGender,
),
Text(homepageL10n.gendered(_gender)),
const Divider(height: 32),
Text(
counterL10n.clicked(_counter),
style: theme.textTheme.display1,
),
FlatButton(
child: Text(counterL10n.resetCounter),
onPressed: resetCounter,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: incrementCounter,
tooltip: counterL10n.clickMe,
child: Icon(Icons.add),
),
);
}
void incrementCounter() => setState(() => _counter++);
void resetCounter() => setState(() => _counter = 0);
void updateGender(String gender) => setState(() => _gender = gender);
void updateLocale(BuildContext context, Locale locale) =>
I18n.of(context).locale = locale;
}