2017-12-26 52 views
0

を持つ値を持つリソースファイルを作成します。私は、プログラムMyResources.resxがここMyResources.fr-CA.resxC#の:Googleがサービスを翻訳使用して、フランス語の文字に私のC#のコンソールアプリケーションで

にファイルを変換しようとしているコードです:

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(); 
} 

問題は、フランス語の文字が「?」と表示されることです。 「&#39」のような文字を表示します

したがって、プログラムでフランス語ベースの値をリソースに挿入する方法はありますかファイルは正しくありますか?

+0

さて、この種の翻訳ではhttps://code.google.com/archive/p/google-language-api-for-dotnet/ Google Language APIを使用してもよろしいですか?このAPIを使用すると、ある言語から別の言語へのすべてのエンコード・デコード・ジョブをこのAPIに任せることができます。それは単なる選択肢ですが、答えとしてHtmlDecode文字も検索します。しかし、私の意見では、試してみてください。 –

+0

私はそれを使用して、次のエラーが表示されます: 'GoogleAPIException:[エラーコード:403]翻訳v2を使用してください。 –

+0

どのバージョンを使用しましたか?以下に、Google .NET APIのリストを示します。 https://code.google.com/archive/p/google-api-for-dotnet/downloads Google Translate API for .NET 0.3.1を使用することをおすすめします。 –

答えて

0

フランス語の文字を正しくデコードするには、WebClientのエンコードタイプを変更する必要があります。

ので、この行:

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

と(TranslateTextメソッドを呼び出した後に)あなたの "結果" 変数が正しくフランス語で読めるようになります。

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

は次のようにする必要があります。

この方法を試してみてください、それは私のために働いてください。よろしくです。 よろしくお願いします。

+0

私はそれを確認します。 –

関連する問題