Introduction
A few days ago, I updated my Internet Explorer 7 browser to the latest Internet Explorer 8. Naturally thereafter, I was checking out the "Accelators", a new feature in IE8, when I stumbled upon the translation accelator via Windows Live. Right away I thought that was very cool, and started looking into other translation accelators offered by IE8. I played around with the web translators for a while and found out that the translation works to a certain extent. All web translators pretty much translate word-per-word rather than by context, which is understandable.
Anyway, I still thought it would be cool to be able to translate a website or parts of a website without saving all the translations in a database. So with that, I started surfing the internet looking for a free API, and lo and behold, I found the Google AJAX Language API, click here to check it out. Look at a snapshot below to see how an English phrase can be translated to various languages.
Requirements
We will translate a few phrases from English to other languages using Google AJAX Language API's Rest Interface described here.
The Fun Begins!
To start of, you need to familiarize yourself with the languages that Google supports which is shown in the Languages Enum. Yes I know, it's a lot. According to terms of use, that you will not "submit any request exceeding 5000 characters in length, to the translation components of the Service". So with that out of the way, let's start coding.
Create An Enum Class
First, we will create our own enum
class for the languages based from Google's Languages Enum, as mentioned and shown above. This is really straight forward. See the code below:
1 using System;
2
3 public class Language
4 {
5 public const string AFRIKAANS = "af";
6 public const string ALBANIAN = "sq";
7 public const string AMHARIC = "am";
8 public const string ARABIC = "ar";
9 public const string ARMENIAN = "hy";
10 public const string AZERBAIJANI = "az";
11 public const string BASQUE = "eu";
12 public const string BELARUSIAN = "be";
13 public const string BENGALI = "bn";
14 public const string BIHARI = "bh";
15 public const string BULGARIAN = "bg";
16 public const string BURMESE = "my";
17 public const string CATALAN = "ca";
18 public const string CHEROKEE = "chr";
19 public const string CHINESE = "zh";
20 public const string CHINESE_SIMPLIFIED = "zh-CN";
21 public const string CHINESE_TRADITIONAL = "zh-TW";
22 public const string CROATIAN = "hr";
23 public const string CZECH = "cs";
24 public const string DANISH = "da";
25 public const string DHIVEHI = "dv";
26 public const string DUTCH = "nl";
27 public const string ENGLISH = "en";
28 public const string ESPERANTO = "eo";
29 public const string ESTONIAN = "et";
30 public const string FILIPINO = "tl";
31 public const string FINNISH = "fi";
32 public const string FRENCH = "fr";
33 public const string GALICIAN = "gl";
34 public const string GEORGIAN = "ka";
35 public const string GERMAN = "de";
36 public const string GREEK = "el";
37 public const string GUARANI = "gn";
38 public const string GUJARATI = "gu";
39 public const string HEBREW = "iw";
40 public const string HINDI = "hi";
41 public const string HUNGARIAN = "hu";
42 public const string ICELANDIC = "is";
43 public const string INDONESIAN = "id";
44 public const string INUKTITUT = "iu";
45 public const string ITALIAN = "it";
46 public const string JAPANESE = "ja";
47 public const string KANNADA = "kn";
48 public const string KAZAKH = "kk";
49 public const string KHMER = "km";
50 public const string KOREAN = "ko";
51 public const string KURDISH = "ku";
52 public const string KYRGYZ = "ky";
53 public const string LAOTHIAN = "lo";
54 public const string LATVIAN = "lv";
55 public const string LITHUANIAN = "lt";
56 public const string MACEDONIAN = "mk";
57 public const string MALAY = "ms";
58 public const string MALAYALAM = "ml";
59 public const string MALTESE = "mt";
60 public const string MARATHI = "mr";
61 public const string MONGOLIAN = "mn";
62 public const string NEPALI = "ne";
63 public const string NORWEGIAN = "no";
64 public const string ORIYA = "or";
65 public const string PASHTO = "ps";
66 public const string PERSIAN = "fa";
67 public const string POLISH = "pl";
68 public const string PORTUGUESE = "pt-PT";
69 public const string PUNJABI = "pa";
70 public const string ROMANIAN = "ro";
71 public const string RUSSIAN = "ru";
72 public const string SANSKRIT = "sa";
73 public const string SERBIAN = "sr";
74 public const string SINDHI = "sd";
75 public const string SINHALESE = "si";
76 public const string SLOVAK = "sk";
77 public const string SLOVENIAN = "sl";
78 public const string SPANISH = "es";
79 public const string SWAHILI = "sw";
80 public const string SWEDISH = "sv";
81 public const string TAJIK = "tg";
82 public const string TAMIL = "ta";
83 public const string TAGALOG = "tl";
84 public const string TELUGU = "te";
85 public const string THAI = "th";
86 public const string TIBETAN = "bo";
87 public const string TURKISH = "tr";
88 public const string UKRAINIAN = "uk";
89 public const string URDU = "ur";
90 public const string UZBEK = "uz";
91 public const string UIGHUR = "ug";
92 public const string VIETNAMESE = "vi";
93 public const string UNKNOWN = "";
94
95 public Language()
96 {
97 }
98 }
The Translation Method
We will also create a method that returns a translated string
. In this example, the method is inside my test ASP.NET Page, that's why it's private
, I recommend moving it to a public
class, and make it static
so other pages will have access to this method.
30 private string Translate(string stringToTranslate,
string fromLanguage, string toLanguage)
31 {
32
33 if (!String.IsNullOrEmpty(stringToTranslate))
34 {
35
36
37 if (stringToTranslate.Length <= 5000)
38 {
39 const int bufSizeMax = 65536;
40 const int bufSizeMin = 8192;
41
42 try
43 {
44
45 string requestUri = "http://ajax.googleapis.com/
ajax/services/language/translate?v=1.0&q=" +
46 stringToTranslate + "&langpair=" +
47 fromLanguage + "%7C" + toLanguage;
48
49
50 HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(requestUri);
51 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
52 Stream responseStream = response.GetResponseStream();
53
54
55 int length = (int)response.ContentLength;
56 int bufSize = bufSizeMin;
57
58 if (length > bufSize)
59 bufSize = length > bufSizeMax ? bufSizeMax : length;
60
61
62 byte[] buf = new byte[bufSize];
63 StringBuilder sb = new StringBuilder(bufSize);
64
65
66 while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)
67 {
68 sb.Append(Encoding.UTF8.GetString(buf, 0, length));
69 }
70
71
72
"responseDetails": null, "responseStatus": 200}
73
74 string translatedText = sb.Remove(0, 36).ToString();
75 translatedText = translatedText.Substring(0,
translatedText.IndexOf("\"},"));
76
77 return translatedText;
78 }
79 catch
80 {
81 return "Cannot get the translation. Please try again later.";
82 }
83 }
84 else
85 {
86 return "String to translate must be less than 5000 characters long.";
87 }
88 }
89 else
90 {
91 return "String to translate is empty.";
92 }
93 }
Notice that the method accepts three (3) parameters; the string
to translate, the language to translate from, and the language to translate to. The most important part of this code can be seen in lines 45-52. Lines 45-47 build a URL that sends a request and gets the response shown in lines 50-52. Because we're accessing the API via Google's Rest Interface, the response is returned through a Stream
object. This is a lot easier to code as compared to web services. The returned string
encoded from the stream
is cleaned-up (lines 74-75) and returned as the translated string
.
Ready to Translate
Translating is again very straight forward. We simply call the Translate
method that we built above to get translated string
s.
9 protected void Page_Load(object sender, EventArgs e)
10 {
11 string stringToTranslate = "Where do you live?
What's your name? My name is Junnark.";
12 Response.Write("<b>English:</b> " + stringToTranslate + "<br/><br/>");
13
14 string translatedString =
Translate(stringToTranslate, Language.ENGLISH, Language.FILIPINO);
15 Response.Write("<b>Filipino:</b> " + translatedString + "<br/><br/>");
16
17 translatedString =
Translate(stringToTranslate, Language.ENGLISH, Language.SPANISH);
18 Response.Write("<b>Spanish:</b> " + translatedString + "<br/><br/>");
19
20 translatedString =
Translate(stringToTranslate, Language.ENGLISH, Language.CHINESE);
21 Response.Write("<b>Chinese:</b> " + translatedString + "<br/><br/>");
22
23 translatedString =
Translate(stringToTranslate, Language.ENGLISH, Language.FRENCH);
24 Response.Write("<b>French:</b> " + translatedString + "<br/><br/>");
25
26 translatedString =
Translate(stringToTranslate, Language.ENGLISH, Language.JAPANESE);
27 Response.Write("<b>Japanese:</b> " + translatedString + "<br/><br/>");
28 }
Last Words
That was quick and fun. Of course, we didn't have to make the Language Enum
class, but, because of it, our code is a little bit more elegant.
As always, the code and the article are provided "As Is", there are absolutely no warranties. Use at your own risk.
Note: The original article can be seen here.
Happy coding!
History
- 2nd April 2009: Initial post