如何传递引用默认资源文件的本地化密钥

时间:2018-09-18 15:44:08

标签: c# .net localization resx

所以我有两个带有转换字符串的resx文件,如下图所示

LocalizedText.resx (Default)
Key                   Value

Name                 This is English Name

LocalizedText.fr-FR.resx
Key                   Value

Name                 This is French Name

我们知道Visual Studio默认情况下会为默认资源文件创建一个设计好的.cs文件。下面是该屏幕截图。

enter image description here

下面是我的翻译代码:

public string GetTranslatedMessage(string key, CultureInfo culture, string resxLoc, Assembly assembly)
{
    var rm = new ResourceManager(resxLoc, assembly);
    return rm.GetString(key, culture);
}

呼叫代码:

var translatedMsg = GetTranslatedMessage(LocalizedText.Name, culture, resxLoc, assembly);

问题:您可以清楚地看到,我使用了LocalizedText.Name(它引用了父文件),并且我还具有智能感知支持。但是NOT WORK because it actually reads/passes the Value instead of the key。我的意思是,当传递给实际函数时,传递的内容是默认资源文件中经过手工翻译的字符串,因此显然无法正常工作。

解决方法:

public static const readonly ThisIsNameKey = "Name";

呼叫代码:

  var translatedMsg = GetTranslatedMessage(ThisIsNameKey, culture, resxLoc, assembly); // And this works fine.

我想要什么:为什么我不能使用Visual Studio的智能支持传递密钥,而将密钥引用为“ LocalizedText.Name”。我希望将它作为键传递,然后可以引用。

我可以使用

var translatedMsg = GetTranslatedMessage(LocalizedText.Name, culture, resxLoc, assembly);

并捕获第一个参数作为Key而不是值。请帮我。我不想创建常量,我想利用/读取默认Resx设计器文件中的密钥。

0 个答案:

没有答案