WPF實現無刷新動態切換多語言(國際化)


1. 在WPF中國際化使用的是 .xaml文件的格式

      如圖:Resource Dictionary (WPF)

      

2. 創建默認的語言文件和其他語言文件

       這里以英語為默認語言,新建一個 Resource Dictionary (WPF)文件,並命名為DefaultLanguage.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="OK">
        OK
    </sys:String>
    <sys:String x:Key="Cancel">
        Cancel
    </sys:String>
</ResourceDictionary>

   默認語言文件的 BuildAction要設置為 Page,如圖:

       

       為了便於管理,一般將所有的語言文件都放在一個目錄下,這里創建lang目錄,

       然后在創建另一個語言文件,這里是中文,命名為 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="OK">
        確定
    </sys:String>

    <sys:String x:Key="Cancel">
        取消
    </sys:String>
</ResourceDictionary>

       其他非默認語言的設置應該如下:

       BuildAction設置為:Content ;CopyToOutputDirectory設置為:Copy if newer (先這樣做吧,原因未清)

        

3.在App.xaml中配置默認語言:

<Application x:Class="LanTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary><!--這個節點就是配置默認語言的-->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="lang\DefaultLanguage.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

4.實際使用(敲代碼了) 

     4.1. 界面效果如下:

           

    4.2. 界面的.xaml代碼

 1 <Window x:Class="LanTest.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="350" Width="525">
 5     <Grid>
 6         <!--這里的{DynamicResource OK}就是動態調用 資源中的key為OK的內容-->
 7         <Button Content="{DynamicResource OK}" HorizontalAlignment="Left" Margin="134,161,0,0" VerticalAlignment="Top" Width="104" Height="38"/>
 8         <Button Content="{DynamicResource Cancel}" HorizontalAlignment="Left" Margin="278,161,0,0" VerticalAlignment="Top" Width="100" Height="38"/>
 9         <Button Content="Button" HorizontalAlignment="Left" Margin="287,59,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" Loaded="Button_Loaded"/>
10         <ComboBox Name="cbLang" HorizontalAlignment="Left" Margin="118,59,0,0" VerticalAlignment="Top" Width="120">
11         </ComboBox>
12 
13     </Grid>
14 </Window>
View Code

    4.3. 后台邏輯代碼:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Globalization;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Windows;
 8 using System.Windows.Controls;
 9 using System.Windows.Data;
10 using System.Windows.Documents;
11 using System.Windows.Input;
12 using System.Windows.Media;
13 using System.Windows.Media.Imaging;
14 using System.Windows.Navigation;
15 using System.Windows.Shapes;
16 
17 namespace LanTest
18 {
19     /// <summary>
20     /// Interaction logic for MainWindow.xaml
21     /// </summary>
22     public partial class MainWindow : Window
23     {
24         public MainWindow()
25         {
26             InitializeComponent();
27         }
28 
29         //定義ComboBox選項的類,存放Name和Value
30         public class CategoryInfo
31         {
32             public string Name
33             {
34                 get;
35                 set;
36             }
37             public string Value
38             {
39                 get;
40                 set;
41             }
42 
43         }
44 
45         //切換語言
46         private void btnChangeLang_Click(object sender, RoutedEventArgs e)
47         {
48             object selectedName = cbLang.SelectedValue;
49             if (selectedName != null)
50             {
51                 string langName = selectedName.ToString();
52                 //英語的語言文件名為:DefaultLanguage,所有這里要轉換一下
53                 if (langName == "en_US")
54                     langName = "DefaultLanguage";
55                 //根據本地語言來進行本地化,不過這里上不到
56                 //CultureInfo currentCultureInfo = CultureInfo.CurrentCulture;
57 
58                 ResourceDictionary langRd = null;
59                 try
60                 {
61                     //根據名字載入語言文件
62                     langRd = Application.LoadComponent(new Uri(@"lang\" + langName + ".xaml", UriKind.Relative)) as ResourceDictionary;
63                 }
64                 catch(Exception e2)
65                 {
66                     MessageBox.Show(e2.Message);
67                 }
68 
69                 if (langRd != null)
70                 {
71                     //如果已使用其他語言,先清空
72                     if (this.Resources.MergedDictionaries.Count > 0)
73                     {
74                         this.Resources.MergedDictionaries.Clear();
75                     }
76                     this.Resources.MergedDictionaries.Add(langRd);
77                 }
78             }
79             else
80                 MessageBox.Show("Please selected one Language first.");
81         }
82 
83         //控件載入時,為ComboBox賦值
84         private void cbLang_Loaded(object sender, RoutedEventArgs e)
85         {
86             List<CategoryInfo> categoryList = new List<CategoryInfo>();
87             categoryList.Add(new CategoryInfo() { Name = "English", Value = "en_US" });
88             categoryList.Add(new CategoryInfo() { Name = "中文", Value = "zh_CN" });
89             
90             cbLang.ItemsSource = categoryList;//綁定數據,真正的賦值
91             cbLang.DisplayMemberPath = "Name";//指定顯示的內容
92             cbLang.SelectedValuePath = "Value";//指定選中后的能夠獲取到的內容
93         }
94     }
95 }
View Code

 

 

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM