using System; using System.Collections.Generic; using System.Globalization; using System.Windows; using Prism.Commands; using Prism.Mvvm; using XP.Common.Localization.Enums; using XP.Common.Localization.Interfaces; using XP.Common.Logging.Interfaces; namespace XP.Common.Localization.ViewModels { /// /// 语言切换器 ViewModel | Language switcher ViewModel /// 提供语言选择和切换功能 | Provides language selection and switching functionality /// public class LanguageSwitcherViewModel : BindableBase { private readonly ILocalizationService _localizationService; private readonly ILocalizationConfig _config; private readonly ILoggerService _logger; private LanguageOption? _selectedLanguage; /// /// 选中的语言选项 | Selected language option /// public LanguageOption? SelectedLanguage { get => _selectedLanguage; set { if (SetProperty(ref _selectedLanguage, value)) { ApplyCommand.RaiseCanExecuteChanged(); UpdatePreviewTexts(); } } } /// /// 可用语言列表 | Available languages list /// public IEnumerable AvailableLanguages { get; } /// /// 应用按钮命令 | Apply button command /// public DelegateCommand ApplyCommand { get; } private string _previewRestartNotice = string.Empty; /// /// 预览重启提示文本 | Preview restart notice text /// public string PreviewRestartNotice { get => _previewRestartNotice; set => SetProperty(ref _previewRestartNotice, value); } private string _previewApplyButtonText = string.Empty; /// /// 预览应用按钮文本 | Preview apply button text /// public string PreviewApplyButtonText { get => _previewApplyButtonText; set => SetProperty(ref _previewApplyButtonText, value); } /// /// 构造函数 | Constructor /// /// 本地化服务 | Localization service /// 本地化配置 | Localization configuration /// 日志服务 | Logger service public LanguageSwitcherViewModel( ILocalizationService localizationService, ILocalizationConfig config, ILoggerService logger) { _localizationService = localizationService ?? throw new ArgumentNullException(nameof(localizationService)); _config = config ?? throw new ArgumentNullException(nameof(config)); _logger = logger.ForModule(); // 初始化可用语言列表 | Initialize available languages list AvailableLanguages = new[] { new LanguageOption(SupportedLanguage.ZhCN, "简体中文", "🇨🇳"), new LanguageOption(SupportedLanguage.ZhTW, "繁體中文", "🇹🇼"), new LanguageOption(SupportedLanguage.EnUS, "English", "🇺🇸") }; // 设置当前选中的语言 | Set currently selected language var currentLang = localizationService.CurrentLanguage; foreach (var lang in AvailableLanguages) { if (lang.Language == currentLang) { _selectedLanguage = lang; break; } } // 初始化命令 | Initialize commands ApplyCommand = new DelegateCommand(OnApply, CanApply); // 初始化预览文本 | Initialize preview texts UpdatePreviewTexts(); } /// /// 更新预览文本 | Update preview texts /// private void UpdatePreviewTexts() { if (_selectedLanguage == null) return; // 获取选中语言的 CultureInfo | Get CultureInfo for selected language var previewCulture = GetCultureInfo(_selectedLanguage.Language); // 使用选中的语言获取本地化文本 | Get localized text using selected language PreviewRestartNotice = _localizationService.GetString("Settings_Language_RestartNotice", previewCulture); PreviewApplyButtonText = _localizationService.GetString("Button_Apply", previewCulture); } /// /// 判断是否可以应用 | Determine if can apply /// 与配置文件中已保存的语言比较 | Compare with saved language in config /// private bool CanApply() { if (_selectedLanguage == null) return false; // 获取配置文件中已保存的语言 | Get saved language from config var savedLanguage = _config.GetSavedLanguage() ?? _localizationService.CurrentLanguage; return _selectedLanguage.Language != savedLanguage; } /// /// 应用语言切换 | Apply language change /// private void OnApply() { if (_selectedLanguage == null) return; try { var newLanguage = _selectedLanguage.Language; _localizationService.SetLanguage(newLanguage); _logger.Info($"Language switched to {newLanguage}"); // 显示重启提示对话框 | Show restart prompt dialog MessageBox.Show( _localizationService.GetString("Settings_Language_SavedRestartRequired"), _localizationService.GetString("Dialog_Notice"), MessageBoxButton.OK, MessageBoxImage.Information); // 更新命令状态 | Update command state ApplyCommand.RaiseCanExecuteChanged(); } catch (Exception ex) { _logger.Error(ex, $"Failed to switch language to {_selectedLanguage.Language}"); // 显示错误对话框 | Show error dialog var errorMsg = string.Format( _localizationService.GetString("Settings_Language_SwitchFailed"), ex.Message); MessageBox.Show( errorMsg, _localizationService.GetString("Dialog_Error"), MessageBoxButton.OK, MessageBoxImage.Error); // 恢复到之前的语言 | Restore previous language var currentLang = _localizationService.CurrentLanguage; foreach (var lang in AvailableLanguages) { if (lang.Language == currentLang) { _selectedLanguage = lang; RaisePropertyChanged(nameof(SelectedLanguage)); break; } } } } /// /// 获取语言对应的 CultureInfo | Get CultureInfo for language /// private CultureInfo GetCultureInfo(SupportedLanguage language) { return language switch { SupportedLanguage.ZhCN => new CultureInfo("zh-CN"), SupportedLanguage.ZhTW => new CultureInfo("zh-TW"), SupportedLanguage.EnUS => new CultureInfo("en-US"), _ => new CultureInfo("zh-CN") }; } } }