简体   繁体   English

为什么设置全球化文化不会导致使用我的本地化字符串?

[英]Why is setting globalization culture not causing my localized string to be used?

In a.Net 6 console app, I have set up a String resource Strings in a separate library project such that String.MyString = "string" in Strings.resx and "foreign string" in Strings.sv.resx... this was done by simply adding a new Resources File to the project and using the visual tool to add strings.在 .Net 6 控制台应用程序中,我在一个单独的库项目中设置了一个字符串资源Strings ,这样String.MyString = "string" in Strings.resx 和 "foreign string" in Strings.sv.resx ...通过简单地向项目添加一个新的资源文件并使用可视化工具添加字符串来完成。

I want to set my application culture to locale "sv" throughout so I wrote this test code:我想始终将我的应用程序文化设置为语言环境“sv”,所以我编写了这段测试代码:

internal class Program
{
  private static async Task Main(string[] args)
  {
    var culture = CultureInfo.CreateSpecificCulture("sv");
    Console.WriteLine($"1: {Strings.MyString}");
    CultureInfo.DefaultThreadCurrentCulture = culture;
    Console.WriteLine($"2: {Strings.MyString}");
    Thread.CurrentThread.CurrentCulture = culture;
    Console.WriteLine($"3: {Strings.MyString}");
    Strings.Culture = culture;
    Console.WriteLine($"4: {Strings.MyString}");
    ...

Output: Output:

1: string 1:字符串
2: string 2:字符串
3: string 3:字符串
4: foreign string 4:外来字符串

My expectation is that only the first line would be "string" since I change the culture.我的期望是只有第一行是"string" ,因为我改变了文化。 Forcibly changing the localization on Strings is the only way that works and seems a really bad way to do it.强行更改Strings的本地化是唯一可行的方法,而且似乎是一种非常糟糕的方法。

Every example I have seen seems to favor the Thread approach (3) so what is not working?我看到的每个示例似乎都支持Thread方法 (3) 那么什么不起作用呢? Isn't Strings supposed to use the thread/application locale? Strings不应该使用线程/应用程序语言环境吗?

Not sure about exact difference between CurrentUICulture and CurrentCulture of Thread.CurrentThread but changing CurrentUICulture is enough for changes to be applied.不确定CurrentUICultureThread.CurrentThreadCurrentCulture之间的确切区别,但更改 CurrentUICulture 足以应用更改。

I have two files in project:我在项目中有两个文件:

  1. Strings.resx - default culture strings are located here (English) Strings.resx - 默认区域性字符串位于此处(英语)

  2. Strings.ru.resx - Russian-translated strings are here Strings.ru.resx - 俄语翻译的字符串在这里

    Console.WriteLine($"Default culture: {Strings.TestString}"); var ruCulture = new CultureInfo("ru"); Thread.CurrentThread.CurrentUICulture = ruCulture; Console.WriteLine($"{ruCulture}: {Strings.TestString}");

Output: Output:

Default culture: English
ru: Русский

As a side note, no matter what your default actual culture is - it will search resources in primary *.resx file (without culture suffix).作为旁注,无论您的默认实际文化是什么 - 它都会在主 *.resx 文件(没有文化后缀)中搜索资源。
But when changing to alternative culture.resx with suffix will be searched (.ru.resx in my case).但是,当更改为带有后缀的替代 culture.resx 时,将搜索(在我的例子中为 .ru.resx)。

PS: Rider has nice designer to edit several files from single form. PS:Rider 有很好的设计器,可以从单一表单编辑多个文件。 Not sure about Visual Studio.不确定 Visual Studio。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM