Google Translator using Windows forms
This latest version of GoogleTranslator utilizes Google Translate's AJAX APIs to translate text and retrieves the translation by parsing the returned JSON content.
What is it?
Sample Application beta Version |
GoogleTranslator is an object that allows you to translate text using the power of Google's online language tools. This demo allows you to easily perform a reverse translation. The app can be used as a poor man's resource translator for simple phrases, but you'd be wise to confirm the translation with a native speaker before using the results.
How do I create it?
string sourceLanguage =sourceLang;
string targetLanguage = targetLang;
string urlNew = string.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
sourceLanguage,
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(urlNew, outputFile);
}
How it works
Translate works by directly invoking Google's translation API called by its online translation form and parsing the results. After Getting result string we do below logic to show translated phrase .
// 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 + " "; }
Comments
Post a Comment