/MultiLanguageForXAML

Multi-Language support For XAML. WPF/UWP 动态多语言切换支持

Primary LanguageC#MIT LicenseMIT

MultiLanguageForXAML

针对WPF和UWP的通用多语言支持库

Nuget

效果预览

  • WPF

steup

  • UWP

steup

用法

  • 定义语言文件
 //Languages/zh.json 编译时拷贝到目录
   {
   "txt": "一",
   "format":"你好 {0} !",
   "world":"世界"
   }

//Languages/en.json 编译时拷贝到目录
   {
   "txt": "one",
   "format":"hello {0} !",
   "world":"world"
   }

  • 初始化
            //WPF
            //怀疑用Environment.CurrentDirectory开机启动时目录会出错,待验证
            string appDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string path = Path.Combine(appDir, "Res\\Languages");
            LanService.Init(new JsonDB(path), true);
            
            //UWP
            string path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Languages");
            LanService.Init(new JsonDB(path), true);
  • XAML
<Window
    ...
    xmlns:lan="clr-namespace:MultiLanguageManager;assembly=MultiLanguageManager.WPF">
    <StackPanel>
        <Button lan:Xaml.Key="txt" />
        <Button lan:Xaml.Key="format">
            <lan:Xaml.Parameters>
                <lan:FormatParameters>
                    <Run
                        lan:Xaml.Key="world"
                        FontStyle="Italic"
                        Foreground="Red" />
                </lan:FormatParameters>
            </lan:Xaml.Parameters>
        </Button>
    </StackPanel>
</Window/>
  • 切换语言
            //WPF
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(CultureName);
            await LanService.UpdateLanguage();
            
            //UWP
            ApplicationLanguages.PrimaryLanguageOverride = CultureName;
            await LanService.UpdateLanguage();

全栈开发qq群:191034956