WPF 多语言有各种实现方式。如 https://www.codeproject.com/Articles/35159/WPF-Localization-Using-RESX-Files,后来发现这个不够直接和简洁,在看到这里 https://www.cnblogs.com/yang-fei/p/4854460.html 这个朋友的实现觉得蛮不错的。

目前我觉得最好的一种或者说最适合我的一种是利用资源字典文件来做--- 就是吧各个语言创建为一个个资源字典文件,在程序启动的时候将选定的一种语言的资源字典文件作为当前的资源文件。

注意点【WPF中实现先登录后启动主程序的方法】 这个文章帮助我解决了在启动主窗口前启动语言选择窗口供用户选择确定一种语言,当关闭这个语言窗口后发现主窗口也关闭了。。。解决方法在那片文章里,可以这样:

public App() {

App.Current.ShutdownMode= ShutdownMode.OnExplicitShutdown;//防止关闭主窗口

}

先创建2个资源字典文件放入Resources\Language 文件夹。

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里包含进来创建的两个语言资源字典文件:

<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里代码:

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);
} } }

  

主窗口代码:

<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

WPF 实现多语言支持的更多相关文章

  1. WPF使用X:Static做多语言支持

    让程序支持多语言,一般把需要显示的字符串保存在一个资源类的static属性中. <!--[if !supportLists]--> <!--[endif]--> 微软的WPF程 ...

  2. dotnetcore3.1 WPF 实现多语言

    dotnetcore3.1 WPF 实现多语言 Intro 最近把 DbTool 从 WinForm 迁移到了 WPF,并更新到了 dotnet core 3.1,并实现了基于 Microsoft.E ...

  3. 分享两种实现Winform程序的多语言支持的解决方案

    因公司业务需要,需要将原有的ERP系统加上支持繁体语言,但不能改变原有的编码方式,即:普通程序员感受不到编码有什么不同.经过我与几个同事的多番沟通,确定了以下两种方案: 方案一:在窗体基类中每次加载并 ...

  4. EnumHelper.cs枚举助手(枚举描述信息多语言支持)C#

    C#里面经常会用到枚举类型,枚举是值类型对象,如果你想用枚举类型的多属性特性,或者你想在MVC页面上通过简单的值类型转换,将某字段值所代表的含义转换为文字显示,这时候必须要将枚举扩展,是它支持文本描述 ...

  5. ios调用系统相册、相机 显示中文标题、本地化多语言支持

    因为调用系统相册.相机需要显示中文,所以搞了半天才知道是在Project->info->Custom ios Target Properties 添加 Localizations 并加入C ...

  6. (视频)《快速创建网站》 3.3 国际化高大上 - WordPress多语言支持

    本文是<快速创建网站>系列的第7篇,如果你还没有看过之前的内容,建议你点击以下目录中的章节先阅读其他内容再回到本文. 访问本系列目录,请点击:http://devopshub.cn/tag ...

  7. tp 多语言支持

    tp支持多语言 通过get来改变语言的 http://localhost/tp/index.php/Admin/User/add/hl/zh-cn http://localhost/tp/index. ...

  8. iOS-生成国际化包-配置App多语言支持

      标签: ios国际化 ios多语言支持 xcode多语言支持 xcode生成多语言 国际化 it 分类: 功能知识   如果你的App需要支持多国语言.那么,就应该为你的App应用添加“国际化”支 ...

  9. Zend Framework 入门(2)—多国语言支持

    如果你的项目想要支持多语言版本,那么就需要用到 Zend_Translate.Zend_Translate 的详细文档在这里,不过如果想偷懒的话,也很简单,在View Helpers 文档中介绍了如何 ...

随机推荐

  1. 去掉img与img之间,video与video之间默认的间距(3种方式)

    img,video{ /*第1种方式*/ border:; vertical-align: bottom; /*第2种方式*/ outline-width:0px; vertical-align:to ...

  2. todo JVM笔记

    之前给自己定了很多计划,要学Dubbo,Netty,SSHM源码,Tomcat源码...这些基本浅尝辄止,很难继续研读,过不了多久就忘了. 觉得还是基础不够,所以决定把<JVM>.< ...

  3. python之路之——操作系统的发展历史

    阅读目录 手工操作 —— 穿孔卡片 批处理 —— 磁带存储和批处理系统 多道程序系统 分时系统 实时系统 通用操作系统 操作系统的进一步发展 操作系统的作用 手工操作 —— 穿孔卡片 1946年第一台 ...

  4. glup安装

    资料参考:http://www.w3ctrain.com/2015/12/22/gulp-for-beginners/ 1.在安装 node 的环境后: npm install gulp -g 全局安 ...

  5. hdu4731 Minimum palindrome (找规律)

    这道题找下规律,3个字母或者以上的时候就用abcabcabc....循环即可. 一个字母时,就是aaaaa.....; 当只有2个字母时!s[1][]=a"; s[2][]="ab ...

  6. Codeforces 920 反图联通块 线段树质因数暴力

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  7. flask-migrate的使用

    先安装flask-migrate:pip install flask-migrate 然后见代码: 输入命令生成migrates文件夹 然后可以看到项目下生成文件夹: 然后输入命令: 看到: 总之,模 ...

  8. rsync服务实践

    RSYNC数据备份 RSYNC=Remote Sync 远程同步   高效,一定要结合shell 官方网站:https://rsync.samba.org/ Author:     Andrew Tr ...

  9. 对postman的研究

    1.Postman可用作macOS,Windows和Linux操作系统的本机应用程序. 2.最常用的方法是GET,POST,PUT和DELETE.方法的名称是不言自明的.例如,GET使您可以从服务器检 ...

  10. 1. svn 简介

    参考文档: http://svndoc.iusesvn.com/ SVN的 相关网站 什么是svn?Subversion是一个“集中式”的信息共享系统.版本库是Subversion的核心部分,是数据的 ...