language_list.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import 'package:flutter/material.dart';
  2. import '../../services/index.dart';
  3. enum LanguageList {
  4. hans, // 简体
  5. en, // 英语
  6. hant // 繁体
  7. }
  8. extension LanguageListExt on LanguageList {
  9. static LanguageList getCurrentLanguage() {
  10. String key = SharedPreferenceService.to.getString(SharedPreferenceService.languageSaveKey, defaultValue: LanguageList.hans.getKey());
  11. for (var element in LanguageList.values) {
  12. if (element.getKey() == key) {
  13. return element;
  14. }
  15. }
  16. return LanguageList.hans;
  17. }
  18. /// pc 上设置的语言环境
  19. static LanguageList getFromPcKey(String keyfromPc) {
  20. switch(keyfromPc) {
  21. case 'zh-CN':
  22. return LanguageList.hans;
  23. case 'en':
  24. return LanguageList.en;
  25. default:
  26. return getCurrentLanguage();
  27. }
  28. }
  29. String getDisplayName() {
  30. switch(this) {
  31. case LanguageList.hans:
  32. return '简体';
  33. case LanguageList.en:
  34. return 'English';
  35. case LanguageList.hant:
  36. return '繁體';
  37. }
  38. }
  39. String getKey() {
  40. switch(this) {
  41. case LanguageList.hans:
  42. return 'hans';
  43. case LanguageList.en:
  44. return 'en';
  45. case LanguageList.hant:
  46. return 'hant';
  47. }
  48. }
  49. Locale getLocale() {
  50. switch(this) {
  51. case LanguageList.hans:
  52. return const Locale('zh', 'CN');
  53. case LanguageList.en:
  54. return const Locale('en', 'US');
  55. case LanguageList.hant:
  56. return const Locale('zh', 'HK');
  57. }
  58. }
  59. }