Skip to content

多语言 ​

AvaloniaFluentUI提供了LocalizationService类,用于翻译内置组件的文本。使用方式较为简单,默认情况下翻译结果为en-US使用方法:

csharp
// 获取实例
LocalizationService.Instance;

// 设置默认语言
LocalizationService.Instance.SetCulture("zh-CN");

通过App.axaml文件设置默认语言 ​

xml
<Application
  x:Class="Demo.App"
  xmlns="https://github.com/avaloniaui"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:fs="clr-namespace:AvaloniaFluentUI.Styling;assembly=AvaloniaFluentUI"
  xmlns:local="using:Demo"
  RequestedThemeVariant="Default">
  <!--  "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options.  -->

  <Application.DataTemplates>
    <local:ViewLocator/>
  </Application.DataTemplates>

  <Application.Styles>
    <fs:AvaloniaFluentTheme Locale="zh-CN"/>
  </Application.Styles>
</Application>

NOTE

LoadResxFile LoadResxDirectory默认只加载当前语言的文件,语言改变后会自动加载对应的文件

默认内置控件暂只支持en-US zh-CN ja-JP三种翻译, 可添加自定义翻译语言,添加方法:

csharp
// 通过字典添加
LocalizationService.Instance.AddValue("zh-CN:Key", "Value");
LocalizationService.Instance.AddValue("en-US:Key", "Value");

LocalizationService.Instance.AddValues(
    ["zh-CN:Key", "zh-CN:Key", ...],
    ["Value", "Value", ...]
);

// 通过 resx 文件添加
Strings.en-US.resx;
Strings.zh-CN.resx;
Strings.ja-JP.resx;
...
// 加载单个文件
LocalizationService.Instance.LoadResxFile(PATH);

// 加载整个文件夹
LocalizationService.Instance.LoadResxDirectory(DIR);

// 获取 Value
// 通过 XAML
Text="{Binding Source={x:Static locale:LocalizationService.Instance}, Path=[Key]}"

// 通过代码
LocalizationService.Instance.GetString("Key");
LocalizationService.Instance.GetString("Key", "CultureInfo");

基于 MIT 许可发布