strings.resx
。System.Threading
and System.Globalization
Run this code:
Console.WriteLine(Properties.strings.Hello);
It should print "Hello".
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”的位置。
下面的代码,将打印“ Hello”:
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
Console.WriteLine(Properties.strings.Hello);
那是因为它找不到任何“ en-US”资源,也找不到“ en”资源,因此它将回退到默认值,这是我们从一开始就添加的资源。
如果需要,您可以使用更具体的资源创建文件(例如,分别在法国和加拿大使用French的strings.fr-FR.resx和strings.fr-CA.resx)。在每个这样的文件中,您将需要为那些与后备资源不同的字符串添加资源。因此,如果文本在法国和加拿大是相同的,则可以将其放在strings.fr.resx中,而在加拿大法语中不同的字符串可以输入strings.fr-CA.resx。
答案可以参考Visual Studio在此处完成的“幕后”管道:resx.designer.cs文件,使智能工作;使用类库编译的附属程序集,需要与已编译的程序集以及使用该程序集的任何更高版本的项目一起部署,等等。答案很好,很简单,但是它无助于说明哪里可能出问题了,例如您不使用Visual Studio。
+1个帖子!与其尝试手动制作文件,不如尝试Zeta资源编辑器(zeta-resource-editor.com/index.html)。它是免费的,可以帮助您比在VS中更快地完成这类翻译。
Access Modifier
应该设置Public
要生成的资源类。该类不一定位于Properties命名空间中,而是在其中放置.resx文件的位置。请注意,在VS 2017中,由于错误(至少在15.4版之前),在winform中进行本地化的resx无法正常工作。可以买到票:developercommunity.visualstudio.com/content/problem/63772/…
从.NET 4.5开始,也可以使用System.Globalization.CultureInfo.DefaultThreadCurrentCulture而不是Thread.CurrentThread.CurrentUICulture,以便更改整个应用程序的语言环境,而不是逐个线程