141 lines
4.5 KiB
Dart
141 lines
4.5 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:get/get.dart';
|
||
|
||
import '../app_settings/app_settings.dart';
|
||
import '../tools/store_service.dart';
|
||
import 'app_dept.dart';
|
||
|
||
class CurrentLocaleTool {
|
||
/// 获取当前语言的Locale字符串,没有的话获取系统的
|
||
static String getCurrentLocaleString() {
|
||
final String languageCode = StoreService.to.getLanguageCode()!.isNotEmpty
|
||
? appDept.deptSupportedLocales
|
||
.where((Locale element) =>
|
||
element.toString() == StoreService.to.getLanguageCode()!)
|
||
.first
|
||
.toString()
|
||
: convertLocale(Get.deviceLocale!).toString(); // Get.deviceLocale;
|
||
// final String languageCode = convertLocale(locale).toString();
|
||
// AppLog.log('11111locale.toString(): ${locale.toString()} locale: $locale languageCode:$languageCode 从本地获取code:${StoreService.to.getLanguageCode()}');
|
||
return languageCode;
|
||
}
|
||
|
||
/// 获取当前储存的Locale,没有的话获取系统的
|
||
static Locale getCurrentLocale() {
|
||
final Locale locale = StoreService.to.getLanguageCode()!.isNotEmpty
|
||
? appDept.deptSupportedLocales
|
||
.where((Locale element) =>
|
||
element.toString() == StoreService.to.getLanguageCode()!)
|
||
.first
|
||
: Get.deviceLocale!; // Get.deviceLocale;
|
||
final Locale getLocale = convertLocale(locale);
|
||
// AppLog.log('222locale.toString(): ${locale.toString()} locale: $locale getLocale:$getLocale 从本地获取code:${StoreService.to.getLanguageCode()}');
|
||
return getLocale;
|
||
}
|
||
|
||
static Locale getCurrentLocaleWithLanguageCode(String languageCode) {
|
||
final List<String> parts = languageCode.split('_');
|
||
final String language = parts[0];
|
||
final String country = parts.length > 1 ? parts[1] : '';
|
||
|
||
for (Locale locale in appDept.deptSupportedLocales) {
|
||
if (locale.languageCode == language && locale.countryCode == country) {
|
||
return locale;
|
||
}
|
||
}
|
||
|
||
return Get.deviceLocale!;
|
||
}
|
||
|
||
/// 国际化中文繁体及中文的转化
|
||
static Locale convertLocale(Locale locale) {
|
||
switch (locale.languageCode) {
|
||
case 'zh':
|
||
if (locale.scriptCode == 'Hans') {
|
||
// 简体中文
|
||
return const Locale('zh', 'CN');
|
||
} else if (locale.scriptCode == 'Hant') {
|
||
// 繁体中文
|
||
if (locale.countryCode == 'CN') {
|
||
return const Locale('zh', 'TW');
|
||
} else if (locale.countryCode == 'HK') {
|
||
return const Locale('zh', 'HK');
|
||
}
|
||
}
|
||
break;
|
||
case 'en':
|
||
return const Locale('en', 'US');
|
||
case 'fr':
|
||
return const Locale('fr', 'FR');
|
||
case 'ru':
|
||
return const Locale('ru', 'RU');
|
||
case 'de':
|
||
return const Locale('de', 'DE');
|
||
case 'ja':
|
||
return const Locale('ja', 'JP');
|
||
case 'ko':
|
||
return const Locale('ko', 'KR');
|
||
case 'it':
|
||
return const Locale('it', 'IT');
|
||
case 'uk':
|
||
return const Locale('uk', 'UA');
|
||
case 'pt':
|
||
return const Locale('pt', 'PT');
|
||
case 'es':
|
||
return const Locale('es', 'ES');
|
||
case 'ar':
|
||
return const Locale('ar', 'SA');
|
||
case 'vi':
|
||
return const Locale('vi', 'VN');
|
||
case 'ms':
|
||
return const Locale('ms', 'MY');
|
||
case 'nl':
|
||
return const Locale('nl', 'NL');
|
||
case 'ro':
|
||
return const Locale('ro', 'RO');
|
||
case 'lt':
|
||
return const Locale('lt', 'LT');
|
||
case 'sv':
|
||
return const Locale('sv', 'SE');
|
||
case 'et':
|
||
return const Locale('et', 'EE');
|
||
case 'pl':
|
||
return const Locale('pl', 'PL');
|
||
case 'sk':
|
||
return const Locale('sk', 'SK');
|
||
case 'cs':
|
||
return const Locale('cs', 'CZ');
|
||
case 'el':
|
||
return const Locale('el', 'GR');
|
||
case 'he':
|
||
return const Locale('he', 'IL');
|
||
case 'sr':
|
||
return const Locale('sr', 'RS');
|
||
case 'tr':
|
||
return const Locale('tr', 'TR');
|
||
case 'hu':
|
||
return const Locale('hu', 'HU');
|
||
case 'bg':
|
||
return const Locale('bg', 'BG');
|
||
case 'kk':
|
||
return const Locale('kk', 'KZ');
|
||
case 'bn':
|
||
return const Locale('bn', 'BD');
|
||
case 'hr':
|
||
return const Locale('hr', 'HR');
|
||
case 'th':
|
||
return const Locale('th', 'TH');
|
||
case 'id':
|
||
return const Locale('id', 'ID');
|
||
case 'fi':
|
||
return const Locale('fi', 'FI');
|
||
case 'da':
|
||
return const Locale('da', 'DK');
|
||
default:
|
||
return locale;
|
||
}
|
||
// 默认返回 Locale 的完整格式
|
||
return locale;
|
||
}
|
||
}
|