开发者

Localisation in WPF

I am reading one book in WPF which provides a method for creating an application for localisation purposes.What he suggested and the same I did was:

1)edit the project file .csproj and add an element en-US to create a localised assembly that contains en-US localised embedded baml.When I compiled the project it created a sub-folder en-US that contained the en-US localised embedded baml.I took it's backup.

2)I did the same for fr-FR and created a backup for it.

3)Next I placed both the created subfolders in the application folder with the names en-US and fr-FR.

The book suggests that that if I change the culture setting to fr-FR(via control panel regional settings) the fr-FR localised assembly will be used.

But, I found that no matter what the culture is, it's using the en-US one. I know this because I changed the culture to fr-FR and deleted the fr-FR folder, but still the app. is running and when the en-US one is deleted, it throws an exception.

Can anyone clarify where am I doing/understanding wrong?

Here are the words of the book:

Preparing an Application for Localization

The next step is to switch on localization support for your project. This takes just one change—you need to add the following element to the .csproj file for your project anywhere in the first element:

en-US

This tells the compiler that the default culture for your applicat开发者_开发知识库ion is U.S. English (obvi ously, you could choose something else if that’s appropriate). Once you make this change, the build process changes. The next time you compile your application, you’ll end up with a sub-folder named en-US. Inside that folder is a satellite assembly with the same name as your application and the extension .resources.dll (for example, LocalizableApplication.resources.dll).

This assembly contains all the compiled BAML resources for your application, which were previously stored in your main application assembly.

Now, when you run this application, the common language runtime (CLR) automatically looks for satellite assemblies in the right directory based on the computer’s regional settings and loads the correct localized resource. For example, if you’re running in the fr-FR culture, the CLR will look for an fr-FR subdirectory and use the satellite assemblies it finds there.

That means that if you want to add support for more cultures to a localized application, you simply need to add more subfolders and satellite assemblies without disturbing the original application executable.

When the CLR begins probing for a satellite assembly, it follows a few simple rules of precedence:

1. First, it checks for the most specific directory that’s available. That means it looks for a satellite assembly that’s targeted for the current language and region (such as fr-FR).

2. If it can’t find this directory, it looks for a satellite assembly that’s targeted for the current language (such as fr).

3. If it can’t find this directory, an IOException exception is thrown.


The Resource Manager in .NET uses the CurrentUICulture flag to decide which localized assembly to load, rather than the CurrentCulture flag. CurrentUICulture represents the native language of the version of Windows that you are running, whereas CurrentCulture represents your current regional setting, set via Control Panel.

Another way to think about this is that there are two independent things that your application can ask about your user:

1) What language does he speak? (CurrentUICulture)

2) Where is he located? (CurrentCulture)

The answer to #1 should dictate the language that you use for displaying UI elements, i.e. which localized resource file to load.

The answer to #2 should dictate how you display things like numeric strings and currency values. This is automatically handled by numeric/currency conversion to string values in their ToString methods.

To double-check the value of these two properties, you can do something like this:

        Console.WriteLine("CurrentCulture={0}, CurrentUICulture={1}", CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);

To change CurrentCulture, you just change your location in Control Panel | Region and Language | Formats.

The change CurrentUICulture, you can do one of the following:

  • Test on a non-English version of Windows. (E.g. in a VM)
  • Use a MUI pack in Windows 7
  • Add an override in your code for testing purposes, typically based on a .config file setting, where you explicitly set the value of CultureInfo.CurrentUICulture.

Example of overriding UICulture, based on an UICulture setting in app.config is shown below. You'd set UICultureOverride to typical culture strings, e.g. fr-FR, de-DE, etc.

        // using System.Threading;
        // using System.Globalization;
        // using System.Configuration;
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(ConfigurationManager.AppSettings.Get("UICultureOverride"));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜