开发者

How should I Implement Localization on Wpf Application using MVVM pattern?

开发者 https://www.devze.com 2023-04-08 11:09 出处:网络
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 justfor the View.

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 b开发者_如何学编程ut LocBaml is not looking helpful for converting these error messages and message boxes message in other languages. How can I accomplish this?


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.


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.


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 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")


You can take a look at some software I wrote to do this:

http://tap-source.com/?p=232

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号