开发者

Is it possible to use multiple languages in .NET resource files?

开发者 https://www.devze.com 2023-01-03 10:03 出处:网络
We\'ve got an interesting requirement that we\'ll want to support multiple languages at runtime since we\'re a service. If a user talks to us using Japanese or English, we\'ll want to respond in the a

We've got an interesting requirement that we'll want to support multiple languages at runtime since we're a service. If a user talks to us using Japanese or English, we'll want to respond in the appropriate language. FxCop likes us to store our strings in resource files, but I was curious to know if there was an integrated way to select resource string at runtime without having to开发者_如何转开发 do it manually.

Bottom Line: We need to be able to support multiple languages in a single binary. :)


Do you really need all the language resources stored in a single binary? By default, .NET looks for resources for the non-default language in separate 'satellite' .dlls, in a subdirectory next to your main .dll named after the language.

For example, if your .dll is called MyProject.dll, you'd have a jp-JP\MyProject.dll, fr-BE\MyProject.dll, etc. Note that these extra .dll's only contain the resources; your code is compiled into the main .dll. You'd need to deploy these salellite .dll's together with your main .dll.

To automatically create such satellite .dll's in Visual Studio, you'd create an Xyz.resx for your default translations, and Xyz.lang.resx for every language you want to translate into, e.g., Xyz.jp-JP.resx or Xyz.de-AT.resx. Visual Studio will take care of creating a lang\MyProject.dll satellite assembly for each language used, which will contain the resources of all *.lang.resx files in your project together.

In this setup, you can access the right resources through a ResourceManager, just like the default resources, by passing the proper CultureInfo as the second parameter to GetString or GetObject. The easiest way is to use the generated resource class, which already has a properly configured ResourceManager available:

CultureInfo culture = new CultureInfo("jp-JP");
...
string labelText = ResourceName.ResourceManager.GetString("SomeKey", culture);

Note that if you'd like to make this process a little easier, you could set the Thread's CurrentUICulture to the right culture, and then all resource lookups without a culture specified will use the CurrentUICulture (on that particular thread only):

Thread.CurrentUICulture = new CultureInfo("jp-JP");
...
string labelText = ResourceName.SomeKey;

This is a lot easier and most other parts of the .NET Framework will start using the same language as part of their messages and other localization settings. If they are available, that is. (You might need to install a language pack.) If they are not available, the Framework will fall back to another language (usually English).

However, if you only want your resources to be translated, you'd need to go the way of the ResourceManager.GetString calls.

0

精彩评论

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