Google Translate in C#

vinnyMS100

New member
Joined
Aug 4, 2021
Messages
2
Programming Experience
Beginner
i have a C# program that translates from one language to another. there's a problem when trying to translate from Chinese to English the "<"and ">" it translates as "\u003c" and "\u003e".

also i need it to translate multi line text without formatting loss,


it only translates one line, the first line
 
Last edited by a moderator:
Also, based on the size of your attachment, I suspect that you have included binary files. Please do not do that. ALWAYS delete the bin and obj folders before zipping a project to attach.
 
If you need that level of sophistication in translations, why are you using the free Google Translation service. You should be paying for the Google Cloud Translation API. It supports translating HTML, documents, etc.

 
Also as a quick aside, I recommend upgrading to at least VS2019 and using .NET Framework 4.8 for your project.
 
here's the code

Program.cs
C#:
using System;
using System.Windows.Forms;

namespace RavSoft.GoogleTranslator
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new GoogleTranslatorFrm());
        }
    }
}

Translator.cs

C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;

namespace RavSoft.GoogleTranslator
{
    /// <summary>
    /// Translates text using Google's online language tools.
    /// </summary>
    public class Translator
    {
        #region Properties

            /// <summary>
            /// Gets the supported languages.
            /// </summary>
            public static IEnumerable<string> Languages {
                get {
                    Translator.EnsureInitialized();
                    return Translator._languageModeMap.Keys.OrderBy (p => p);
                }
            }

            /// <summary>
            /// Gets the time taken to perform the translation.
            /// </summary>
            public TimeSpan TranslationTime {
                get;
                private set;
            }

            /// <summary>
            /// Gets the url used to speak the translation.
            /// </summary>
            /// <value>The url used to speak the translation.</value>
            public string TranslationSpeechUrl {
                get;
                private set;
            }

            /// <summary>
            /// Gets the error.
            /// </summary>
            public Exception Error {
                get;
                private set;
            }

        #endregion

        #region Public methods

            /// <summary>
            /// Translates the specified source text.
            /// </summary>
            /// <param name="sourceText">The source text.</param>
            /// <param name="sourceLanguage">The source language.</param>
            /// <param name="targetLanguage">The target language.</param>
            /// <returns>The translation.</returns>
            public string Translate
                (string sourceText,
                 string sourceLanguage,
                 string targetLanguage)
            {
                // Initialize
                this.Error = null;
                this.TranslationSpeechUrl = null;
                this.TranslationTime = TimeSpan.Zero;
                DateTime tmStart = DateTime.Now;
                string translation = string.Empty;

                try {
                    // Download translation
                    string url = string.Format ("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
                                                Translator.LanguageEnumToIdentifier (sourceLanguage),
                                                Translator.LanguageEnumToIdentifier (targetLanguage),
                                                HttpUtility.UrlEncode (sourceText));
                    string outputFile = Path.GetTempFileName();
                    using (WebClient wc = new WebClient ()) {
                        wc.Headers.Add ("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
                        wc.DownloadFile(url, outputFile);
                    }

                    // Get translated text
                    if (File.Exists (outputFile)) {

                        // Get phrase collection
                        string text = File.ReadAllText(outputFile);
                        int index = text.IndexOf (string.Format(",,\"{0}\"", Translator.LanguageEnumToIdentifier (sourceLanguage)));
                        if (index == -1) {
                            // Translation of single word
                            int startQuote = text.IndexOf('\"');
                            if (startQuote != -1) {
                                int endQuote = text.IndexOf('\"', startQuote + 1);
                                if (endQuote != -1) {
                                    translation = text.Substring(startQuote + 1, endQuote - startQuote - 1);
                                }
                            }
                        }
                        else {
                            // Translation of phrase
                            text = text.Substring(0, index);
                            text = text.Replace("],[", ",");
                            text = text.Replace("]", string.Empty);
                            text = text.Replace("[", string.Empty);
                            text = text.Replace("\",\"", "\"");

                            // Get translated phrases
                            string[] phrases = text.Split (new[] { '\"' }, StringSplitOptions.RemoveEmptyEntries);
                            for (int i=0; (i < phrases.Count()); i += 2) {
                                string translatedPhrase = phrases[i];
                                if (translatedPhrase.StartsWith(",,")) {
                                    i--;
                                    continue;
                                }
                                translation += translatedPhrase + "  ";
                            }
                        }

                        // Fix up translation
                        translation = translation.Trim();
                        translation = translation.Replace(" ?", "?");
                        translation = translation.Replace(" !", "!");
                        translation = translation.Replace(" ,", ",");
                        translation = translation.Replace(" .", ".");
                        translation = translation.Replace(" ;", ";");

                        // And translation speech URL
                        this.TranslationSpeechUrl = string.Format ("https://translate.googleapis.com/translate_tts?ie=UTF-8&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=gtx",
                                                                   HttpUtility.UrlEncode (translation), Translator.LanguageEnumToIdentifier (targetLanguage), translation.Length);
                    }
                }
                catch (Exception ex) {
                    this.Error = ex;
                }

                // Return result
                this.TranslationTime = DateTime.Now - tmStart;
                return translation;
            }

        #endregion

        #region Private methods

            /// <summary>
            /// Converts a language to its identifier.
            /// </summary>
            /// <param name="language">The language."</param>
            /// <returns>The identifier or <see cref="string.Empty"/> if none.</returns>
            private static string LanguageEnumToIdentifier
                (string language)
            {
                string mode = string.Empty;
                Translator.EnsureInitialized();
                Translator._languageModeMap.TryGetValue (language, out mode);
                return mode;
            }

            /// <summary>
            /// Ensures the translator has been initialized.
            /// </summary>
            private static void EnsureInitialized()
            {
                if (Translator._languageModeMap == null) {
                    Translator._languageModeMap = new Dictionary<string,string>();
                    Translator._languageModeMap.Add ("Afrikaans",   "af");
                    Translator._languageModeMap.Add ("Albanian",    "sq");
                    Translator._languageModeMap.Add ("Arabic",      "ar");
                    Translator._languageModeMap.Add ("Armenian",    "hy");
                    Translator._languageModeMap.Add ("Azerbaijani", "az");
                    Translator._languageModeMap.Add ("Basque",      "eu");
                    Translator._languageModeMap.Add ("Belarusian",  "be");
                    Translator._languageModeMap.Add ("Bengali",     "bn");
                    Translator._languageModeMap.Add ("Bulgarian",   "bg");
                    Translator._languageModeMap.Add ("Catalan",     "ca");
                    Translator._languageModeMap.Add ("Chinese",     "zh-CN");
                    Translator._languageModeMap.Add ("Croatian",    "hr");
                    Translator._languageModeMap.Add ("Czech",       "cs");
                    Translator._languageModeMap.Add ("Danish",      "da");
                    Translator._languageModeMap.Add ("Dutch",       "nl");
                    Translator._languageModeMap.Add ("English",     "en");
                    Translator._languageModeMap.Add ("Esperanto",   "eo");
                    Translator._languageModeMap.Add ("Estonian",    "et");
                    Translator._languageModeMap.Add ("Filipino",    "tl");
                    Translator._languageModeMap.Add ("Finnish",     "fi");
                    Translator._languageModeMap.Add ("French",      "fr");
                    Translator._languageModeMap.Add ("Galician",    "gl");
                    Translator._languageModeMap.Add ("German",      "de");
                    Translator._languageModeMap.Add ("Georgian",    "ka");
                    Translator._languageModeMap.Add ("Greek",       "el");
                    Translator._languageModeMap.Add ("Haitian Creole",    "ht");
                    Translator._languageModeMap.Add ("Hebrew",      "iw");
                    Translator._languageModeMap.Add ("Hindi",       "hi");
                    Translator._languageModeMap.Add ("Hungarian",   "hu");
                    Translator._languageModeMap.Add ("Icelandic",   "is");
                    Translator._languageModeMap.Add ("Indonesian",  "id");
                    Translator._languageModeMap.Add ("Irish",       "ga");
                    Translator._languageModeMap.Add ("Italian",     "it");
                    Translator._languageModeMap.Add ("Japanese",    "ja");
                    Translator._languageModeMap.Add ("Korean",      "ko");
                    Translator._languageModeMap.Add ("Lao",         "lo");
                    Translator._languageModeMap.Add ("Latin",       "la");
                    Translator._languageModeMap.Add ("Latvian",     "lv");
                    Translator._languageModeMap.Add ("Lithuanian",  "lt");
                    Translator._languageModeMap.Add ("Macedonian",  "mk");
                    Translator._languageModeMap.Add ("Malay",       "ms");
                    Translator._languageModeMap.Add ("Maltese",     "mt");
                    Translator._languageModeMap.Add ("Norwegian",   "no");
                    Translator._languageModeMap.Add ("Persian",     "fa");
                    Translator._languageModeMap.Add ("Polish",      "pl");
                    Translator._languageModeMap.Add ("Portuguese",  "pt");
                    Translator._languageModeMap.Add ("Romanian",    "ro");
                    Translator._languageModeMap.Add ("Russian",     "ru");
                    Translator._languageModeMap.Add ("Serbian",     "sr");
                    Translator._languageModeMap.Add ("Slovak",      "sk");
                    Translator._languageModeMap.Add ("Slovenian",   "sl");
                    Translator._languageModeMap.Add ("Spanish",     "es");
                    Translator._languageModeMap.Add ("Swahili",     "sw");
                    Translator._languageModeMap.Add ("Swedish",     "sv");
                    Translator._languageModeMap.Add ("Tamil",       "ta");
                    Translator._languageModeMap.Add ("Telugu",      "te");
                    Translator._languageModeMap.Add ("Thai",        "th");
                    Translator._languageModeMap.Add ("Turkish",     "tr");
                    Translator._languageModeMap.Add ("Ukrainian",   "uk");
                    Translator._languageModeMap.Add ("Urdu",         "ur");
                    Translator._languageModeMap.Add ("Vietnamese",  "vi");
                    Translator._languageModeMap.Add ("Welsh",       "cy");
                    Translator._languageModeMap.Add ("Yiddish",     "yi");
                }
            }

        #endregion

        #region Fields

            /// <summary>
            /// The language to translation mode map.
            /// </summary>
            private static Dictionary<string, string> _languageModeMap;

        #endregion
    }
}

GoogleTranslatorFrm.cs

C#:
using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace RavSoft.GoogleTranslator
{
    /// <summary>
    /// A sample application to demonstrate the <see cref="TranslatorOld"/> class.
    /// </summary>
    public partial class GoogleTranslatorFrm : Form
    {
        #region Constructor

            /// <summary>
            /// Initializes a new instance of the <see cref="GoogleTranslatorFrm"/> class.
            /// </summary>
            public GoogleTranslatorFrm()
            {
                InitializeComponent();
            }

        #endregion

        #region Form event handlers

            /// <summary>
            /// Handles the Load event of the GoogleTranslatorFrm control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
            private void GoogleTranslatorFrm_Load
                (object sender,
                 EventArgs e)
            {
                this._comboFrom.Items.AddRange (Translator.Languages.ToArray());
                this._comboTo.Items.AddRange (Translator.Languages.ToArray());
                this._comboFrom.SelectedItem = "English";
                this._comboTo.SelectedItem = "French";
            }

        #endregion

        #region Button handlers

            /// <summary>
            /// Handles the LinkClicked event of the _lnkSourceEnglish control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.Windows.Forms.LinkLabelLinkClickedEventArgs"/> instance containing the event data.</param>
            private void _lnkSourceEnglish_LinkClicked
                (object sender,
                 LinkLabelLinkClickedEventArgs e)
            {
                this._comboFrom.SelectedItem = "English";
            }

            /// <summary>
            /// Handles the LinkClicked event of the _lnkTargetEnglish control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.Windows.Forms.LinkLabelLinkClickedEventArgs"/> instance containing the event data.</param>
            private void _lnkTargetEnglish_LinkClicked
                (object sender,
                 LinkLabelLinkClickedEventArgs e)
            {
                this._comboTo.SelectedItem = "English";
            }

            /// <summary>
            /// Handles the Click event of the _btnTranslate control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
            private void _btnTranslate_Click
                (object sender,
                 EventArgs e)
            {
                // Initialize the translator
                Translator t = new Translator();

                this._editTarget.Text = string.Empty;
                this._editTarget.Update();
                this._translationSpeakUrl = null;

                // Translate the text
                try {
                    this.Cursor = Cursors.WaitCursor;
                    this._lblStatus.Text = "Translating...";
                    this._lblStatus.Update();
                    this._editTarget.Text = t.Translate (this._editSourceText.Text.Trim(), (string) this._comboFrom.SelectedItem, (string) this._comboTo.SelectedItem);
                    if (t.Error == null) {
                        this._editTarget.Update();
                        this._translationSpeakUrl = t.TranslationSpeechUrl;
                    }
                    else {
                        MessageBox.Show (t.Error.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                 
                }
                catch (Exception ex) {
                    MessageBox.Show (ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally {
                    this._lblStatus.Text = string.Format ("Translated in {0} mSec", (int) t.TranslationTime.TotalMilliseconds);
                    this.Cursor = Cursors.Default;
                }
            }

            /// <summary>
            /// Handles the Click event of the _btnSpeak control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
            private void _btnSpeak_Click
                (object sender,
                 EventArgs e)
            {
                if (!string.IsNullOrEmpty (this._translationSpeakUrl)) {
                    this._webBrowserCtrl.Navigate (this._translationSpeakUrl);
                }
            }

            /// <summary>
            /// Handles the LinkClicked event of the _lnkReverse control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="System.Windows.Forms.LinkLabelLinkClickedEventArgs"/> instance containing the event data.</param>
            private void _lnkReverse_LinkClicked
                (object sender,
                 LinkLabelLinkClickedEventArgs e)
            {
                // Swap translation mode
                string from = (string) this._comboFrom.SelectedItem;
                string to = (string) this._comboTo.SelectedItem;
                this._comboFrom.SelectedItem = to;
                this._comboTo.SelectedItem = from;

                // Reset text
                this._editSourceText.Text = this._editTarget.Text;
                this._editTarget.Text = string.Empty;
                this.Update();
                this._translationSpeakUrl = string.Empty;
            }

        #endregion

        #region Fields

            /// <summary>
            /// The URL used to speak the translation.
            /// </summary>
            private string _translationSpeakUrl;

        #endregion
    }
}
 
Last edited by a moderator:
That couldn't have gone much worse. You posted the code as text and tried to format it as code, but you used the wrong tags. The Inline code tag is for inline code, not large code blocks. You also ignored the part where I said the RELEVANT code. If just post every bit of code you have then you're expecting us to waste our time trawling through it to work out what's relevant. You also ignored the part where explicitly told you to delete bin and obj folders before zipping a project.
 
Back
Top Bottom