Can you recommend a good way to implement a Multilanguage system for a WPF app? The method I'm using right now involves XML, classes and a xaml extension. It Works fine in most of cases, but when I have to deal with dynamic labels or dynamic text in general it require some extra effort. I would like to let the programmer working only in the main problem and forgot the lang issues.
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- Xamarin. The name 'authorEntry does not exist
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
Follow these steps:
1) Place all
String
fragments in a separate resource file.Example:
StringResources.xaml
:2) Make copies for each language and add them (translated) to the merged dictionaries. Don't forget to add the country's ISO code to make things easier.
Example
App.xaml
:The last resource file with strings will be used to replace text parts in code.
3a) Use the text parts from the
String
table:Example
Window1.xaml
:3b) Load the resource from code (Only use this code if you don't want to set via
XAML
):4) Switch to new culture at start of application:
Codesnippet from
App.xaml.cs
:Josh Smith wrote an in-depth tutorial about his preferred method for this: Creating an Internationalized Wizard in WPF.
It might point you towards a big redesign (it's a MVVM solution), but using MVVM seems worth it for other reasons as well.
I am using the WPF Localization Extension. It is a really easy way to localize any type of
DependencyProperty
onDependencyObject
s.Text = {LocText ResAssembly:ResFile:ResKey}
INotifyPropertyChanged
for advanced use"this is the '{0}' value"
LocText
extension).resx
) across all assemblies (also the dynamic loaded one at runtime)TypeConverter
) for it exists (extendsLocalizeExtension
)Text
, upperText
, lowerText
,Image
s,Brush
es,Double
andThickness
UID
property untouchedSpecificCulture
to use asIFormatProvider
(e.g.(123.20).ToString(LocalizeDictionary.SpecificCulture) = "123.20"
or"123,20"
)Thread.CurrentCulture
orThread.CurrentUICulture
(can be changed easily)Using this article I've managed to easily use resource files to handle multilingual WPF windows. http://www.codeproject.com/KB/WPF/WPF_Resx_Localization.aspx You should give it a check because it's really simple and effective.