让WPF应用程序支持多语言

其实实现这个功能原理很简单 就是把相应语言的资源文件加载上,当然程序中的文字部分肯定不能写死,只能以(DynamicResource,StaticResource)的形式出现,下面是一个非常简单的例子仅供参考!

 

1.首先是2个资源文件

Resources.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="Tradename1">苹果</sys:String>
    <sys:String x:Key="Tradename2">橘子</sys:String>
    <sys:String x:Key="Tradename3">香蕉</sys:String>
    <sys:String x:Key="Tradename4">西瓜</sys:String>
    <sys:String x:Key="Tradename5">桃子</sys:String>

</ResourceDictionary>

 

 

Resources.en-US.xaml(英文)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:sys="clr-namespace:System;assembly=mscorlib"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   
    <sys:String x:Key="Tradename1">Apple</sys:String>
    <sys:String x:Key="Tradename2">Orange</sys:String>
    <sys:String x:Key="Tradename3">Banana</sys:String>
    <sys:String x:Key="Tradename4">Watermelon</sys:String>
    <sys:String x:Key="Tradename5">Peach</sys:String>

</ResourceDictionary>

 

2.窗口XAML
    <Grid>

        <StackPanel Orientation="Vertical">
            <Label Content="{DynamicResource Tradename1}"/>
            <Label Content="{DynamicResource Tradename2}"/>
            <Label Content="{DynamicResource Tradename3}"/>
            <Label Content="{DynamicResource Tradename4}"/>
            <Label Content="{DynamicResource Tradename5}"/>
        </StackPanel>

        <Button Content="English" Name="Bt_US" HorizontalAlignment="Left" Margin="301,10,0,0" VerticalAlignment="Top" Width="75" Click="Bt_US_Click"/>
        <Button Content="中文" Name="Bt_CN" HorizontalAlignment="Left" Margin="221,10,0,0" VerticalAlignment="Top" Width="75" Click="Bt_CN_Click"/>

    </Grid>

窗口CS

      private void Bt_CN_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Resources.zh-CN.xaml", UriKind.RelativeOrAbsolute) });
        }

        private void Bt_US_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Resources.en-US.xaml", UriKind.RelativeOrAbsolute) });
        }

 

就上面简单的两部就可以完成手动选择不同的语言了,当然如果你想程序加载的时候就自动选择语言也可以

在App.xaml.cs中(没有构造函数的加上构造函数)

        public App()
        {
            this.Startup += (s, e) => {
                string _Language = System.Globalization.CultureInfo.InstalledUICulture.Name.ToString(CultureInfo.InvariantCulture);

                switch (_Language)
                {
                    case "zh-CN":
                        App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Resources.zh-CN.xaml", UriKind.RelativeOrAbsolute) });
                        break;
                    case "en-US":
                        App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Resources.en-US.xaml", UriKind.RelativeOrAbsolute) });
                        break;
                }
            };
        }

 

 

 

 

 

 

 

你可能感兴趣的:(让WPF应用程序支持多语言)