123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import 'package:flutter/material.dart';
- import '../localization/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 SPANISH = 'es';
- 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 SPANISH:
- return Locale(SPANISH, "SP");
- case HINDI:
- return Locale(HINDI, "IN");
- default:
- return Locale(ENGLISH, 'US');
- }
- }
-
- String getTranslated(BuildContext context, String key) {
- return DemoLocalization.of(context).translate(key);
- }
|