怎样实现wpf prism module的国际化和本地化?

english | 简体中文

上一篇有简单介绍主工程的国际化,使用的资源字典(xaml)实现的。
这几天我添加了几个prism模块(module),发现子模块使用资源字典的方式实现国际化和本地化不好做,没有找到比较好的参考文章,所以换了一种方式,使用资源文件实现了。

一、本文概述

子模块的国际化和本地化要求:

    1. 各模块需要有自己单独的语言文件。
    1. 在主工程中动态切换语言时,子模块也需要跟着切换。
    1. 使用了prism实现模块化框架,即要求主工程与各子模块不能有引用关系,即松耦合,不能直接在主工程中切换子模块的语言文件。

基于上面的要求,我尝试在各模块(module)中也定义了语言文件(xaml),主窗体切换语言时,加载模块语言文件老是提示不存在对应的资源字典文件,我恼火呀,后面还是参考“accelerider.windows”国际化的方式,使用资源文件实现本地化和国际化了,不纠结xaml的方式了,唉。

下面是修改后的效果:

和上一版异同:

    1. 标题栏国际化无变化,只是文字绑定换了种方式,实现效果一致。
    1. 左侧添加了三个子模块(home\client\server),使用prism动态加载的,并且跟随主工程主窗体语言切换而切换语言。

下面简单介绍怎么创建模块,以及主窗体和模块国际化怎么做的,真的是很简单的介绍,具体的实现可以拉代码看看。

二、 添加三个prism模块(module)

可安装prism模板,快速创建模块工程,当然手工创建.net core工程也是可以的,就是多了几个步骤而已(需要用nuget安装prism.wpf包(7.2.0.1422)),我使用得的prism模板快速创建的。

2.1 创建模块之前的准备工作

下载上图搜索到的prism模板,重启vs,它会自动安装,新建项目时,就有prism模块模板选择了:

注意要选择.net core 3的版本,因为我是使用.net core创建的wpf项目。

2.2 创建模块

下面是已经创建好的三个模块工程截图:

目前三个模块文件组织结构类似:

  • i18nresources:资源文件夹,放3个语言资源文件和一个t4模板文件(用于引用语言key),其中t4模板文件在3个模块和主工程中定义是一样的,具体可从github下载源码查看。
  • views放置视图文件,现在只使用到了主工程主窗体中显示的tabitem视图,即maintabitem.xaml,继承自tabitem。
  • xxxxmodule.cs:prism模板定义文件,prism发现模块使用。

三个模块关键点需要注意:

    1. 编辑模块工程文件,修改模块文件输出目录:
// 省略部分代码,下面这一行设置为false,代表输出目录不带.net core版本信息
<appendtargetframeworktooutputpath>flase</appendtargetframeworktooutputpath>
// 省略部分代码,修改debug与release编译输出目录,方便主工程统一加载模块
<propertygroup condition="'$(configuration)|$(platform)'=='debug|anycpu'">
  <outputpath>..\build\debug\modules</outputpath>
</propertygroup>
<propertygroup condition="'$(configuration)|$(platform)'=='release|anycpu'">
  <outputpath>..\build\release\modules</outputpath>
</propertygroup>  
// 省略部分代码
    1. xxxmodule中需要将资源文件的resourcemanager引用添加到另一个库中保存,待切换语言时需要使用,如在homemodule的构造函数中添加代码如下,只添加这一句代码就好,模块的国际化及本地化就算完事了:
i18nmanager.instance.add(terminalmacs.home.i18nresources.uiresource.resourcemanager);
    1. xxxmodule的registertypes方法中注册视图”maintabitem”到”regionnames.maintabregion”,主窗体使用”regionnames.maintabregion”关联模块视图显示加载。
_regionmanager.registerviewwithregion(regionnames.maintabregion, typeof(maintabitem));
    1. ui控件国际化文字绑定,其中markup使用的一个开源库命名空间,后面会给出链接,本项目直接将该库加载进了解决方案中;i18nresources:language即t4模板文件生成的类,关联文字翻译的key。绑定文字部分代码如下:
<textblock grid.row="2" text="{markup:i18n {x:static i18nresources:language.maintabitm_header}}"

三、 主工程

主工程目录组织结构如下:

3.1 动态加载prism模块

配置加载3个模块的关键代码在app.xaml.cs文件中,看上面的代码,我将三个模块输出到了modules目录下,主工程直接加载此目录即可,其他加载方式还有使用配置文件等,可以参考prism官方例子,文末给出链接:

protected override imodulecatalog createmodulecatalog()
{
    string modulepath = @".\modules";
    if (!directory.exists(modulepath))
    {
        directory.createdirectory(modulepath);
    }
    return new directorymodulecatalog() { modulepath = modulepath };
}

主窗体显示子模块注册的tabitem视图,prism:regionmanager.regionname即在各子模块中注册过的区域字符串,他与模块对应的tabitem视图关联,代码如下:

<tabcontrol grid.columnspan="2" selectedindex="0"
    style="{staticresource maintabcontrolstyle}" 
    itemcontainerstyle="{staticresource maintabitemstyle}"
    prism:regionmanager.regionname="{x:static ui:regionnames.maintabregion}"/>

主窗体以tabcontrol的控件形式展示子模块视图:

主工程要能正常加载子模块,主工程的工程文件也需要修改其输出目录:

// 省略部分代码,下面这一行设置为false,代表输出目录不带.net core版本信息
<appendtargetframeworktooutputpath>flase</appendtargetframeworktooutputpath>
// 省略部分代码,修改debug与release编译输出目录,方便主工程统一加载模块
<propertygroup condition="'$(configuration)|$(platform)'=='debug|anycpu'">
  <outputpath>..\build\debug</outputpath>
</propertygroup>
<propertygroup condition="'$(configuration)|$(platform)'=='release|anycpu'">
  <outputpath>..\build\release</outputpath>
</propertygroup>  
// 省略部分代码

3.2 修改语言文件格式

删除了原有的xaml语言文件,替换为resx的资源文件,和三个模块的资源文件类型类似,下面是主工程的资源文件:

替换成资源文件,编辑是要比xaml文件要方便点,起初是有考虑使用资源文件实现国际化的,作死想尝试xaml文件。

3.3 语言切换核心代码

动态切换语言的关键代码改为:

public static void setlanguage(string language = "")
{
    if (string.isnullorwhitespace(language))
    {
        language = confighelper.readkey(key_of_language);
        if (string.isnullorwhitespace(language))
        {
            language = system.globalization.cultureinfo.currentculture.tostring();
        }
    }

    confighelper.setkey(key_of_language, language);
    _lastlanguage = language;

    var culture = new system.globalization.cultureinfo(language);
    i18nmanager.instance.currentuiculture = culture;
}

核心的语言切换代码是最后一句,不详细说了,解决方案中有库、有源码:

i18nmanager.instance.currentuiculture = culture;

四. 源码

    1. 源码地址,欢迎star:https://github.com/dotnet9/terminalmacs/tree/master/src/terminalmacs.manager/terminalmacs.managerforwpf
    1. 官方网站:
    1. 合作网站:

五. 参考资料

    1. prism template pack(prism模板):https://marketplace.visualstudio.com/items?itemname=brianlagunas.prismtemplatepack
    1. wpf国际化开源辅助库:https://github.com/dingpingzhang/wpfextensions
    1. accelerider.windows(子模块加载参考开源项目):https://github.com/accelerider/accelerider.windows
    1. prism-samples-wpf(官方demo):https://github.com/prismlibrary/prism-samples-wpf