Created
April 1, 2020 08:28
-
-
Save podcoder/2155b0e6f2018062234bde08fff03632 to your computer and use it in GitHub Desktop.
Language constants file to easily handle localization
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:flutter_localization_master/localization/demo_localization.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
const String LAGUAGE_CODE = 'languageCode'; | |
//languages code | |
const String ENGLISH = 'en'; | |
const String FARSI = 'fa'; | |
const String ARABIC = 'ar'; | |
const String HINDI = 'hi'; | |
Future<Locale> setLocale(String languageCode) async { | |
SharedPreferences _prefs = await SharedPreferences.getInstance(); | |
await _prefs.setString(LAGUAGE_CODE, languageCode); | |
return _locale(languageCode); | |
} | |
Future<Locale> getLocale() async { | |
SharedPreferences _prefs = await SharedPreferences.getInstance(); | |
String languageCode = _prefs.getString(LAGUAGE_CODE) ?? "en"; | |
return _locale(languageCode); | |
} | |
Locale _locale(String languageCode) { | |
switch (languageCode) { | |
case ENGLISH: | |
return Locale(ENGLISH, 'US'); | |
case FARSI: | |
return Locale(FARSI, "IR"); | |
case ARABIC: | |
return Locale(ARABIC, "SA"); | |
case HINDI: | |
return Locale(HINDI, "IN"); | |
default: | |
return Locale(ENGLISH, 'US'); | |
} | |
} | |
String getTranslated(BuildContext context, String key) { | |
return DemoLocalization.of(context).translate(key); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment