# 05 - 国际化(i18n)示例 演示如何使用 Sharp.Wpf 的国际化功能实现多语言支持,包括运行时切换语言、资源文件管理等。 ## 🎯 学习目标 - 使用 `ILocalizationService` 实现多语言 - 在 XAML 中使用 `{loc:Localize}` 绑定本地化字符串 - 运行时动态切换语言 - 管理资源文件(.resx) - 格式化带参数的本地化字符串 ## 📦 使用的模块 - **Sharp.Wpf** - ILocalizationService, LocalizeExtension ## 🚀 快速运行 ```bash cd examples/05-Localization dotnet run ``` ## 📝 核心代码讲解 ### 1. 创建资源文件 ``` Resources/ ├── Strings.resx # 默认语言(中文) ├── Strings.en-US.resx # 英文 └── Strings.ja-JP.resx # 日文(可选) ``` **Strings.resx** (中文): ```xml 欢迎使用 Sharp 框架 用户名 密码 登录 欢迎, {0}! ``` **Strings.en-US.resx** (英文): ```xml Welcome to Sharp Framework Username Password Login Welcome, {0}! ``` ### 2. App.xaml.cs - 注册国际化服务 ```csharp using System.Globalization; using System.Resources; using Wpf.Localization; protected override void OnStartup(StartupEventArgs e) { var services = new ServiceCollection(); services.AddSharpWpf(); // 添加国际化支持 var resourceManager = new ResourceManager( "LocalizationExample.Resources.Strings", // 资源文件的完整命名空间 Assembly.GetExecutingAssembly()); services.AddLocalization( resourceManager, new CultureInfo("zh-CN"), // 中文 new CultureInfo("en-US"), // 英文 new CultureInfo("ja-JP") // 日文 ); services.AddViewModel(); var provider = services.BuildServiceProvider(); var mainWindow = provider.GetRequiredService(); mainWindow.Show(); } ``` ### 3. MainViewModel.cs - 语言切换 ```csharp using Wpf.Localization; public class MainViewModel : ViewModelBase { private readonly ILocalizationService _localization; private readonly IDialogService _dialogService; public ILocalizationService Localization => _localization; public List AvailableLanguages { get; } public CultureInfo CurrentLanguage { get => _localization.CurrentCulture; set { _localization.ChangeCulture(value); OnPropertyChanged(); } } public ICommand ChangeLanguageCommand { get; } public ICommand ShowGreetingCommand { get; } public MainViewModel( ILocalizationService localization, IDialogService dialogService) { _localization = localization; _dialogService = dialogService; AvailableLanguages = localization.GetSupportedCultures().ToList(); // 监听语言变化 _localization.CultureChanged += (s, culture) => { _dialogService.ShowMessage( $"语言已切换到: {culture.NativeName}", "提示"); }; ChangeLanguageCommand = new RelayCommand(culture => { if (culture != null) { CurrentLanguage = culture; } }); ShowGreetingCommand = new RelayCommand(() => { // 使用格式化字符串 var message = _localization.GetString("WelcomeUser", "张三"); _dialogService.ShowMessage(message, "问候"); }); } } ``` ### 4. MainWindow.xaml - 使用本地化绑定 ```xml