Skip to content

Instantly share code, notes, and snippets.

@ilikerobots
Last active December 19, 2019 13:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ilikerobots/474b414138f3f99150dbb3d0cc4cc721 to your computer and use it in GitHub Desktop.
Save ilikerobots/474b414138f3f99150dbb3d0cc4cc721 to your computer and use it in GitHub Desktop.
Example of overriding default locale for use with intl
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"),
),
],
),
),
);
}
}
@AdeSupriyadi
Copy link

AdeSupriyadi commented Nov 17, 2018

where is messages_all.dart?

and I have error initializeMessages isn't defined for the class Trahslations

@GuillermoLM
Copy link

Not working

@maheshmnj
Copy link

where is messages_all.dart?

and I have error initializeMessages isn't defined for the class Trahslations

it will be generated automatically see documentation https://flutter.dev/docs/development/accessibility-and-localization/internationalization
and run I will try to publish some article on How to do it with a efficient way to change locale in the run time

//flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/l10n --no-use-deferred-loading  lib/l10n/intl_en.arb lib/l10n/intl_messages.arb lib/l10n/intl_kn.arb lib/locale/locale.dart

@maheshmnj
Copy link

Not working

can you try with ChangeNotifierProvider its so simple to use

@maheshmnj
Copy link

Not working

may be my answer here can help you https://stackoverflow.com/a/59410830/8253662

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment