From: http://compositeextensions.codeplex.com/discussions/52910?ProjectName=compositeextensions
Microsoft encourages you to use their tool “LocBaml” for localizing WPF applications (see MSDN). Unfortunately, this tool is still not production-ready and it has some serious disadvantages when you have to sign the assemblies of your application.
I prefer the usage of the “old” ResX files for localizing my WPF applications. They are very well supported by Visual Studio and I like the fallback strategy of the resources system when a resource item isn’t found.
How to use a ResX file in a WPF application:
- Create a Resource (ResX) file in your project or you can reuse the Properties/Resources.resx file.
- In the Visual Studio Resource designer you have to change the Access Modifier from Internal to Public. This can be found in the top toolbar of the designer.
- Add the namespace to your XAML View:
xmlns:p="clr-namespace:Jbe.TestSuite.Infrastructure.Shell.Properties"
- The Resources can be accessed via the x:Static markup extension of XAML:
<MenuItem Header="{x:Static p:Resources.FileMenu}">
What about Images?
If you have the need to localize images then it definitely gets trickier. The ResX files support only the native bitmap representation of images whereas WPF uses a managed version called BitmapSource. Therefore, you have to convert a GDI “Bitmap” object into a “BitmapSource” object. The CompositeExtensions library contains the method CreateBitmapSource in the class ResourceService that does the trick.
public static BitmapSource CreateBitmapSource(System.Drawing.Bitmap bitmap)
{
return Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero,
new Int32Rect(0, 0, bitmap.Width, bitmap.Height), BitmapSizeOptions.FromEmptyOptions());
}
How to configure the application language?
By default the .NET Framework uses the language of the operating system. I believe this behavior should be fine for most customers. However, some customers or software testers like to define the language for the application independent of the operating system settings.
I use the Settings designer that Visual Studio offers to implement the language configuration. In the Settings designer I just define a Culture and UICulture string property. One of the first lines when the application start calls this method:
private void InitializeCultures()
{
if (!string.IsNullOrEmpty(Settings.Default.Culture))
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(Settings.Default.Culture);
}
if (!string.IsNullOrEmpty(Settings.Default.UICulture))
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Settings.Default.UICulture);
}
}
The code snippet above reads the culture settings from the configuration file and sets it for the whole application. Now you can modify the “App.config” file when you need to start the application with another language.
Don’t get confused with CurrentCulture and CurrentUICulture. These two properties represent the “Regional and Language Options” which can be found in the Windows Control Panel.
- CurrentCulture represents the “Format” tab of the “Regional and Language Options”. This setting is responsible for formatting and parsing of values.
- CurrentUICulture represents the second part of the “Keyboards and Languages” tab, the “Display Language”. This property controls which language of the resources are loaded and shown to the user.
Note: This discussion is only about CurrentUICulture.
Part 2 discusses the usage of CurrentCulture in WPF applications.
Code Download: http://compositeextensions.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27102
or Code Download