dotnetcore3.1 WPF 实现多语言
dotnetcore3.1 WPF 实现多语言
Intro
最近把 DbTool 从 WinForm 迁移到了 WPF,并更新到了 dotnet core 3.1,并实现了基于 Microsoft.Extensions.Localization
实现了基本的多语言支持。下面来分享一下如何来实现
服务注册
如果不熟悉如何在 WPF 中使用依赖注入,可以参考上一篇文章 dotnetcore3.1 WPF 中使用依赖注入
在应用启动前注册 Localization 服务,我这里使用的是自己自定义的基于 JSON 的多语言服务
services.AddJsonLocalization(options => options.ResourcesPathType = ResourcesPathType.CultureBased);
服务注册的最后使用了一个 ServiceLocator 模式的代码(DependencyResolver),保存了所有的注册服务,后面的 Localizer 扩展会用到
DependencyResolver.SetDependencyResolver(services);
代码文件实现方式
代码文件(如:MainWindow.xaml.cs) 中实现多语言较为简单,直接注入 IStringLocalizer
即可,获取对应的要实例化的,比如:
public partial class MainWindow: Window
{
private readonly IStringLocalizer<MainWindow> _localizer;
public MainWindow(
IStringLocalizer<MainWindow> localizer)
{
InitializeComponent();
_localizer = localizer;
}
// ...
{
// ...
MessageBox.Show(_localizer["Success"], _localizer["Tip"]);
}
}
xaml 实现方式
LocaliazerExtension
xaml 文件中使用需要自定义一个扩展,定义如下,【实现源码】
:
public class LocalizerExtension : MarkupExtension
{
private readonly IStringLocalizerFactory _localizerFactory;
public string Key { get; }
public LocalizerExtension(string key)
{
Key = key;
_localizerFactory = DependencyResolver.Current.
ResolveService<IStringLocalizerFactory>();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
var targetRootType = serviceProvider.GetType()
.GetProperty("System.Xaml.IRootObjectProvider.RootObject", BindingFlags.Instance | BindingFlags.NonPublic)
?.GetValue(serviceProvider)
?.GetType();
if (null == targetRootType)
{
targetRootType = typeof(MainWindow);
}
var localizer = _localizerFactory.Create(targetRootType);
var value = localizer[Key];
return (string)value;
}
}
这里使用到了上面提到的 ServiceLocator
模式的代码,从 DependencyResolver
获取注册的服务,感觉这里的实现需要优化,有更好想法的小伙伴还望一起交流一下,另外如果你的应用比较简单,我觉得上面代码里的通过反射获取 targetRootType
的代码可以直接使用某一个类型例如:typeof(MainWindow)
,这样会更高效
在 xaml 中使用
- 在
Window
标签中添加扩展对应的命令空间,例如:xmlns:loc="clr-namespace:DbTool.Localization"
- 在需要实现多语言的地方引用,例如:
<TextBlock Margin="0,0,4,0" Text="{loc:Localizer DbConnectionString}"></TextBlock>
- 在对应的资源文件中配置要使用多语言资源,如上面的
DbConnectionString
<Window x:Class="DbTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:loc="clr-namespace:DbTool.Localization"
mc:Ignorable="d"
Title="DbTool" Height="450" Width="800" FontSize="14">
<Grid>
<TextBlock Margin="0,0,4,0" Text="{loc:Localizer DbConnectionString}"></TextBlock>
</Grid>
</Window>
语言切换
发生语言切换时或应用启动时设置默认语言时,要更新当前线程的 Culture 信息
// set current culture
var defaultCulture = "zh";
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(defaultCulture);
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(defaultCulture);
实现效果
More
这种方式的实现,目前还需要重启之后界面的语言才会发生变化,可以进一步优化,实现动态多语言,修改语言之后界面就切换,目前不是太需要,暂时没做,有需要的可以先自己研究一下。
Reference
- https://github.com/WeihanLi/WeihanLi.Extensions.Localization.Json
- https://github.com/WeihanLi/DbTool
- https://github.com/WeihanLi/DbTool/blob/wpf-dev/DbTool/MainWindow.xaml
- https://github.com/WeihanLi/DbTool/blob/wpf-dev/DbTool/MainWindow.xaml.cs
- https://github.com/WeihanLi/DbTool/blob/wpf-dev/DbTool/Localization/LocalizerExtension.cs
dotnetcore3.1 WPF 实现多语言的更多相关文章
- dotnetcore3.1 WPF 中使用依赖注入
dotnetcore3.1 WPF 中使用依赖注入 Intro 在 ASP.NET Core 中默认就已经集成了依赖注入,最近把 DbTool 迁移到了 WPF dotnetcore 3.1, 在 W ...
- WPF 实现多语言支持
WPF 多语言有各种实现方式.如 https://www.codeproject.com/Articles/35159/WPF-Localization-Using-RESX-Files,后来发现这个 ...
- WPF国际化(多语言)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 设置 WPF 的全球化语言
https://stackoverflow.com/questions/7454024/setting-culture-en-in-globally-in-wpf-app Thread.Current ...
- WPF使用X:Static做多语言支持
让程序支持多语言,一般把需要显示的字符串保存在一个资源类的static属性中. <!--[if !supportLists]--> <!--[endif]--> 微软的WPF程 ...
- WPF 获得当前输入法语言区域
原文:WPF 获得当前输入法语言区域 本文告诉大家如何获得 WPF 输入法的语言区域 需要使用 user32 的方法,很简单,请看下面 [DllImport("user32.dll" ...
- [No000012C]WPF(4/7)类型转换器和标记扩展[译]
介绍 之前讨论了WPF的基础架构,然后逐步开始学习布局面板,转换,介绍了不同的控件,容器,UI转换等.在这篇文章中,我将讨论每个创建XAML应用前的开发人员应该了解的关于XAML最重要的东西. 标记扩 ...
- WPF入门(三)->几何图形之线条(LineGeometry)
原文:WPF入门(三)->几何图形之线条(LineGeometry) 前一章我们对wpf的xaml语言有了一定的了解,那么我们现在开始来学习如何使用wpf来画出几何图形. LineGeometr ...
- 2019-6-23-WPF-获得当前输入法语言区域
title author date CreateTime categories WPF 获得当前输入法语言区域 lindexi 2019-06-23 11:51:21 +0800 2018-10-12 ...
随机推荐
- ii
char a[10], b[10], c[10], d[10],e[10],f[10],g[10],h[10]; scanf("%s %s %s %s", a, b, c, d); ...
- Error connecting to the Service Control Manager: 拒绝访问 Mongodb问题-解决
原文地址:https://blog.csdn.net/carrot5032/article/details/74742888 发现在mongodb.log里出现 2017-07-07T17:01:5 ...
- 软件发布!DOTA2统计学
更新日志: 1.2 增加DOTABUFF作为数据源,可以与DOTAMAX切换 1.1 增加关于对话框 增加版本信息 修复列表框头的错误 受到 http://tieba.baidu.com/p/38 ...
- K8S生产环境中实践高可靠的配置和技巧都有哪些?
K8S环境中实践高可靠的配置和技巧都有哪些? 磁盘类型及大小 磁盘类型: 推荐使用ssd 磁盘 对于worker节点,创建集群时推荐使用挂载数据盘.这个盘是专门给/var/lib/docker 存放本 ...
- PKU-3580 SuperMemo(Splay模板题)
SuperMemo 题目链接 Your friend, Jackson is invited to a TV show called SuperMemo in which the participan ...
- Python 类中方法的内部变量,命名加'self.'变成 self.xxx 和不加直接 xxx 的区别
先看两个类的方法: >>> class nc(): def __init__(self): self.name ='tester' #name变量加self >>> ...
- 浏览器无法进入GitHub(已解决)
时间:2020/1/22 今天突然chrome登不上GitHub,一直出现响应时间过长的问题,如下: 开始还以为是GitHub服务器出问题了(虽然概率很小.....),但这种情况一直持续了几个小时,我 ...
- react中,路由的使用。import {BrowserRouter,Switch,Route} from "react-router-dom";
import React from "react"; import ReactDom from "react-dom"; import {BrowserR ...
- 设计模式-05建造者模式(Builder Pattern)
1.模式动机 比如我们要组装一台电脑,都知道电脑是由 CPU.主板.内存.硬盘.显卡.机箱.显示器.键盘和鼠标组成,其中非常重要的一点就是这些硬件都是可以灵活选择,但是组装步骤都是大同小异(可以组一个 ...
- struts.xml头部代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...