-
-
Save ethanliew/71bda80ee2d0e1fba9dde4677e95558d to your computer and use it in GitHub Desktop.
Example of overriding default locale for use with intl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:intl/intl.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_localizations/flutter_localizations.dart'; | |
import 'package:intl_translate_example/l10n/messages_all.dart'; | |
void main() => runApp(new MyApp()); | |
class Translations { | |
static Future<Translations> load(Locale locale) { | |
final String name = | |
locale.countryCode.isEmpty ? locale.languageCode : locale.toString(); | |
final String localeName = Intl.canonicalizedLocale(name); | |
return initializeMessages(localeName).then((bool _) { | |
Intl.defaultLocale = localeName; | |
return new Translations(); | |
}); | |
} | |
static Translations of(BuildContext context) { | |
return Localizations.of<Translations>(context, Translations); | |
} | |
String get title2 { | |
return Intl.message( | |
'Hello World', | |
name: 'title3', | |
desc: 'Title for the Demo application', | |
); | |
} | |
} | |
class TranslationsDelegate extends LocalizationsDelegate<Translations> { | |
const TranslationsDelegate(); | |
@override | |
bool isSupported(Locale locale) => ['en', 'fr'].contains(locale.languageCode); | |
@override | |
Future<Translations> load(Locale locale) => Translations.load(locale); | |
@override | |
bool shouldReload(TranslationsDelegate old) => false; | |
} | |
class SpecifiedLocalizationDelegate | |
extends LocalizationsDelegate<Translations> { | |
final Locale overriddenLocale; | |
const SpecifiedLocalizationDelegate(this.overriddenLocale); | |
@override | |
bool isSupported(Locale locale) => overriddenLocale != null; | |
@override | |
Future<Translations> load(Locale locale) => | |
Translations.load(overriddenLocale); | |
@override | |
bool shouldReload(SpecifiedLocalizationDelegate old) => true; | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => new _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
SpecifiedLocalizationDelegate _localeOverrideDelegate; | |
@override | |
void initState() { | |
super.initState(); | |
_localeOverrideDelegate = new SpecifiedLocalizationDelegate(null); | |
} | |
onLocaleChange(Locale l) { | |
setState(() { | |
_localeOverrideDelegate = new SpecifiedLocalizationDelegate(l); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
theme: new ThemeData(primarySwatch: Colors.blue), | |
localizationsDelegates: [ | |
_localeOverrideDelegate, | |
const TranslationsDelegate(), | |
GlobalMaterialLocalizations.delegate, | |
GlobalWidgetsLocalizations.delegate, | |
], | |
supportedLocales: [ | |
const Locale('en', ''), // English | |
const Locale('fr', ''), // French | |
], | |
home: new MyHomePage(onLocaleChange: onLocaleChange), | |
); | |
} | |
} | |
typedef void LocaleChangeCallback(Locale locale); | |
class MyHomePage extends StatefulWidget { | |
final LocaleChangeCallback onLocaleChange; | |
MyHomePage({Key key, this.onLocaleChange}) : super(key: key); | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text(Translations.of(context).title2), | |
), | |
body: new Center( | |
child: new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
new RaisedButton( | |
onPressed: () => widget.onLocaleChange(const Locale("en", "")), | |
child: new Text("EN"), | |
), | |
new RaisedButton( | |
onPressed: () => widget.onLocaleChange(const Locale("fr", "")), | |
child: new Text("FR"), | |
), | |
new RaisedButton( | |
onPressed: () => widget.onLocaleChange(null), | |
child: new Text("Default"), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment