Skip to content

Instantly share code, notes, and snippets.

@boeledi
Created October 18, 2018 22:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save boeledi/43515f5e551347715c2b031834801ef3 to your computer and use it in GitHub Desktop.
Save boeledi/43515f5e551347715c2b031834801ef3 to your computer and use it in GitHub Desktop.
Sample to implement the all_translations.dart
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'all_translations.dart';
void main() async {
// Initializes the translation module
await allTranslations.init();
// then start the application
runApp( MyApplication(),);
}
class MyApplication extends StatefulWidget {
@override
_MyApplicationState createState() => _MyApplicationState();
}
class _MyApplicationState extends State<MyApplication> {
@override
void initState(){
super.initState();
// Initializes a callback should something need
// to be done when the language is changed
allTranslations.onLocaleChangedCallback = _onLocaleChanged;
}
///
/// If there is anything special to do when the user changes the language
///
_onLocaleChanged() async {
// do anything you need to do if the language changes
print('Language has been changed to: ${allTranslations.currentLanguage}');
}
///
/// Main initialization
///
@override
Widget build(BuildContext context){
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
// Tells the system which are the supported languages
supportedLocales: allTranslations.supportedLocales(),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
final String language = allTranslations.currentLanguage;
final String buttonText = language == 'fr' ? '=> English' : '=> Français';
return Scaffold(
appBar: AppBar(title: Text(allTranslations.text('main_title'))),
body: Container(
width: double.infinity,
child: Column(
children: <Widget>[
RaisedButton(
child: Text(buttonText),
onPressed: () async {
await allTranslations.setNewLanguage(language == 'fr' ? 'en' : 'fr');
setState((){});
},
),
Text(allTranslations.text('main_body')),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment