Tuesday 18 June 2013

Language Translator [using GOOGLE API]

Language Translator [using GOOGLE API]



his thread is a full tutorial about making a Translator usingGoogle's API to build a C# Windows Forms Applications that uses your internet connection and translate your text from google

What Do You Need ?

1- Google API , download it from the attachments
-the API I used , I downloaded it from CodePlex since code.google is forbidden in my Country :sleep: -

2- .NET framework 3.5

WHY 3.5 ? if you are using .NET 4.0 you need to get a newer API than this I used
ARE YOU USING Visual Studio 2010 ? so the Default is .NET 4.0
HOW TO MAKE IT 3.5 ? follow me in pics :confused: :

Go to Solution Explorer --> Right Click on the Project File --> Choose 3.5 client profile from the framework target
Posted Image

LET'S START :
**First Step**
now open your Windows Forms project , Before Starting in Designing the form
let's add the API , google API is a dll library

adding it code be done from the Solution Explorer --> Right Click on References --> Add Reference
Posted Image

by Clicking OK , you should take attention that the GOOGLE namespace is now available
go to your FORM code and add
using Google.API.Translate;
Posted Image


**Second Step**
making the Form design :
see my design and you can make any changes you want
Posted Image

if you have any questions about ToolStrip menu or any GUI ask me ... but it's simple and easy

**Third Step : CODING**
first : let's fulfill the combo boxes with languages FROM and TO that google translator give us it using it's API

DOUBLE CLICK on the combo box will take you to the event "Click"
my combo box name is "combo_from"
type this code :
            for (int i = 0; i < Google.API.Translate.Language.TranslatableCollection.Count(); i++)
            {
                combo_from.Items.Add(Google.API.Translate.Language.TranslatableCollection.ElementAt(i).ToString());
            }
and the same thing for the second combobox ...

second : translate METHOD that the translate button will call it

public string TranslateYourText(string text, string langFrom, string langTo)
        {
            string translated = "";
            Console.WriteLine(text);
            try
            {
                TranslateClient client = new TranslateClient("");
                Language lang1 = Language.English;
                Language lang2 = Language.Arabic;
                foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.NeutralCultures))
                {
                    if (ci.EnglishName == langFrom)
                    {
                        lang1 = ci.Name;
                    }
                    if (ci.EnglishName == langTo)
                    {
                        lang2 = ci.Name;
                    }
                }
                if (Autodetect == true)
                {
                    string from;
                    translated = client.TranslateAndDetect(text, lang2.ToString(), out from);
                    foreach (string options in Language.GetEnums())
                    {
                        if (options == from)
                        {
                            CultureInfo ci = new CultureInfo(from);
                            combo_from.Text = ci.EnglishName;
                        }

                    }
                }
                else
                {
                    translated = client.Translate(text, lang1, lang2, TranslateFormat.Text);
                    Console.WriteLine(translated);
                }
                return translated;
            }
            catch
            {
                MessageBox.Show(this, "Check Your Internet Connection","Try Again Please",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return translated;
            }
        }

now , you should know one thing , using (Display Parameter Info) and (Display Quick Info) is very important now since you don't know what's the behavior of Google Api

about the TranslateYourText method :
  • 3 parameters , text you want to translate , lang. from and lang. to
  • Creating an instance of TranslateClient
    TranslateClient client = new TranslateClient("");
  • Languages available written in English

anyhow , this method is one and only method and your translation is ready

Double Click on the Translate button and ...paste this

                    string translated = TranslateYourText(txt_from.Text, combo_from.Text, combo_to.Text);
                    txt_to.Text = translated;

if you have any questions , ask ..!

if you have any updates , the project is available for all code callers and every body make an update please write his name in the about

and thank you[ATTACH]4007[/ATTACH]

Attached Files


  • No comments:

    Post a Comment