264 lines
14 KiB
Dart
264 lines
14 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||
|
||
/// 应用本地化支持类
|
||
/// 管理多语言翻译和区域设置
|
||
class AppLocalizations {
|
||
final Locale locale;
|
||
|
||
AppLocalizations(this.locale);
|
||
|
||
/// 获取本地化实例
|
||
static AppLocalizations? of(BuildContext context) {
|
||
return Localizations.of<AppLocalizations>(context, AppLocalizations);
|
||
}
|
||
|
||
/// 本地化委托
|
||
static const AppLocalizationsDelegate delegate = AppLocalizationsDelegate();
|
||
|
||
/// 支持的语言列表
|
||
static const supportedLocales = [
|
||
Locale('zh', 'CN'), // 简体中文
|
||
Locale('zh', 'TW'), // 繁体中文
|
||
Locale('en', 'US'), // English
|
||
];
|
||
|
||
/// 本地化代理列表
|
||
static const localizationsDelegates = [
|
||
AppLocalizationsDelegate(),
|
||
GlobalMaterialLocalizations.delegate,
|
||
GlobalWidgetsLocalizations.delegate,
|
||
GlobalCupertinoLocalizations.delegate,
|
||
];
|
||
|
||
// 基础本地化字符串
|
||
String get appName {
|
||
switch (locale.languageCode) {
|
||
case 'zh':
|
||
return locale.countryCode == 'TW' ? '想拍' : '想拍';
|
||
case 'en':
|
||
default:
|
||
return 'InspoSnap';
|
||
}
|
||
}
|
||
|
||
String get appDescription {
|
||
switch (locale.languageCode) {
|
||
case 'zh':
|
||
return locale.countryCode == 'TW'
|
||
? '拍攝什麼激發你的靈感'
|
||
: '拍摄什么激发你的灵感';
|
||
case 'en':
|
||
default:
|
||
return 'Shoot What Inspires You';
|
||
}
|
||
}
|
||
|
||
// 导航相关
|
||
String get gallery => _getLocalizedString('图库', '圖庫', 'Gallery');
|
||
String get folders => _getLocalizedString('文件夹', '資料夾', 'Folders');
|
||
String get tags => _getLocalizedString('标签', '標籤', 'Tags');
|
||
String get settings => _getLocalizedString('设置', '設定', 'Settings');
|
||
|
||
// 欢迎页面
|
||
String get welcomeMessage => _getLocalizedString('欢迎使用想拍', '歡迎使用想拍', 'Welcome to InspoSnap');
|
||
String get welcomeSubtitle => _getLocalizedString('拍摄什么激发你的灵感', '拍攝什麼激發你的靈感', 'Shoot What Inspires You');
|
||
String get featureDescription => _getLocalizedString(
|
||
'从其他应用分享图片到这里,\n开始收集你的灵感吧!',
|
||
'從其他應用分享圖片到這裡,\n開始收集你的靈感吧!',
|
||
'Share images from other apps here,\nand start collecting your inspirations!',
|
||
);
|
||
String get startUsing => _getLocalizedString('开始使用', '開始使用', 'Start Using');
|
||
|
||
// 通用操作
|
||
String get save => _getLocalizedString('保存', '儲存', 'Save');
|
||
String get cancel => _getLocalizedString('取消', '取消', 'Cancel');
|
||
String get delete => _getLocalizedString('删除', '刪除', 'Delete');
|
||
String get edit => _getLocalizedString('编辑', '編輯', 'Edit');
|
||
String get search => _getLocalizedString('搜索', '搜尋', 'Search');
|
||
String get share => _getLocalizedString('分享', '分享', 'Share');
|
||
String get export => _getLocalizedString('导出', '匯出', 'Export');
|
||
String get confirm => _getLocalizedString('确认', '確認', 'Confirm');
|
||
String get close => _getLocalizedString('关闭', '關閉', 'Close');
|
||
|
||
// 错误消息
|
||
String get errorTitle => _getLocalizedString('出错了', '出錯了', 'Error');
|
||
String get errorUnknown => _getLocalizedString('出了点小问题,请稍后重试', '出了點小問題,請稍後重試', 'Something went wrong, please try again later');
|
||
String get errorNetwork => _getLocalizedString('网络连接异常,请检查网络后重试', '網路連線異常,請檢查網路後重試', 'Network connection error, please check your connection and try again');
|
||
String get errorStorage => _getLocalizedString('存储空间不足,请清理空间后重试', '儲存空間不足,請清理空間後重試', 'Storage space insufficient, please free up space and try again');
|
||
String get errorPermission => _getLocalizedString('需要相关权限才能继续使用', '需要相關權限才能繼續使用', 'Related permissions are required to continue');
|
||
String get errorImageProcessing => _getLocalizedString('图片处理失败,请检查图片格式或重试', '圖片處理失敗,請檢查圖片格式或重試', 'Image processing failed, please check the image format or try again');
|
||
String get errorShareReceive => _getLocalizedString('出了点小问题,请重新分享试试~', '出了點小問題,請重新分享試試~', 'Something went wrong, please try sharing again~');
|
||
|
||
// 成功消息
|
||
String get successSaved => _getLocalizedString('保存成功', '儲存成功', 'Saved successfully');
|
||
String get successDeleted => _getLocalizedString('删除成功', '刪除成功', 'Deleted successfully');
|
||
String get successUpdated => _getLocalizedString('更新成功', '更新成功', 'Updated successfully');
|
||
|
||
// 空状态
|
||
String get emptyGallery => _getLocalizedString('还没有灵感?去其他App分享图片到这里吧~', '還沒有靈感?去其他App分享圖片到這裡吧~', 'No inspirations yet? Go share images from other apps here~');
|
||
String get emptyFolder => _getLocalizedString('这个文件夹还在等它的第一张灵感图片', '這個文件夾還在等它的第一張靈感圖片', 'This folder is still waiting for its first inspiration image');
|
||
String get emptySearch => _getLocalizedString('换个关键词试试?或者去收集更多灵感吧!', '換個關鍵詞試試?或者去收集更多靈感吧!', 'Try a different keyword? Or go collect more inspirations!');
|
||
|
||
// 主题相关
|
||
String get themeSystem => _getLocalizedString('跟随系统', '跟隨系統', 'Follow System');
|
||
String get themeLight => _getLocalizedString('浅色模式', '淺色模式', 'Light Mode');
|
||
String get themeDark => _getLocalizedString('深色模式', '深色模式', 'Dark Mode');
|
||
String get themeDescriptionSystem => _getLocalizedString('根据系统设置自动切换浅色和深色模式', '根據系統設定自動切換淺色和深色模式', 'Automatically switch between light and dark mode based on system settings');
|
||
String get themeDescriptionLight => _getLocalizedString('始终使用浅色模式', '始終使用淺色模式', 'Always use light mode');
|
||
String get themeDescriptionDark => _getLocalizedString('始终使用深色模式', '始終使用深色模式', 'Always use dark mode');
|
||
|
||
// 语言设置
|
||
String get languageSimplifiedChinese => _getLocalizedString('简体中文', '簡體中文', 'Simplified Chinese');
|
||
String get languageTraditionalChinese => _getLocalizedString('繁体中文', '繁體中文', 'Traditional Chinese');
|
||
String get languageEnglish => _getLocalizedString('English', 'English', 'English');
|
||
|
||
// 设置相关
|
||
String get settingsAppearance => _getLocalizedString('外观设置', '外觀設定', 'Appearance');
|
||
String get settingsStorage => _getLocalizedString('存储管理', '儲存管理', 'Storage Management');
|
||
String get settingsAbout => _getLocalizedString('关于应用', '關於應用', 'About App');
|
||
String get settingsLanguage => _getLocalizedString('语言设置', '語言設定', 'Language');
|
||
String get settingsTheme => _getLocalizedString('主题模式', '主題模式', 'Theme Mode');
|
||
String get settingsGridLayout => _getLocalizedString('网格布局', '網格佈局', 'Grid Layout');
|
||
String get settingsStorageUsage => _getLocalizedString('存储使用情况', '儲存使用情況', 'Storage Usage');
|
||
String get settingsClearCache => _getLocalizedString('清理缓存', '清理緩存', 'Clear Cache');
|
||
String get settingsVersion => _getLocalizedString('版本信息', '版本資訊', 'Version Info');
|
||
String get settingsPrivacyPolicy => _getLocalizedString('隐私政策', '隱私政策', 'Privacy Policy');
|
||
String get settingsTermsOfService => _getLocalizedString('用户协议', '使用者協議', 'Terms of Service');
|
||
|
||
// 分享相关
|
||
String get shareReceiveTitle => _getLocalizedString('保存图片', '儲存圖片', 'Save Image');
|
||
String get shareReceiveMultiple => _getLocalizedString('共 %d 张图片', '共 %d 張圖片', '%d images');
|
||
String get shareReceiveFolder => _getLocalizedString('选择文件夹', '選擇文件夾', 'Select Folder');
|
||
String get shareReceiveTags => _getLocalizedString('添加标签', '新增標籤', 'Add Tags');
|
||
String get shareReceiveNote => _getLocalizedString('添加备注(可选)', '新增備註(可選)', 'Add Note (Optional)');
|
||
String get shareReceiveBatchMode => _getLocalizedString('批量应用', '批次應用', 'Batch Apply');
|
||
String get shareReceiveSingleMode => _getLocalizedString('单张编辑', '單張編輯', 'Single Edit');
|
||
String get shareReceiveSaveProgress => _getLocalizedString('正在保存 %d/%d', '正在儲存 %d/%d', 'Saving %d/%d');
|
||
|
||
// 文件夹相关
|
||
String get folderUncategorized => _getLocalizedString('未分类', '未分類', 'Uncategorized');
|
||
String get folderCreateNew => _getLocalizedString('新建文件夹', '新建文件夾', 'New Folder');
|
||
String get folderNameHint => _getLocalizedString('请输入文件夹名称', '請輸入文件夾名稱', 'Enter folder name');
|
||
String get folderIcon => _getLocalizedString('选择图标', '選擇圖標', 'Select Icon');
|
||
String get folderColor => _getLocalizedString('选择颜色', '選擇顏色', 'Select Color');
|
||
|
||
// 标签相关
|
||
String get tagCreateNew => _getLocalizedString('创建标签', '建立標籤', 'Create Tag');
|
||
String get tagNameHint => _getLocalizedString('请输入标签名称', '請輸入標籤名稱', 'Enter tag name');
|
||
String get tagMaxLength => _getLocalizedString('标签最多20个字符', '標籤最多20個字元', 'Tag max 20 characters');
|
||
String get tagDuplicate => _getLocalizedString('标签已存在', '標籤已存在', 'Tag already exists');
|
||
|
||
// 图片详情
|
||
String get imageDetails => _getLocalizedString('图片详情', '圖片詳情', 'Image Details');
|
||
String get imageInfo => _getLocalizedString('图片信息', '圖片資訊', 'Image Info');
|
||
String get imageFolder => _getLocalizedString('所属文件夹', '所屬文件夾', 'Folder');
|
||
String get imageTags => _getLocalizedString('标签', '標籤', 'Tags');
|
||
String get imageNote => _getLocalizedString('备注', '備註', 'Note');
|
||
String get imageSize => _getLocalizedString('文件大小', '檔案大小', 'File Size');
|
||
String get imageDimensions => _getLocalizedString('图片尺寸', '圖片尺寸', 'Dimensions');
|
||
String get imageFormat => _getLocalizedString('图片格式', '圖片格式', 'Format');
|
||
String get imageCreated => _getLocalizedString('保存时间', '儲存時間', 'Saved Time');
|
||
|
||
// 删除确认
|
||
String get deleteConfirm => _getLocalizedString('确定要删除吗?', '確定要刪除嗎?', 'Are you sure you want to delete?');
|
||
String get deleteConfirmImage => _getLocalizedString('删除后无法恢复', '刪除後無法恢復', 'Cannot be recovered after deletion');
|
||
String get deleteConfirmFolder => _getLocalizedString('文件夹删除后,其中的图片将移至"未分类"', '文件夾刪除後,其中的圖片將移至"未分類"', 'After folder deletion, images will be moved to "Uncategorized"');
|
||
|
||
/// 获取本地化字符串的通用方法
|
||
String _getLocalizedString(String simplified, String traditional, String english) {
|
||
switch (locale.languageCode) {
|
||
case 'zh':
|
||
return locale.countryCode == 'TW' ? traditional : simplified;
|
||
case 'en':
|
||
default:
|
||
return english;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 本地化委托类
|
||
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
|
||
const AppLocalizationsDelegate();
|
||
|
||
@override
|
||
bool isSupported(Locale locale) {
|
||
// 检查是否支持该locale
|
||
for (final supportedLocale in AppLocalizations.supportedLocales) {
|
||
if (supportedLocale.languageCode == locale.languageCode) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
@override
|
||
Future<AppLocalizations> load(Locale locale) async {
|
||
// 返回对应语言的本地化实例
|
||
return AppLocalizations(locale);
|
||
}
|
||
|
||
@override
|
||
bool shouldReload(AppLocalizationsDelegate old) => false;
|
||
}
|
||
|
||
/// 本地化扩展方法
|
||
extension LocalizationsContextExtension on BuildContext {
|
||
/// 获取本地化实例
|
||
AppLocalizations? get loc => AppLocalizations.of(this);
|
||
|
||
/// 获取当前语言代码
|
||
String? get languageCode => loc?.locale.languageCode;
|
||
|
||
/// 检查是否为中文
|
||
bool get isChinese => languageCode == 'zh';
|
||
|
||
/// 检查是否为英文
|
||
bool get isEnglish => languageCode == 'en';
|
||
|
||
/// 获取文本方向
|
||
TextDirection get textDirection {
|
||
return isEnglish ? TextDirection.ltr : TextDirection.ltr;
|
||
}
|
||
}
|
||
|
||
/// 本地化混入
|
||
mixin LocalizationsMixin {
|
||
/// 获取本地化实例
|
||
AppLocalizations? getLocalization(BuildContext context) {
|
||
return AppLocalizations.of(context);
|
||
}
|
||
|
||
/// 获取应用名称
|
||
String getAppName(BuildContext context) {
|
||
return getLocalization(context)?.appName ?? '想拍';
|
||
}
|
||
|
||
/// 获取应用描述
|
||
String getAppDescription(BuildContext context) {
|
||
return getLocalization(context)?.appDescription ?? '拍摄什么激发你的灵感';
|
||
}
|
||
|
||
/// 获取错误消息
|
||
String getErrorMessage(BuildContext context, String errorKey) {
|
||
final localization = getLocalization(context);
|
||
if (localization == null) return '出了点小问题';
|
||
|
||
switch (errorKey) {
|
||
case 'unknown':
|
||
return localization.errorUnknown;
|
||
case 'network':
|
||
return localization.errorNetwork;
|
||
case 'storage':
|
||
return localization.errorStorage;
|
||
case 'permission':
|
||
return localization.errorPermission;
|
||
case 'image_processing':
|
||
return localization.errorImageProcessing;
|
||
case 'share_receive':
|
||
return localization.errorShareReceive;
|
||
default:
|
||
return localization.errorUnknown;
|
||
}
|
||
}
|
||
} |