How can I Localizing strings in code in WPF
Basically, what do I have to do to Localize something like this:
MessageBox.Show("Hello");
So depending on the culture it should show "Ciao", "Hola", "Bonjour"
, but this has to be done in the code, can't move it to the GUI.
I know how to localize the GUI (with LocBaml
), but that doesn't work with what I need, but I can imagine is something similar to what LocBaml
does (expect the part where it generates an assembly maybe). I just need a pinpoint in the correct direction.
Thanks!
If your strings are already localized using resource dictionaries, you can use just
MessageBox.Show((string)Application.Current.Resources["IDS_HELLO"]);
This approach has however a small disadvantage that the text won't change automatically if the application's language is changed during the messagebox.Show
. (Mind that the dynamic localization requires anyway additional effort from your side.)
精彩评论