是否可以使用.NET资源文件多国语言? [英] Is it possible to use multiple languages in .NET resource files?

查看:199
本文介绍了是否可以使用.NET资源文件多国语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经有了一个有趣的要求,我们将要支持多国语言在运行时,因为我们是一个服务。如果用户会谈,我们用日语或英语,我们希望在适当的语言来回应。 的FxCop 的人像我们这样来存储我们的字符串资源文件,但我很好奇,想知道是否有一个综合的方式来选择资源字符串在运行时无需做手工。

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. :)

推荐答案

你真的需要所有存储在一个单一的二进制语言资源?默认情况下,.NET查找在单独的卫星.dll文件的非默认语言资源,在一个子目录旁边的语言命名的主.dll文件。

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.

例如,如果您的.dll被称为MyProject.dll,你就会有一个JP-JP \ MyProject.dll,FR-是\ MyProject.dll等需要注意的是,这些额外的.dll文件的只包含的资源;您的code编译成主.dll文件。你需要与你的主要的.dll一起部署这些salellite .dll文件的。

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.

要自动创建这样的卫星.dll文件在Visual Studio中,你会创建一个Xyz.resx作为默认的翻译,和XYZ。的的.resx对于要转化为,例如,每一种语言,Xyz.jp-JP.resx或Xyz.de-AT.resx。 Visual Studio将采取在项目中创建的的\ MyProject.dll卫星组装所用每一种语言,这将包含所有的资源*。的.resx文件的护理在一起。

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.

在此设置中,你可以通过的ResourceManager ,就像默认的资源,通过传递正确的的CultureInfo 作为第二个参数的GetString GetObject的。最简单的方法是使用生成的资源类,它已经有一个正确配置的的ResourceManager 可供选择:

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);

请注意,如果你想使这个过程变得更轻松,你可以设置的CurrentUICulture 到正确的文化,如果没有指定将使用一种文化则所有资源查找的的CurrentUICulture (在那个特定的线程只):

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;

这是一个极大的方便,.NET Framework的其他大部分地区将开始使用相同的语言作为他们的消息和其他本地化设置的一部分。如果它们是可用的,那就是。 (您可能需要安装语言包)。如果得不到相关信息,该框架将回落到另一种语言(通常是英文)。

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).

不过,如果你只是想资源,翻译,你需要去的 ResourceManager.GetString 通话的方式。

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

这篇关于是否可以使用.NET资源文件多国语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆