---
title: "WPF 多语言"
author: "EvenCode"
date: "2014-03-14T12:01:22Z"
tags: []
categories: ["编程语言"]
slug: "wpf"
draft: false
lastmod: "2014-03-14T16:33:35Z"
id: 2031031
url: "https://evencode.iteye.com/blog/2031031"
---

一．本功能的原理

1、为不同语言创建不同的资源文件，如en-us.xaml、zh-cn.xaml

2、在App.xaml加载时根据配置文件的值读取不同的资源文件

3、在各窗体的xaml文件中利用动态绑定显示资源文件内容

4、更改不同语言需修改配置文件的值并重新启动程序，也可获取当前操作系统的语言类型在启动时读取不同的语言资源文件

 

 

en-us.xaml(zh-cn.xaml将内容改为中文） 

&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    

                    xmlns:s="clr-namespace:System;assembly=mscorlib"&gt;

    &lt;s:String x:Key="MainWindowTitle"&gt;Localization Demos&lt;/s:String&gt;

    &lt;s:String x:Key="LanguageMenuHeader"&gt;\_Languages&lt;/s:String&gt;

    &lt;s:String x:Key="EnglishMenuHeader"&gt;\_Englishs&lt;/s:String&gt;

    &lt;s:String x:Key="ChineseMenuHeader"&gt;\_EnglishsHeader&lt;/s:String&gt;

    &lt;s:String x:Key="OpenLanguageFileMenuHeader"&gt;\_Open Language Files&lt;/s:String&gt;

&lt;/ResourceDictionary&gt;

 

 

 创建配置文件App.Config（需引入命名空间System.configuration)

  &lt;appSettings&gt;

    &lt;add key="Lang" value="/Resources/Langs/en-us.xaml" /&gt;

  &lt;/appSettings&gt;

 

 

public partial class App : Application

    {

        protected override void OnStartup(StartupEventArgs e)

        {

            base.OnStartup(e);

 

            string langType = ConfigurationManager.AppSettings.Get("Lang");

            App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(langType, UriKind.RelativeOrAbsolute) });

        }

}

动态绑定：

    &lt;StackPanel VirtualizingStackPanel.VirtualizationMode="Recycling" HorizontalAlignment="Left" VerticalAlignment="Top"&gt;

        &lt;Label Content="{DynamicResource MainWindowTitle}"/&gt;

        &lt;Label Content="{DynamicResource LanguageMenuHeader}"/&gt;

        &lt;Label Content="{DynamicResource EnglishMenuHeader}"/&gt;

        &lt;Label Content="{DynamicResource ChineseMenuHeader}"/&gt;

        &lt;Label Content="{DynamicResource OpenLanguageFileMenuHeader}"/&gt;

    &lt;/StackPanel&gt;

 

修改配置文件值：

  XmlDocument doc = new XmlDocument();

            doc.Load(path);

            XmlNode node = doc.SelectSingleNode(@"//add\[@key='Lang']");

            XmlElement ele = (XmlElement)node;

            ele.SetAttribute("value", Type);

            doc.Save(path);

 

 

在运行时动态更换语言：

ResourceDictionary langRd = null;

```
                langRd =
                    Application.LoadComponent(
                             new Uri(@"Lang\" + lang + ".xaml", UriKind.Relative))
                    as ResourceDictionary;
   

            if (langRd != null)
            {
                if (this.Resources.MergedDictionaries.Count > 0)
                {
                    this.Resources.MergedDictionaries.Clear();
                }
                this.Resources.MergedDictionaries.Add(langRd);
```