Jump to content

C# Translator [using GOOGLE API]

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Professor Green

Professor Green

    Newbie

  • Members
  • Pip
  • 5 posts
Hello

this thread is a full tutorial about making a Translator using Google'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


Edited by Professor Green, 14 July 2011 - 02:08 PM.
add the project src code


#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
it's funny that code.google is blocked, but google services are not. =)

Fun tutorial! great work! +rep

#3
Professor Green

Professor Green

    Newbie

  • Members
  • Pip
  • 5 posts
sam_coder , thank u

am gonna show you how does code.google appear here !!!
that's what I call real fun ;p
and all google services works perfect here , maps , translator ... everything
but no access for codes
Attached File  123132123.png   75.89K   113 downloads

#4
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
wow, that's weird. Not that I understand the specific politics surrounding that, or the laws in your area, but I betcha Tor could get you around that.

Tor Project: Anonymity Online

I know that at times when I've used this, which is perfectly legal where I am, I've hit Google's site, and it's redirected me to Google Russia or whatever.

Anyways, that's well beyond the scope of this tutorial, so I'll be quiet. =)

#5
mak_code

mak_code

    Newbie

  • Members
  • Pip
  • 1 posts
Hi all,

I run the above code,but unfortunally i got below error
[response status:403]Please use Translate v2. See Google Translate API v2 - Google Code

Code:
string input="Abdulrahman";
string Domain = "Domainname";
string username = "usename";
string password = "password";
string ur = "http://translate.google.com.sa/?hl=en&tab=wT#";
HttpWebRequest webRequest1 = WebRequest.Create(ur) as HttpWebRequest;
System.Net.NetworkCredential netcredt = new System.Net.NetworkCredential(username, password, Domain);
System.Net.WebResponse response = webRequest1.GetResponse();
TranslateClient cient = new TranslateClient(ur);
string trtad = cient.Translate(input, "en", "ar");

Can anyone tell me wts the problem..?

Thanks
Mak.

#6
adnan

adnan

    Newbie

  • Members
  • Pip
  • 3 posts
i m facing problem in design of this form please help me out...

#7
adnan

adnan

    Newbie

  • Members
  • Pip
  • 3 posts
i m getting runtime error "Check Your Internet Connection"..

#8
adnan

adnan

    Newbie

  • Members
  • Pip
  • 3 posts
is it required to have API key for this software to work ..plz let me know its urgent...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users