针对 WPF 的多语言支持库
- WPF
- 定义语言文件
//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 JsonFileDB(path), true,"zh");
//嵌入模式
//LanService.Init(new EmbeddedJsonDB("Samples.WPF.EmbeddedConfig.Languages"), true, "en");
- XAML
<Window
...
xmlns:lan="clr-namespace:MultiLanguageForXAML;assembly=MultiLanguageForXAML">
<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
LanService.UpdateCulture("en");
- 自定义控件映射(可选)
Xaml.CustomMaps.Add(typeof(CustomTitleBar), CustomTitleBar.TitleProperty);
应用推荐