C# 在PCL中转换字符串

C# 在PCL中转换字符串,c#,localization,C#,Localization,我的项目中有一系列本地化文件。我当前的语言环境是de CH,但服务器的模式是英文的 在我的PCL中,是否有方法将de CH字符串转换为英语形式 翻译在标准的resx文件中。以下内容对我很有用: 我在文件夹en US中创建了一个PCL项目,其中包含一个英文资源文件: <?xml version="1.0" encoding="utf-8"?> <root> ... <data name="GoodMorning" xml:space="preserve">

我的项目中有一系列本地化文件。我当前的语言环境是de CH,但服务器的模式是英文的

在我的PCL中,是否有方法将de CH字符串转换为英语形式


翻译在标准的resx文件中。

以下内容对我很有用:

我在文件夹en US中创建了一个PCL项目,其中包含一个英文资源文件:

<?xml version="1.0" encoding="utf-8"?>
<root>
  ...
  <data name="GoodMorning" xml:space="preserve">
    <value>Good morning!</value>
  </data>
</root>
…这也许能回答你的问题。如果不是,则
ResourceManager.GetString
方法有一个重载,它接受
CultureInfo
;这也可能是一条可行的道路

所有这些都假设您拥有资源id;如果您有瑞士字符串的值,并且希望找到相应的英语字符串,那么事情会变得更加复杂。当然,您可以使用的工具取决于您的PCL针对的平台


编辑: 给定瑞士字符串,可以使用反射进行查找以获取属性值。为简单起见,这假设瑞士语和英语资源完全相同——现实生活中的代码可能需要处理一种或两种语言中缺失的字符串:

public static string GetEnglishString(string swiss)
{
    Type englishResources = typeof(en_US.Resources);
    Type swissResources = typeof(de_CH.Resources);
    PropertyInfo[] infos = swissResources.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
    foreach (PropertyInfo info in infos.Where(info => "Culture" != info.Name && "ResourceManager" != info.Name))
    {
        string value = info.GetValue(null, null) as string;
        if (value == swiss)
        {
            PropertyInfo englishProperty = englishResources.GetProperty(
                info.Name,
                BindingFlags.NonPublic | BindingFlags.Static);
            string english = englishProperty.GetValue(null, null) as string;
            return english;
        }
    }

    return null;
}

这需要通过NuGet提供。在现实生活中,只需这样做一次,就可以建立一个从瑞士到英语的
词典。祈祷这组瑞士字符串不包含重复的值。:-)

相关问题?在我看来这是可能的?这是我拥有的价值,我的目标是78
string english = en_US.Resources.GoodMorning; // Returns "Good morning!"
string swiss = de_CH.Resources.GoodMorning;   // Returns "Guten morgen!"
public static string GetEnglishString(string swiss)
{
    Type englishResources = typeof(en_US.Resources);
    Type swissResources = typeof(de_CH.Resources);
    PropertyInfo[] infos = swissResources.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
    foreach (PropertyInfo info in infos.Where(info => "Culture" != info.Name && "ResourceManager" != info.Name))
    {
        string value = info.GetValue(null, null) as string;
        if (value == swiss)
        {
            PropertyInfo englishProperty = englishResources.GetProperty(
                info.Name,
                BindingFlags.NonPublic | BindingFlags.Static);
            string english = englishProperty.GetValue(null, null) as string;
            return english;
        }
    }

    return null;
}