1. 程式人生 > >WPF 多國語言 localization

WPF 多國語言 localization

因為專案是多國語言的,所以用到了wpf的多國語言相關知識,其實也比較簡單,分享給大家,自己也做個備忘。

基本步驟如下:

1.讓介面上所有能夠出現的label都使用動態資源(DynamicResource)這樣我們在切換語言的時候程式介面上的字元才會自動切換,這裡我們舉個簡單的TextBlock的例子,原本也許你會直接寫

<TextBlock Text="選取語言" Width="150" VerticalAlignment="Center"/>

但是如果需要國際化,那麼你需要這樣寫

<TextBlock Text="{DynamicResource SELECT_LANG}" Width="150" VerticalAlignment="Center"/>

把原本hard code在程式裡面的字元扣出來,當做動態資源匯入。

2.將所用到的字元資源寫進資源字典(ResourceDictionary)中,有幾種語言就寫成幾個資源字典,一般我們會放在lang資料夾下面

例如英文的資源字典叫做en-us.xaml

<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">
    <s:String x:Key="WINDOW_TITLE">Localization Demo</s:String>
    <s:String x:Key="SELECT_LANG">Select Language</s:String>
    <s:String x:Key="TEST_LABLE">Test Lable</s:String>
    <s:String x:Key="TEST">Test</s:String>
    
</ResourceDictionary>

同樣的中文字典檔案叫做zh-cn.xaml

<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">
    <s:String x:Key="WINDOW_TITLE">國際化演示</s:String>
    <s:String x:Key="SELECT_LANG">選取語言</s:String>
    <s:String x:Key="TEST_LABLE">測試文字</s:String>
    <s:String x:Key="TEST">測試</s:String>

</ResourceDictionary>

3.在應用程式級的App.xaml中指定預設的資源字典(預設語言)
<Application x:Class="LocalizationDemo.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="pack://application:,,,/lang/en-us.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

ok這樣所有的準備工作都已經做好

要做的就是在需要切換的時候把資源字典換掉即可(這裡色combobox選擇不同的語言檔案後切換語言)

private void LangList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.LangList.SelectedIndex == 0)
            {
                LoadLanguageFile("pack://application:,,,/lang/en-us.xaml");
            }
            else
            {
                LoadLanguageFile("pack://application:,,,/lang/zh-cn.xaml");
            }
        }

        void LoadLanguageFile(string languagefileName)
        {
            Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary()
            {
                Source = new Uri(languagefileName, UriKind.RelativeOrAbsolute)
            };
        }


前臺ui

<Window x:Class="LocalizationDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="{DynamicResource WINDOW_TITLE}" 
        WindowStartupLocation="CenterScreen"
        Height="350" Width="525">
    <StackPanel Margin="20">
        <StackPanel Orientation="Horizontal" Height="25">
            <TextBlock Text="{DynamicResource SELECT_LANG}" Width="150"  VerticalAlignment="Center"/>
            <ComboBox x:Name="LangList" Width="200"  
                      SelectionChanged="LangList_SelectionChanged"
                      SelectedIndex="0"  VerticalAlignment="Center">
                <ComboBoxItem >English</ComboBoxItem>
                <ComboBoxItem >中文</ComboBoxItem>
            </ComboBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Height="25">
            <TextBlock Text="{DynamicResource TEST_LABLE}" Width="150"  VerticalAlignment="Center"/>
            <TextBlock Text="{DynamicResource TEST}" Width="150"  VerticalAlignment="Center"/>
        </StackPanel>
    </StackPanel>
</Window>

code behind程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LocalizationDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void LangList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.LangList.SelectedIndex == 0)
            {
                LoadLanguageFile("pack://application:,,,/lang/en-us.xaml");
            }
            else
            {
                LoadLanguageFile("pack://application:,,,/lang/zh-cn.xaml");
            }
        }

        void LoadLanguageFile(string languagefileName)
        {
            Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary()
            {
                Source = new Uri(languagefileName, UriKind.RelativeOrAbsolute)
            };
        }
    }
}



這樣就可以看到程式的語言動態切換的效果嘍