https://blog.csdn.net/eyupaopao/article/details/120090431
resx資源文件實現(xiàn)
resx資源文件,實現(xiàn)的過程比第一種復雜,但resx文件本身編輯比較簡單,維護起來比較方便。需要用到的框架:WpfExtensions.Xaml
- 為每種語言添加.resx資源文件,放在I18nResource文件夾下
I18nResource.resx 代表英語,名字不要改
I18nResource.zh-CN.resx 代表中文
國家簡寫
創(chuàng)建完I18nResource.resx文件后會生成一個I18nResource.Designer.cs文件 - 添加一個I18nProvider.tt文件,與資源文件放在同一個文件夾,visual studio會自動解析這個文件,并掃描resx資源文件,并生成一個I18nProvider.cs文件,代碼:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
using System.Windows;
<#
const string ResourceFileName = "I18nResource.resx";
#>
namespace <#=System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint").ToString()#>
{
public static class I18nProvider
{
<#
var resourceKeys = XElement.Load(this.Host.ResolvePath(ResourceFileName))
.Elements("data")
.Select(item => item.Attribute("name")?.Value)
.Where(item => item != null);
var resourceDesignerName = Path.GetFileNameWithoutExtension(ResourceFileName);
foreach (string resourceKey in resourceKeys)
{
#>
public static readonly ComponentResourceKey <#= resourceKey #> = new ComponentResourceKey(typeof(<#= resourceDesignerName #>), nameof(<#= resourceDesignerName #>.<#= resourceKey #>));
<#
}
#>
}
}
- 如果修改了resx文件,需要把I18nProvider.tt重新生成一下,右鍵運行自定義工具
- 在代碼中訪問
I18nManager.Instance.Get(I18nProvider.Input_User_Name).ToString()
- 多語言開源項目
Accelerider.Windows
TerminalMACS.ManagerForWPF文章來源:http://www.zghlxwxcb.cn/news/detail-660551.html
WpfExtensions文章來源地址http://www.zghlxwxcb.cn/news/detail-660551.html
到了這里,關于WPF國際化的實現(xiàn)方法(WpfExtensions.Xaml)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!