赞
踩
用xaml实现比较简单,缺点是后期软件越来越大,xaml编辑起来就变得相当麻烦。
首先每种需要支持的语言创建一个xaml文件。如图:
内容:
zh-CN.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="hello">你好世界!</sys:String>
<sys:String x:Key="title">国际化程序</sys:String>
</ResourceDictionary>
en-US.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="hello">Hello world!</sys:String>
<sys:String x:Key="title">Internationalization App</sys:String>
</ResourceDictionary>
使用:
<Label Content="{DynamicResource hello}" />
要切换的时候,加载对应的文件,即可完成切换。代码如下:
英文
ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(new Uri("Language/en-US.xaml", UriKind.Relative)) ;
if (resource != null)
{
Application.Current.Resources.MergedDictionaries.Add(resource);
}
中文
ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(new Uri("Language/zh-CN.xaml", UriKind.Relative)) ;
if (resource != null)
{
Application.Current.Resources.MergedDictionaries.Add(resource);
}
resx资源文件,实现的过程比第一种复杂,但resx文件本身编辑比较简单,维护起来比较方便。需要用到的框架:WpfExtensions.Xaml
<#@ 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 #>));
<#
}
#>
}
}
图:1、2步完成之后
public App()
{
I18nManager.Instance.Add(I18nResource.I18nResource.ResourceManager);
}
xmlns:markup="clr-namespace:WpfExtensions.Xaml.Markup;assembly=WpfExtensions.Xaml"
xmlns:i18n="clr-namespace:Eyu.I18nDEMO.I18nResource"
<Label Content="{markup:I18n {x:Static i18n:I18nProvider.hello}}" />
public static void SetLanguage(string languageTag)
{
CultureInfo currentUICulture = I18nManager.Instance.CurrentUICulture;
if (languageTag == currentUICulture.Name) return;
CultureInfo newCulture = new CultureInfo(languageTag);
I18nManager.Instance.CurrentUICulture = newCulture;
}
https://gitee.com/eyupaopao/wpfi18n-demo.git
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。