当前位置:   article > 正文

WPF国际化的实现方法_wpf 国际化

wpf 国际化

一、用xaml资源文件实现

用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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用:

 <Label Content="{DynamicResource hello}" />
  • 1

要切换的时候,加载对应的文件,即可完成切换。代码如下:
英文

    ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(new Uri("Language/en-US.xaml", UriKind.Relative)) ;     
	if (resource != null)
    {
        Application.Current.Resources.MergedDictionaries.Add(resource);
    }
  • 1
  • 2
  • 3
  • 4
  • 5

中文

    ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(new Uri("Language/zh-CN.xaml", UriKind.Relative)) ;     
	if (resource != null)
    {
        Application.Current.Resources.MergedDictionaries.Add(resource);
    }
  • 1
  • 2
  • 3
  • 4
  • 5

二、resx资源文件实现

resx资源文件,实现的过程比第一种复杂,但resx文件本身编辑比较简单,维护起来比较方便。需要用到的框架:WpfExtensions.Xaml

1、为每种语言添加.resx资源文件。
2、添加一个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 #>));
<#
    }
#>
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

图:1、2步完成之后
在这里插入图片描述

3、用Nuget包管理器搜索安装WpfExtensions.Xaml框架
4、加载资源文件。
public App()
{
	I18nManager.Instance.Add(I18nResource.I18nResource.ResourceManager);
}
  • 1
  • 2
  • 3
  • 4
5、在mainwindow.xaml中引入命名空间。
 xmlns:markup="clr-namespace:WpfExtensions.Xaml.Markup;assembly=WpfExtensions.Xaml"
 xmlns:i18n="clr-namespace:Eyu.I18nDEMO.I18nResource"
  • 1
  • 2
6、使用资源
<Label Content="{markup:I18n {x:Static i18n:I18nProvider.hello}}" />
  • 1
7、切换语言的方法
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;
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
8、源代码

https://gitee.com/eyupaopao/wpfi18n-demo.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/427791
推荐阅读
相关标签
  

闽ICP备14008679号