WPF 實現多語言支持

WPF 多語言有各類實現方式。如 https://www.codeproject.com/Articles/35159/WPF-Localization-Using-RESX-Files,後來發現這個不夠直接和簡潔,在看到這裏 https://www.cnblogs.com/yang-fei/p/4854460.html 這個朋友的實現以爲蠻不錯的。html

目前我以爲最好的一種或者說最適合個人一種是利用資源字典文件來作--- 就是吧各個語言建立爲一個個資源字典文件,在程序啓動的時候將選定的一種語言的資源字典文件做爲當前的資源文件。ide

注意點【WPF中實現先登陸後啓動主程序的方法】 這個文章幫助我解決了在啓動主窗口前啓動語言選擇窗口供用戶選擇肯定一種語言,當關閉這個語言窗口後發現主窗口也關閉了。。。解決方法在那片文章裏,能夠這樣:測試


public App() {ui

App.Current.ShutdownMode= ShutdownMode.OnExplicitShutdown;//防止關閉主窗口spa


}code

 

 

先建立2個資源字典文件放入Resources\Language 文件夾。xml

EN.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">

    <!-- String resource that can be localized -->
    <system:String x:Key="Greeting">en-US Message</system:String>
    <system:String x:Key="Message">hello english message</system:String>
    <system:String x:Key="mainWindTitle">title english</system:String>
    <system:String x:Key="MainWindow_mnu1">Menu item</system:String>
    <system:String x:Key="MainWindow_mnu1_sub">Sub Menuitem</system:String>

</ResourceDictionary>

  

 

ZH.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">

    <!-- String resource that can be localized -->
    <system:String x:Key="Greeting">測試字符串</system:String>
    <system:String x:Key="Message">hello 測試的消息</system:String>
    <system:String x:Key="mainWindTitle">標題中文的</system:String>

    <system:String x:Key="MainWindow_mnu1">菜單111</system:String>
    <system:String x:Key="MainWindow_mnu1_sub">子菜單</system:String>

</ResourceDictionary>

  

在APP裏包含進來建立的兩個語言資源字典文件:htm

<Application x:Class="MultiLanguangeTest.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="Resources\Language\ZH.xaml" />  
                <ResourceDictionary Source="Resources\Language\EN.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

  

APP裏代碼:blog

 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace MultiLanguangeTest
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {



        public App() {

            App.Current.ShutdownMode= ShutdownMode.OnExplicitShutdown;//防止關閉主窗口

        
        }




        protected override void OnStartup(StartupEventArgs e)
        {

            //啓動主窗口以前先啓動一個語言選擇窗口
            string lang = "";
            SelectLangWind wind = new SelectLangWind();
            wind.ShowDialog();
            lang = wind.lang;

            ResourceDictionary dict = new ResourceDictionary();
           // dict.Source = new Uri(@"Resources\Language\EN.xaml", UriKind.Relative);

            dict.Source = new Uri(@"Resources\Language\"+lang+".xaml", UriKind.Relative);
            Application.Current.Resources.MergedDictionaries.Clear();
            Application.Current.Resources.MergedDictionaries.Add(dict);
            Application.Current.Resources.MergedDictionaries[0] = dict;


          //  base.OnStartup(e);
        }



    }


 

}

  

 

 

主窗口代碼:ip

<Window x:Class="MultiLanguangeTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="{DynamicResource mainWindTitle}" Height="350" Width="525">
    <Grid>
        <Menu Height="33" VerticalAlignment="Top">
            <MenuItem Header="{DynamicResource MainWindow_mnu1}">
                <MenuItem Header="{DynamicResource MainWindow_mnu1_sub}">
                </MenuItem>
            </MenuItem>

            <MenuItem Header="{DynamicResource MainWindow_mnu1}"></MenuItem>
        </Menu>
        <Grid>
            
   
        <TextBlock Text="{DynamicResource Greeting}" Margin="0,30,0,0"/>
            <Button VerticalAlignment="Top" Content="{DynamicResource Greeting}" Width="213" Height="35" Click="SwitchButton_Click" Margin="141,132,0,0" HorizontalAlignment="Left"/>
        </Grid>

    </Grid>
</Window>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 MultiLanguangeTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private string _currentLan = string.Empty;

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
            _currentLan = "EN";
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;//回覆退出模式
        }


        private void SwitchButton_Click(object sender, RoutedEventArgs e)
        {
            string message = TryFindResource("Message") as string;

            MessageBox.Show(message);

            // TODO: 切換系統資源文件
            ResourceDictionary dict = new ResourceDictionary();

            if (_currentLan == "ZH")
            {
                dict.Source = new Uri(@"Resources\Language\EN.xaml", UriKind.Relative);

                _currentLan = "EN";
            }
            else
            {
                dict.Source = new Uri(@"Resources\Language\ZH.xaml", UriKind.Relative);
                _currentLan = "ZH";
            }


              Application.Current.Resources.MergedDictionaries.Clear();
             Application.Current.Resources.MergedDictionaries.Add(dict);
             Application.Current.Resources.MergedDictionaries[0] = dict;



        }

    }
}

  

 

貼上選擇語言的窗口代碼:

 

<Window x:Class="MultiLanguangeTest.SelectLangWind"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SelectLangWind" Height="234.231" Width="447.692">
    <Grid>
        <ComboBox Name="comLang" HorizontalAlignment="Left" Margin="153,78,0,0" VerticalAlignment="Top" Width="120">
            <ComboBoxItem  Content="English" Tag="EN"/>
            <ComboBoxItem  Content="簡體中文" Tag="ZH"/>

        </ComboBox>
        <Button Content="OK" HorizontalAlignment="Left" Margin="159,134,0,0" VerticalAlignment="Top" Width="114" Height="24" Click="btnOK_Click"/>
        <Label Content="choose language" HorizontalAlignment="Left" Margin="153,36,0,0" VerticalAlignment="Top" Width="138"/>

    </Grid>
</Window>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;

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

        public string lang = "EN";
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (comLang.SelectedIndex < 0) return;
            lang = ""+(comLang.SelectedItem as ComboBoxItem).Tag;
            Close();

        }



    }
}

  

 

 

源代碼: https://files.cnblogs.com/files/wgscd/MultiLanguangeTest.zip

相關文章
相關標籤/搜索