完整项目地址
github : https://github.com/Crazy-GrowUp/AvaloniaI18nTest/tree/master
gitee :https://gitee.com/jack_of_disco/avalonia-i18n-test
0.项目新建 Properties 文件夹
对应的项目配置文件里面就会增加 <Folder Include="Properties\" />
1.项目添加新建项目
在 Properties 里面添加资源文件,命名为 Resource.resx
2.将自定义工具设置为 PublicResXFileCodeGenerator(重要)
右键 Resource.resx
文件,打开属性,修改属性中自定义工具为 PublicResXFileCodeGenerator
3.添加文本资源
4.安装扩展 ResXManager(推荐)
安装完成后,可能需要重启VS
5.添加多国语言
方式一:右键 Resource.resx
使用 ResXManager 打开
Properties 文件夹下面就多了一个 Resource.en.resx
方式二:手动添加多国语言文件
比如我们要添加中文,那么就新建 Resource.zh.resx
资源文件
然后将对应的翻译填写到资源文件中
6.在 .axaml 文件中使用
- 在 MainWindow.axaml 中加上
xmlns:prop="using:AvaloniaI18nTest.Properties"
- 通过
{x:Static prop:Resource.Name}
进行使用
7.在代码中使用
-
在 MainWindowViewModel 中 使用
using AvaloniaI18nTest.Properties;
或者using prop = AvaloniaI18nTest.Properties;
-
取出其中的值
public string ResxName { get; } = Resource.Name; public string ResxName2 { get; } = prop.Resource.Name;
-
在 axaml 中显示
8.切换语言
切换需要需要在 App.axaml.cs 中的 Initialize() 方法中进行设置
// 不写或者为空就是默认系统语言
// Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("");
// 修改语言
// Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");