80 lines
2.8 KiB
Dart
80 lines
2.8 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() {
|
|||
|
|
AppLog.log(
|
|||
|
|
'11111StoreService.to.getLanguageCode():${StoreService.to.getLanguageCode()}');
|
|||
|
|
final Locale locale = StoreService.to.getLanguageCode()!.isNotEmpty
|
|||
|
|
? appDept.deptSupportedLocales
|
|||
|
|
.where((Locale element) =>
|
|||
|
|
element.languageCode.toString() ==
|
|||
|
|
getCurrentLocaleWithLanguageCode(
|
|||
|
|
StoreService.to.getLanguageCode()!)
|
|||
|
|
.languageCode)
|
|||
|
|
.first
|
|||
|
|
: Get.deviceLocale!; // Get.deviceLocale;
|
|||
|
|
final String languageCode = convertLocale(locale).toString();
|
|||
|
|
AppLog.log(
|
|||
|
|
'11111locale.toString(): ${locale.toString()} locale: $locale languageCode:$languageCode');
|
|||
|
|
return languageCode;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 获取当前储存的Locale,没有的话获取系统的
|
|||
|
|
static Locale getCurrentLocale() {
|
|||
|
|
AppLog.log(
|
|||
|
|
'222StoreService.to.getLanguageCode():${StoreService.to.getLanguageCode()}');
|
|||
|
|
final Locale locale = StoreService.to.getLanguageCode()!.isNotEmpty
|
|||
|
|
? appDept.deptSupportedLocales
|
|||
|
|
.where((Locale element) =>
|
|||
|
|
element.languageCode.toString() ==
|
|||
|
|
getCurrentLocaleWithLanguageCode(
|
|||
|
|
StoreService.to.getLanguageCode()!)
|
|||
|
|
.languageCode)
|
|||
|
|
.first
|
|||
|
|
: Get.deviceLocale!; // Get.deviceLocale;
|
|||
|
|
final Locale getLocale = convertLocale(locale);
|
|||
|
|
AppLog.log(
|
|||
|
|
'222locale.toString(): ${locale.toString()} locale: $locale getLocale:$getLocale');
|
|||
|
|
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 const Locale('en', 'US');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 国际化中文繁体及中文的转化
|
|||
|
|
static Locale convertLocale(Locale locale) {
|
|||
|
|
if (locale.languageCode == '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');
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 默认返回 Locale 的完整格式
|
|||
|
|
return locale;
|
|||
|
|
}
|
|||
|
|
}
|