-
-
Save mohSleem/a9042daa9f6c396d97a5aa49767ce7da to your computer and use it in GitHub Desktop.
Sample to implement the all_translations.dart
This file contains hidden or 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 '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