MVVM符合在WPF应用程序本地化 [英] MVVM conform localization in WPF Applications

查看:115
本文介绍了MVVM符合在WPF应用程序本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以本地化使用MVVM模式的WPF应用程序?我真的想这样做的正确的方式。

How can I localize an WPF Application using the MVVM Pattern? I really want to do it the "right" way.

我目前的做法是,我使用的.resx资源文件本地化我的申请。

My current approach is that I use .resx Resource files to localize my application.

我列入他们在我的XAML代码

I included them in my xaml code

xmlns:localization="clr-namespace:ClientLibTestTool.ViewLanguages"

和访问它们是这样的:

        <Button x:Name="BtnGenerate" 
                Content="{x:Static localization:localization.ButtonGenerate}"/>



我的问题:

My Questions:


  1. 有没有更好的办法做到这一点?

  2. 如何测试不同的语言(用不同的语言设置负载应用程序)?

  3. 是否有可能在运行时改变语言

回答:

问题1:

问题2:(谢谢你,斯泰恩)

Question 2: (Thank you, stijn)

public MainWindow()
{
    // Debug Settings
    localization.Culture = CultureInfo.GetCultureInfo("en-US");
    // localization.Culture = CultureInfo.GetCultureInfo("de-DE");
    this.InitializeComponent();
}



问题3:(谢谢你,斯泰恩)

Question 3: (Thank you, stijn)

不是真的,有必要重新绘制完整的窗口。

Not really, it is necessary to redraw the complete window.

推荐答案

这是适当的方式要做到这一点,就我而言。
切换语言,改变了国产化类使用的文化:

This is the appropriate way to do it, as far as I'm concerned. To switch languages, change the culture used by the localization class:

localization.Culture = CultureInfo.GetCultureInfo( "de-DE" );



由于所有的字符串是在运行时获取的(在生成的Designer.cs文件中的所有通话的样子 ResourceManager.GetString(SomeString,resourceCulture); resourceCulture 是怎样得到由上面的呼叫建立,这会影响你在运行时获取,但supose您使用菜单项等值从XAML中,你必须重建整个菜单此操作才会生效。

Since all strings are fetched at runtime (all calls in the generated Designer.cs files look like ResourceManager.GetString( "SomeString", resourceCulture ); and resourceCulture is what gets set by the call above, this affects what you get at runtime. However supose you use the values in menu items etc from within xaml, you have to rebuild the entire menu before this takes effect.

这篇关于MVVM符合在WPF应用程序本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆