WPF实现多语言支持 动态加载资源词典

By | 2019-11-02

软件开发常常会被要求支持多国语言。而且想要软件有更多的受众,做好本地化无疑是必然的选择。对于WPF程序而言,支持多语言的方式有很多种,这里介绍一种比较常见了方式。

1、 创建资源词典,首先新建两个词典文件en-us.xaml、zh-cn.xaml

zh-cn.xaml code:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:GPIO_TOOL_WIN"
                    xmlns:s="clr-namespace:System;assembly=mscorlib">
    <s:String x:Key="buttonTest1">测试按钮1</s:String>
    <s:String x:Key="buttonTest2">测试按钮2</s:String>
    <s:String x:Key="buttoncn">中文</s:String>
    <s:String x:Key="buttonen">英文</s:String>
</ResourceDictionary>

en-us.xaml code:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:TEST_TOOL_WIN"
                    xmlns:s="clr-namespace:System;assembly=mscorlib">
    <s:String x:Key="buttonTest1">Button TEST1</s:String>
    <s:String x:Key="buttonTest2">Button TEST2</s:String>
    <s:String x:Key="buttoncn">Chinese</s:String>
    <s:String x:Key="buttonen">English</s:String>
</ResourceDictionary>

2、 将两个资源词典添加到App.xaml中

<Application x:Class="GPIO_TOOL_WIN.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:TEST_TOOL_WIN"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources\en-us.xaml" />
                <ResourceDictionary Source="Resources\zh-cn.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

3、 在界面设计器中需要显示的位置添加动态资源

例如:

<Window x:Class="GPIO_TOOL_WIN.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TEST_TOOL_WIN"
        mc:Ignorable="d"
        Title="WPF TEST" Height="450" Width="800">
    <Grid>
        <Button x:Name="buttonTest1" Content="{DynamicResource buttonTest1}" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="100"/>
        <Button x:Name="buttonTest2" Content="{DynamicResource buttonTest2}" HorizontalAlignment="Left" Margin="20,50,0,0" VerticalAlignment="Top" Width="100"/>
        <Button x:Name="buttoncn" Content="{DynamicResource buttoncn}" HorizontalAlignment="Left" Margin="20,150,0,0" VerticalAlignment="Top" Width="75" Click="Buttoncn_Click"/>
        <Button x:Name="buttonen" Content="{DynamicResource buttonen}" HorizontalAlignment="Left" Margin="120,150,0,0" VerticalAlignment="Top" Width="75" Click="Buttonen_Click"/>
    </Grid>
</Window>

4、 切换语言代码如下:

List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
            foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            {
                dictionaryList.Add(dictionary);
            }
            string requestedCulture = @"Resources\en-us.xaml";//or zh-cn.xaml
            ResourceDictionary resourceDictionary = dictionaryList.Find(d => d.Source.OriginalString.Equals(requestedCulture));
            Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
            Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);

5、测试效果

发表评论

邮箱地址不会被公开。 必填项已用*标注