Created
January 14, 2023 09:46
-
-
Save XeBastian/24250bc9b724a6ca20212cbc58f637ee to your computer and use it in GitHub Desktop.
Localization in flutter and persisting data to local storage
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
class LanguageRepo { | |
// contains all the values. | |
// for readability when the translations are too much, consider moving all translations to their own files like | |
// en_US.dart and assign the en_US in the following line to the referenced value | |
static Map<String, String> en_US = { | |
"hello": "Hello", | |
"good_morning": "Good Morning", | |
"i_love_you": "I Love you", | |
}; | |
static Map<String, String> ny_NY = { | |
"hello": "Muli bwanji", | |
"good_morning": "Mwadzuka bwanji", | |
"i_love_you": "Ndimakukonda", | |
}; | |
} |
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 'package:get/get.dart'; | |
import 'package:language_repo.dart'; | |
class LocaleController extends Translations { | |
@override | |
Map<String, Map<String, String>> get keys => { | |
// to keep the code clean have transferred the values to another file | |
"en_US": LanguageRepo.en_US, | |
"ny_NY": LanguageRepo.ny_NY, | |
}; | |
} |
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 'package:flutter/material.dart'; | |
import 'package:get/get.dart'; | |
import 'package:locale_controller.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
void main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
SharedPreferences prefs = await SharedPreferences.getInstance(); | |
// check the language from the shared_prefs | |
final isChichewa = prefs.getBool('isChichewa') ?? false; | |
runApp(MyApp(isChichewa: isChichewa)); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key, required this.isChichewa}); | |
final bool isChichewa; | |
@override | |
Widget build(BuildContext context) { | |
return GetMaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
// check locale from the sharedPrefs value and display locale based on whats in shredPrefs | |
locale: isChichewa ? Locale('ny', 'NY') : Locale('en', 'US'), | |
translations: LocaleController(), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
void _registerChichewa() async { | |
// change locale | |
var locale = Locale('ny', 'NY'); | |
Get.updateLocale(locale); | |
// save to sharedprefs | |
SharedPreferences prefs = await SharedPreferences.getInstance(); | |
prefs.setBool('isChichewa', true); | |
} | |
void _registerEng() async { | |
// change locale | |
var locale = Locale('en', 'US'); | |
Get.updateLocale(locale); | |
// save to sharedprefs | |
SharedPreferences prefs = await SharedPreferences.getInstance(); | |
prefs.setBool('isChichewa', false); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
Text( | |
'hello'.tr, | |
style: TextStyle(fontSize: 25), | |
textAlign: TextAlign.center, | |
), | |
SizedBox(height: 10), | |
// use string extension .tr to make the text translatable | |
Text( | |
'good_morning'.tr, | |
style: TextStyle(fontSize: 25), | |
textAlign: TextAlign.center, | |
), | |
SizedBox(height: 10), | |
Text( | |
'i_love_you'.tr, | |
style: TextStyle(fontSize: 25), | |
textAlign: TextAlign.center, | |
), | |
MaterialButton( | |
// run the code inside _registerEng function | |
onPressed: _registerEng, | |
child: Text('Change to English'), | |
), | |
MaterialButton( | |
onPressed: _registerChichewa, | |
child: Text('Pitani ku Chichewa'), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment