WPF多语言

该博客介绍了如何在WPF应用中实现多语言功能,包括创建Resources文件夹和Langs子文件夹来存放不同语言的文本文件,如zh-CN.xaml和en-US.xaml。文章通过展示中文和英文界面的执行效果,详细阐述了实现多语言的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


源码下载地址:WPF多语言实现源码


执行效果:

1. 中文界面



2. 英文界面




实现步骤:

1. 新建Resources文件夹用于存放资源,在Resources中新建Langs用于存放多语言文本文件;

2. 在Langs文件夹中新建zh-CN.xaml和en-US.xaml;

   

    2.1 zh-CN.xaml内容如下:

       

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">
    <system:String x:Key="AppTitle">本地化多语言示例</system:String>
    <system:String x:Key="LanguageMenuHeader">语言</system:String>
    <system:String x:Key="LanguageOptionEnglish">英文</system:String>
    <system:String x:Key="LanguageOptionChinese">中文</system:String>
    <system:String x:Key="LanguageCurrent">当前语言:</system:String>
    <system:String x:Key="LanguageChange">语言更改</system:String>
</ResourceDictionary>

    2.2 en-US.xaml内容如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">
    <system:String x:Key="AppTitle">Localized multi-language demo</system:String>
    <system:String x:Key="LanguageMenuHeader">Languages</system:String>
    <system:String x:Key="LanguageOptionEnglish">English</system:String>
    <system:String x:Key="LanguageOptionChinese">Chinese</system:String>
    <system:String x:Key="LanguageCurrent">Current Language:</system:String>
    <system:String x:Key="LanguageChange">Change Language</system:String>
</ResourceDictionary>

3.  在app.config添加内容:

  <appSettings>
    <add key="Lang_zh-cn" value="/Resources/Langs/zh-cn.xaml" />
    <add key="Lang_en-us" value="/Resources/Langs/en-us.xaml" />
  </appSettings>

4. 主窗口布局:

<Grid>
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
            
        <GroupBox x:Name="groupbox_languages"  Header="{DynamicResource LanguageMenuHeader}" FontSize="24" Background="BurlyWood" BorderBrush="Blue" BorderThickness="3" Margin="20"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <StackPanel Margin="5,5,0,0">
                <TextBlock x:Name="textblock_option_zhcn" Text="{DynamicResource LanguageOptionChinese}" FontSize="22" Foreground="WhiteSmoke"/>
                <TextBlock x:Name="textblock_option_enus" Text="{DynamicResource LanguageOptionEnglish}" FontSize="22" Foreground="WhiteSmoke"/>
            </StackPanel>
        </GroupBox>

        <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Text="{DynamicResource LanguageChange}" FontSize="20" Foreground="Black" HorizontalAlignment="Left" VerticalAlignment="Center"/>
            <ComboBox x:Name="combobox_langChanger" Margin="5,0,0,0"
                    FontSize="20" MinWidth="150" MinHeight="30"
                    SelectionChanged="combobox_langChanger_SelectionChanged"
                    SelectedIndex="0"
                    >
                <ComboBoxItem Content="中文"/>
                <ComboBoxItem Content="English"/>
            </ComboBox>
        </StackPanel>

        <StackPanel Orientation="Horizontal" Grid.Row="2">
            <!--为了测试代码获取文本信息,Text不绑定-->
            <TextBlock x:Name="textblock_currLang" Text="当前语言:" FontSize="20" Foreground="Red" HorizontalAlignment="Left" VerticalAlignment="Center"/>
            <TextBlock x:Name="textblock_langInfo" Text="{Binding Path=SelectionBoxItem, ElementName=combobox_langChanger}"  FontSize="20" Foreground="Blue" HorizontalAlignment="Left" VerticalAlignment="Center"/>
        </StackPanel>
    </Grid>
</Grid>

5.  封装一个类(International),用于实时修改程序当前显示语言:

static public class International
{
    public static ResourceDictionary mResourceDictionaryLanguage = null;

    static public void SetCurrentLanguage(string lang="")
    {
        string currentLang = "zh-CN";

        if (string.IsNullOrEmpty(lang))
        {
            try
            {
                //mCurrentLang = Dispatcher.Thread.CurrentCulture.Name;//获取当前系统语言
                currentLang = Thread.CurrentThread.CurrentCulture.Name;
                if (string.IsNullOrEmpty(currentLang))
                {
                    currentLang = "en-US";
                }
            }
            catch (System.Exception ex)
            {
                currentLang = "en-US";
            }
        }
        else
        {
            currentLang = lang;
        }

        string currlang = "Lang_";
        currlang += currentLang;
        string langType = ConfigurationManager.AppSettings.Get(currlang);

        if (!string.IsNullOrEmpty(langType))
        {
            if (App.Current.Resources.MergedDictionaries.Contains(mResourceDictionaryLanguage))
            {
                App.Current.Resources.MergedDictionaries.Remove(mResourceDictionaryLanguage);
            }
        }

        mResourceDictionaryLanguage = new ResourceDictionary() { Source = new Uri(langType, UriKind.RelativeOrAbsolute) };

        App.Current.Resources.MergedDictionaries.Add(mResourceDictionaryLanguage);
    }

    static public string GetString(string key)
    {
        string value=key;

        try
        {
            value = App.Current.FindResource(key).ToString();
        }
        catch (Exception ex)
        {
                
        }

        return value;
    }
}

6. 在App.xaml.cs中重写OnStartup()方法:

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            International.SetCurrentLanguage();
        }
    }

7. 处理ComboBox的选中事件:

private void combobox_langChanger_SelectionChanged(object sender, SelectionChangedEventArgs e)
        { 
            string strvalue="";

            ComboBox cb = sender as ComboBox;
            string lang = "";
            if (0 == cb.SelectedIndex)
            {
                lang = "zh-cn";
            }
            else
            {
                lang = "en-us";
            }

            International.SetCurrentLanguage(lang);

            strvalue = International.GetString("LanguageCurrent");
            if (null != textblock_currLang)
            {
                textblock_currLang.Text = strvalue;
            }
        }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值