C#:如何从某种文化中获取资源字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1825574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:10:02  来源:igfitidea点击:

C#: How to get a resource string from a certain culture

c#.netlocalizationresources

提问by Svish

I have a resource assembly with translated texts in various languages. Project kind of looks like this:

我有一个资源程序集,其中包含各种语言的翻译文本。项目看起来像这样:

  • FooBar.resx
  • FooBar.nb-NO.resx
  • FooBar.sv-SE.resx
  • ...
  • FooBar.resx
  • FooBar.nb-NO.resx
  • FooBar.sv-SE.resx
  • ...

I can get the texts using static properties like this:

我可以使用这样的静态属性获取文本:

var value = FooBar.Hello;

Or by using reflection like this:

或者像这样使用反射:

var value = resourceAssembly
      .GetType("Namespace.FooBar")
      .GetProperty("Hello")
      .GetValue(null, null) as string;

Both ways will get me the value belonging to the current UI culture of the current thread. Which is fine and totally what I would usually like.

这两种方式都会让我获得属于当前线程的当前 UI 文化的价值。这很好,完全是我通常想要的。

But, is there something I can do if I explicitly want for example the Swedish value, without having to change the UI culture?

但是,如果我明确想要瑞典的价值,而不必改变 UI 文化,我能做些什么吗?

采纳答案by Konamiman

You can manually change the Cultureproperty of the FooBar class that Visual Studio generates. Or if you are directly using the ResourceManager class, you can use the overload of GetStringthat takes the desired culture as a parameter.

您可以手动更改CultureVisual Studio 生成的 FooBar 类的属性。或者,如果您直接使用ResourceManager 类,则可以使用GetString的重载,它将所需的区域性作为参数。

回答by Foxfire

You can manually change the culture in your resource access class. But this is somewhat unrecommended because it leads to lots of other internationalization problems.

您可以手动更改资源访问类中的文化。但这有点不推荐,因为它会导致许多其他国际化问题。

E.g. you would have to:

例如,您必须:

  • Handle all number-formatting with the culture overloads
  • Ensure that other libraries you use have a similar mechanism that you have
  • In cases where the above is impossible (e.g. BCL) create wrappers for everything that MIGHT be culture specific (and that is a LOT of stuff).
  • 使用区域性重载处理所有数字格式
  • 确保您使用的其他库具有与您类似的机制
  • 在上述不可能的情况下(例如 BCL),为可能是特定于文化的所有内容创建包装器(这是很多东西)。

So if at all possible change the current UI culture of the current thread.

因此,如果可能的话,请更改当前线程的当前 UI 文化。

回答by ScottE

Here's some code I've used to grab a resource file by culture name - it's vb.net, but you get the idea.

这是我用来按文化名称获取资源文件的一些代码 - 它是 vb.net,但您明白了。

Dim reader As New System.Resources.ResXResourceReader(String.Format(Server.MapPath("/App_GlobalResources/{0}.{1}.resx"), resourceFileName, culture))

And if you want to return it as a dictionary:

如果你想将它作为字典返回:

If reader IsNot Nothing Then
    Dim d As New Dictionary(Of String, String)
    Dim enumerator As System.Collections.IDictionaryEnumerator = reader.GetEnumerator()
    While enumerator.MoveNext
        d.Add(enumerator.Key, enumerator.Value)
    End While
    Return d
End If

回答by AnthonyWJones

Use the second overload of GetValue:-

使用 GetValue 的第二个重载:-

 .GetValue(null, BindingFlags.GetProperty, null, null, CultureInfo.GetCultureInfo("sv-SE"))

回答by Himanshu Vohra

Try the following code. It worked fine for me at least:

试试下面的代码。至少对我来说效果很好:

FooBar.ResourceManager.GetString("Hello", CultureInfo.GetCultureInfo("sv-SE"))