如何使用定位在C# [英] How to use localization in C#

查看:127
本文介绍了如何使用定位在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法获得本地化的工作。

I just can't seem to get localization to work.

我有一个类库。现在,我想创建的 RESX 的文件在那里,并返回基于线程的文化中的值。

I have a class library. Now I want to create resx files in there, and return some values based on the thread culture.

我怎么能这样做?

推荐答案


  • 资源文件添加到您的项目(可以称之为strings.resx)

  • 在RESX文件添加一个字符串,种源,并给它一个好听的名字(例如:将其命名为你好有,并给它的价值你好)

  • 保存资源文件

运行此code:

Console.WriteLine(Properties.strings.Hello);

它应打印你好。

现在,添加新资源文件,命名为strings.fr.resx(注意FR的一部分,这其中将包含法语资源)。使用相同的名称添加字符串资源作为strings.resx,但在法国值(NAME =你好,值=萨吕)。现在,如果你运行下面的code,它应该打印萨吕:

Now, add a new resource file, named "strings.fr.resx" (note the "fr" part; this one will contain resources in French). Add a string resource with the same name as in strings.resx, but with the value in French (Name="Hello", Value="Salut"). Now, if you run the following code, it should print Salut:

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");
Console.WriteLine(Properties.strings.Hello);

什么情况是,该系统将寻求对FR-FR的资源。它不会找到一个(因为我们指定的文件FR)。然后,它会回落到检查FR,它认为(和使用)。

What happens is that the system will look for a resource for "fr-FR". It will not find one (since we specified "fr" in your file"). It will then fall back to checking for "fr", which it finds (and uses).

以下code,将打印你好:

The following code, will print "Hello":

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
Console.WriteLine(Properties.strings.Hello);

这是因为它没有发现任何EN-US的资源,也没有EN的资源,所以它会回落到默认,这是我们从一开始添加的。

That is because it does not find any "en-US" resource, and also no "en" resource, so it will fall back to the default, which is the one that we added from the start.

如果需要(例如strings.fr-FR.resx和strings.fr-CA.resx法国在法国和加拿大分别),您可以创建更具体的资源文件。在每一个这样的文件,你将需要添加的资源,为那些从它会回落到资源不同的字符串。所以,如果一个文本是法国和加拿大一样的,你可以把它放在strings.fr.resx,而字符串,在加拿大法语是不同的可以进入strings.fr-CA.resx。

You can create files with more specific resources if needed (for instance strings.fr-FR.resx and strings.fr-CA.resx for French in France and Canada respectively). In each such file you will need to add the resources for those strings that differ from the resource that it would fall back to. So if a text is the same in France and Canada, you can put it in strings.fr.resx, while strings that are different in Canadian french could go into strings.fr-CA.resx.

这篇关于如何使用定位在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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