通过显式本地化获取资源价值

ega*_*aga 12 c# localization asp.net-mvc-2

使用不同的资源文件(*.resx),如何通过提供显式本地化来检索本地化值.

也就是说,通常我可以使用custom-tool-namespace.Resource.localizedAttribute直接引用该属性.

它将给出的值取决于已设置为CurrentCulture的本地化(线程方式).但与此不同的是,我想将本地化交给资源获取者.这可能吗?

Dar*_*rov 26

假设您有多个资源文件:

Messages.resx
Messages.fr-FR.resx
...
Messages.xx-XX.resx
Run Code Online (Sandbox Code Playgroud)

所有包含一些字符串值,您可以检索特定文化的值:

var culture = new CultureInfo("fr-FR");
string value = Messages.ResourceManager.GetString("SomeKey", culture);
Run Code Online (Sandbox Code Playgroud)

这将独立于当前线程文化的价值.


小智 5

更好的做法是使用nameof来维护智能感知并避免输入错误

var culture = new CultureInfo("fr-FR");
string value = Messages.ResourceManager.GetString(nameof(Messages.SomeKey), culture);
Run Code Online (Sandbox Code Playgroud)