【发布时间】: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'”
那么,如何以编程方式在资源文件中正确插入基于法语的值?
【问题讨论】:
-
好吧,您介意使用code.google.com/archive/p/google-language-api-for-dotnet Google Language API 进行此类翻译吗?使用此 API,您可以让所有编码 - 将作业从一种语言解码到另一种语言到此 API。这只是一种选择,我还将搜索 HtmlDecode 字符以获取答案。但在我看来,试试看吧。
-
我用过它,它给了我以下错误:
GoogleAPIException: [error code:403]Please use Translate v2. -
你用的是哪个版本的?这是 Google .NET API 的列表。 code.google.com/archive/p/google-api-for-dotnet/downloads 我推荐使用 Google Translate API for .NET 0.3.1。
-
这是另一种选择(需要 google 帐户)developers.google.com/api-client-library/dotnet/apis/translate/… 这是 v2 版本。你也可以试试这个,--如果你遇到其他谷歌翻译 .dll 的问题。
标签: c# character-encoding resources google-translate