Inline Language Translation to your Website

Learn how to add inline language translation to your website using Google AJAX language API. You may either put the languages in a drop down menu or use the country flags.

translation Google Translate, Windows Live Translator and Yahoo! Babelfish have made it very easy for web publishers to add language translation capability into their web pages. Include a line of Javascript code anywhere in your blog template and visitors from other countries will be able to translate and read your content in their own native language. Simple.

 

While these solutions work, the only problem is that when people translate your web pages, everything gets reloaded under a different URL and this is not the best user experience. You may therefore want to try this AJAX based language translation where the content is translated inline in real-time while the visitor remains on your site.

Inline Translation powered by Google AJAX Language API

Watch this screencast video to see how inline translation works or go here for a live demo.

 

There are many advantages offered by Google AJAX Language Translation API vis-a-vis the regular online translation service of Google. You are in complete control of page elements that are to be translated, all existing links and sharing features on your web page stay intact and best of all, visitors get a better experience.

If you liked what you just saw, it is something very easy to implement and internally uses Google API that you may use on any site without any restrictions.

How Inline Language Translation Works ?

The basic idea is that when a visitors clicks a language flag or selects a new language from the drop down, we invoke the Google Translate API and replace (actually hide) the existing text with the translated text returned by the Google API. The cycle is repeated when another languages is selected.

Your visitors can also switch to the original language of the web page anytime.

Add Google AJAX Language API in your Website

If you are ready to take the plunge, here’s what you need to do to get this working on your site.

Step 1: Edit your blog template and place everything that you want to translate inside a <div> tag with some unique id. - let’s say <div id="article">….</div>

translation-div

Step 2: Now place this code - <div id="translation"></div> - near the article div you configured in Step 1. The second div will actually house your translated text so you may add it just above or below the article div.

Step 3: And here’s the actual translation code. If this looks geeky, don’t worry too much - just copy-paste it inside the <head> tag of your blog template as it is.

<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
<script type="text/javascript">

// Initialize version 1.0 of Google AJAX API
google.load("language", "1");

function translate(lang) {
var source = document.getElementById("article").innerHTML;
var len = content.length;

// Google Language API accepts 500 characters per request
var words = 500;

// This is for English pages, you can change the
// sourcelang variable for other languages
var sourcelang = "en";
document.getElementById("translation").innerHTML = "";

for(i=0; i<=(len/words); i++) {
google.language.translate (source.substr(i*words, words),
"en", lang, function (result) {
if (!result.error) {
document.getElementById("translation").innerHTML
= document.getElementById("translation").innerHTML
+ result.translation;
} }); }

// Hide the text written in the original language
document.getElementById("article").style.display = 'none';
return false;
}

// Switch to the original language

function original() {
document.getElementById("translation").style.display='none';
document.getElementById("article").style.display = 'block';
return false;
}
</script>

Step 4: The last step is to include the translation flags or translation drop-down menu in your template.

I prefer having language names (see sidebar) instead of country flags because a flag may not always be the best representation of a language.

<a href="#" onclick="original();">Switch to English</a>
<select onchange="translate(this.options[this.selectedIndex].value);">
<option value="de">deutsch</option>
<option value="pt">português</option>
<option value="fr">français</option>
<option value="ja">???</option>
<option value="ar">???????</option>
<option value="it">italiano</option>
<option value="ru">p??????</option>
<option value="po">polski</option>
<option value="zh-CN">??</option>
<option value="es">español</option>
<option value="ko">???</option>
<option value="nl">nederlands</option>
<option value="hi">?????? </option>
<option value="el">????????</option>
<option value="ro">român?</option>
</select>

Related CTI Applications Articles

Microsoft ERP, CRM, Oracle Teleconference Deal


Freiburg, Germany-based Jedox, an open source software company, has announced the availability of Palo BI Suite 3.0, described by officials as a corporate performance management product for a "single version of truth." Three chords and red guitar...

Read more about Microsoft ERP, CRM, Oracle Teleconference Deal...

Aurora and Google Chrome


We didn’t collaborate with anyone from Google on the Aurora project to envision the future of the web, so we were especially interested to see their new browser, Google Chrome, and the ways in which it echoes some of the same ideas we came up with...

Read more about Aurora and Google Chrome...

Search Content


Content Categories


WhitePapers


Oracle Magazine

Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest...Read More


Sales Force Automation Comparison Guide

Businesses of all sizes can benefit by automating all aspects of their sales processes with an SFA (Sales Force Automation) solution. But due to the sheer number of features that most SFA solutions...Read More


Which CMS Is Right For Me?

If you're wondering which CMS is the right one for your organization, this comprehensive guide will take you through the various options available, detailing the pros and cons of each. Download...Read More


How to Buy a Phone System

Considering a new phone system for your business? The Phone System Buyer's Guide from VoIP-News provides you with all of the information you need to make a more informed decision. The Guide helps you...Read More




View All Whitepapers