【问题标题】:C#: Create Resource file with values having French charactersC#:使用具有法语字符的值创建资源文件
【发布时间】:2017-12-26 11:02:37
【问题描述】:

在我的 C# 控制台应用程序中,我使用谷歌翻译服务以编程方式尝试将 MyResources.resx 文件翻译成 MyResources.fr-CA.resx

代码如下:

public static string TranslateText(string input, string languagePair)
{
       string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
       WebClient webClient = new WebClient();
       webClient.Encoding = System.Text.Encoding.UTF8;
       string result = webClient.DownloadString(url);
       result = result.Substring(result.IndexOf("<span title=\"") + "<span title=\"".Length);
       result = result.Substring(result.IndexOf(">") + 1);
       result = result.Substring(0, result.IndexOf("</span>"));
       return result.Trim();
}

static void Main(string[] args)
{
   var resWriter = new ResourceWriter("Translations.resources");

   ResourceSet resourceSet = MyResources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
   foreach (DictionaryEntry entry in resourceSet)
   {
       string resKey = entry.Key.ToString();
       string resValue = entry.Value.ToString();

       var result = TranslateText(resValue, "en|fr-CA");

       if (result != null)
       {
          resWriter.AddResource(resKey, System.Web.HttpUtility.HtmlDecode(result));
       }
    }

    resWriter.Close();
}

问题是一些法语字符显示为“?”生成的 Resources.fr-CA.resx 文件中的字符,并且只有当我使用 System.Web.HttpUtility.HtmlDecode 时,它才会显示字符,例如“d'”

那么,如何以编程方式在资源文件中正确插入基于法语的值?

【问题讨论】:

标签: c# character-encoding resources google-translate


【解决方案1】:

您必须更改您的 webClient 编码类型才能正确解码法语字符。

所以这一行:

webClient.Encoding = System.Text.Encoding.UTF8;

必须是这样的:

webClient.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");

并且您的“结果”变量(在调用 TranslateText 方法之后)应该可以正确地用法语阅读。

请尝试这种方式并通知我,它对我有用。 问候。

【讨论】:

  • 好的,我会检查的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多