Localize a WPF application

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:

  1. Create a Resource (ResX) file in your project or you can reuse the Properties/Resources.resx file.
  2. 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.
  3. Add the namespace to your XAML View:

    xmlns:p="clr-namespace:Jbe.TestSuite.Infrastructure.Shell.Properties" 
       
  4. 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 Downloadhttp://compositeextensions.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27102 
or Code Download

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值