Без опису

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import 'package:flutter/material.dart';
  2. import 'package:userstory2translate/localization/localization.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4. const String LAGUAGE_CODE = 'languageCode';
  5. //languages code
  6. const String ENGLISH = 'en';
  7. const String FARSI = 'fa';
  8. const String SPANISH = 'es';
  9. const String HINDI = 'hi';
  10. Future<Locale> setLocale(String languageCode) async {
  11. SharedPreferences _prefs = await SharedPreferences.getInstance();
  12. await _prefs.setString(LAGUAGE_CODE, languageCode);
  13. return _locale(languageCode);
  14. }
  15. Future<Locale> getLocale() async {
  16. SharedPreferences _prefs = await SharedPreferences.getInstance();
  17. String languageCode = _prefs.getString(LAGUAGE_CODE) ?? "en";
  18. return _locale(languageCode);
  19. }
  20. Locale _locale(String languageCode) {
  21. switch (languageCode) {
  22. case ENGLISH:
  23. return Locale(ENGLISH, 'US');
  24. case FARSI:
  25. return Locale(FARSI, "IR");
  26. case SPANISH:
  27. return Locale(SPANISH, "SP");
  28. case HINDI:
  29. return Locale(HINDI, "IN");
  30. default:
  31. return Locale(ENGLISH, 'US');
  32. }
  33. }
  34. String getTranslated(BuildContext context, String key) {
  35. return DemoLocalization.of(context).translate(key);
  36. }