我应该如何使用 MVVM 模式在 Wpf 应用程序上实现本地化?
我正在使用 Devexpress 工具并遵循 MVVM 模式开发一个 wpf 应用程序。
我已经使用 Locbaml 工具对其应用了本地化,它工作得很好,但仅适用于视图。
在此应用程序中,我设置网格行验证错误,并从视图模型中弹出一些消息框,但 LocBaml 看起来对于将这些错误消息和消息框消息转换为其他语言没有帮助。我怎样才能做到这一点?
I am working on a wpf application using Devexpress Tool and following MVVM pattern..
I have applied Localization on it using Locbaml Tool and it is working perfectly fine but just for the View.
In this app i am setting Grid Row Vlidation errors and also Popping some MessageBoxes from View Model but LocBaml is not looking helpful for converting these error messages and message boxes message in other languages. How can I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
LocBaml 仅从 baml 中提取内容,baml 是 xaml 文件的编译形式。任何未在 xaml 中定义的内容(例如,在代码隐藏中定义的字符串)将永远不会被它看到。据我所知,解决此问题的唯一方法是在 xaml 中定义您可能想要本地化为字符串资源的所有字符串。这些可以很容易地从代码隐藏中引用,所以它并不像听起来那么糟糕。完全动态生成的字符串无法本地化,但通过一些工作,您可以从 xaml 资源中定义的代码片段构造它们。
LocBaml only extracts stuff from baml, which is the compiled form of your xaml files. Anything not defined in xaml (eg, strings defined in code-behind) will never be seen by it. The only way around this that I'm aware of is to define all the strings you might want to localize as string resources in xaml. These can easily be referenced from code-behind, so it's not as bad as it sounds. Strings that are entirely dynamically generated won't be localizable, but with some work you can construct them from snippets defined in your xaml resources.
直接从 ViewModel 获取消息框是错误的。相反,您应该引发事件并让视图执行 UI。否则,您无法对 ViewModel 进行单元测试,而这是 MVVM 模式的主要目标之一。
It's wrong to have messageboxes directly from the ViewModel. Instead you should raise events and let the view do the UI. Otherwise you couldn't unit test the ViewModel, and that's one of the main goals of the MVVM pattern.
如果您使用 .resx 文件来管理翻译,您只需让它们生成代码(在 .resx 屏幕的组合框中使用访问修饰符:公共),然后让 VM 将消息直接发送到视图。
这样,代码生成的资源文件的底层功能将返回所需文本的翻译版本。
If you are using .resx files to manage you translations, you can simply make them generate code (with Access Modifier: Public in the combo box at the .resx screen) and then make the VM send the messages directly to the view.
That way the underlying functionality of code-generated resource files will return the translated version of the desired text.
http://blogs.microsoft.co.il/blogs/tomershamam/archive/2007/10/30/wpf-localization-on-the-fly-language-selection.aspx 检查这。您可以通过从代码中调用翻译方法来从VM发送本地化文本。例如
LanguageDictionary.Current.Translate("resourceKey","value Name")
http://blogs.microsoft.co.il/blogs/tomershamam/archive/2007/10/30/wpf-localization-on-the-fly-language-selection.aspx check this. You can send localized text from VM by calling the translate method from the code. For example
LanguageDictionary.Current.Translate("resourceKey","value Name")
您可以看一下我为此编写的一些软件:
http://tap-source.com/ ?p=232
希望这有帮助。
You can take a look at some software I wrote to do this:
http://tap-source.com/?p=232
Hope this helps.