如何在 C# 中使用本地化 [英] How to use localization in C#

查看:50
本文介绍了如何在 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"):
    右键单击项目中的属性,选择添加-> New Item... 在上下文菜单中,然后在 Visual C# Items 列表中选择 "Resources file" 并将其命名为 strings.resx.
  • 在 resx 文件中添加一个字符串资源并为其命名(例如:将其命名为Hello"并为其赋值为Hello")
  • 保存资源文件(注意:这将是默认资源文件,因为它没有两个字母的语言代码)
  • 添加对您的程序的引用:System.ThreadingSystem.Globalization
    • Add a Resource file to your project (you can call it "strings.resx") by doing the following:
      Right-click Properties in the project, select Add -> New Item... in the context menu, then in the list of Visual C# Items pick "Resources file" and name it strings.resx.
    • Add a string resouce in the resx file and give it a good name (example: name it "Hello" with and give it the value "Hello")
    • Save the resource file (note: this will be the default resource file, since it does not have a two-letter language code)
    • Add references to your program: System.Threading and System.Globalization
    • 运行此代码:

      Console.WriteLine(Properties.strings.Hello);
      

      它应该打印你好".

      现在,添加一个名为strings.fr.resx"的新资源文件(注意fr"部分;这个将包含法语资源).添加一个与strings.resx 同名的字符串资源,但值为法语(Name="Hello", Value="Salut").现在,如果您运行以下代码,它应该打印 Salut:

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

      以下代码,将打印Hello":

      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天全站免登陆