Click here to Skip to main content
15,895,777 members
Articles / General Programming / Localization

Automatically localizing applications with Google Translate

Rate me:
Please Sign up or sign in to vote.
4.86/5 (15 votes)
21 Aug 2012CPOL4 min read 54.1K   3.4K   43  
Localizing your apps with Google Translate
using System;
using System.Windows.Forms;
using System.Reflection;
using org.vonburg.UtilityApps.App_Localizer.Language_Codes;

namespace org.vonburg.UtilityApps.App_Localizer
{
    public partial class frmMain : Form
    {
        #region Private Variables

        /// <summary>
        /// Holds a refference to the languages object
        /// </summary>
        private languages _languages = new languages();

        #endregion

        public frmMain()
        {
            InitializeComponent();
        }

        #region Form Events

        /// <summary>
        /// Occurs when the form is first loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
            //set version info                        
            Version version = Assembly.GetExecutingAssembly().GetName().Version;
            this.lblVersion.Text = "Version:" + version.Major + "." + version.Minor + "." + version.Build + "." + version.Revision;

            //load the languages in the drop lists            
            string[] sLangs = this._languages.getAllLanguages();
            this.cbSourceLanguage.Items.AddRange(sLangs);
            this.cbSourceLanguage.Text = "English";
            this.cbOutputLanguage.Items.AddRange(sLangs);
            this.cbOutputLanguage.Text = "Spanish";
        }
        
        /// <summary>
        /// Occurs when the btnBrowseSource is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowseSource_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.txtSourceFile.Text = this.openFileDialog.FileName;
            }
        }

        /// <summary>
        /// Occurs when the btnBrowseOutput is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowseOutput_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.txtOutputFile.Text = this.saveFileDialog.FileName;
            }
        }

        /// <summary>
        /// Called when the btnTranslate is clicked.
        /// Performs the translation of the file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTranslate_Click(object sender, EventArgs e)
        {
            //make the tranlate button readonly
            this.btnTranslate.Enabled = false;
            this.lblMsg.Text = "";
            this.Cursor = Cursors.WaitCursor;

            //check for required variables
            if (this.txtSourceFile.Text == "")
            {
                throw new Exception("You must specifiy a source file");
            }
            if (this.txtOutputFile.Text == "")
            {
                throw new Exception("You must specifiy an output file");
            }

            //translate the file
            Localizer localizer = new Localizer();
            string sourceLangCode = this._languages.getLanguageCode(this.cbSourceLanguage.Text);
            string destLangCode = this._languages.getLanguageCode(this.cbOutputLanguage.Text);
            localizer.translateFile(this.txtSourceFile.Text, this.txtOutputFile.Text, sourceLangCode, destLangCode, this.cbOnlyTextProps.Checked);
            
            //make the tranlate button enabled
            this.btnTranslate.Enabled = true;
            this.lblMsg.Text = "Completed translation";
            this.Cursor = Cursors.Default;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions