Translate Any Page with Yahoo!

The World Wide Web has pages in every language, and Yahoo! can help you break through the language barrier.

Because the Web is a global space, we’ve all come across pages in different languages, especially among search results. If you’re searching for information about a phrase like hamburger recipe, it’s strange to come across a page about it in German. It’s stranger still to find a mention of your name on a page in a foreign language. Imagine my surprise when I was searching Yahoo! for my name and found it at the Russian site shown in Figure 1-10.

Russian text with my name (Paul Bausch)

Figure 1-10. Russian text with my name (Paul Bausch)

I can’t read Russian, so of course I had no idea what the text said. I had recently added a photo gallery of old radio dials to my web site; I could tell they were linking to it, but I wanted to know what they were saying.

Yahoo! Language Tools

Yahoo!’s Language Tools page (http://tools.search.yahoo.com/language) has some ways to help you work with other languages. Among them is a translation service that will translate any block of text to a different language. I copied the Russian text from Figure 1-10, pasted it into the text area labeled “Translate this web text,” chose “From Russian to English” from the drop-down list of languages, and clicked Translate. Yahoo! responded with this:

Radio Dials. The gallery of the photographs of ancient, I will not be
afraid this word, radios-scale. The author of collection, photographer
Paul Bausch, decided thus to publish the paternal collection of radio
receivers. 3x, dreams about their own tsifrozerkalke with the
macro-objective become increasingly more importunately.

As you can see, the Yahoo! translation tool isn’t perfect, but it’s good enough to give a sense of what the page is talking about. The translated text refers to the “paternal collection” of photos, because the radios I photographed belonged to my father. I still have no idea what the last sentence of the translation means, but I’m closer to understanding now than when it was in Russian.

Tip

If you’d like to limit the search results that Yahoo! returns to one language or a handful of languages, you can set your preferred languages in your search preferences. By default, Yahoo! returns the best search results from any language.

A faster way to translate any page you find in Yahoo! Search results is via the “Translate this page” link included within the results for non-English pages. Figure 1-11 highlights the link in the search results.

The “Translate this page” link in Yahoo! Search results

Figure 1-11. The “Translate this page” link in Yahoo! Search results

Clicking the “Translate this page” link takes you to a translated version of the page, rather than the page in its original language.

If you find yourself translating pages frequently, there are some ways to speed up the process. You can translate an entire web page by copying and pasting the URL into the field labeled “Translate this web page” on the Yahoo! Language Tools page, choosing the language from the drop-down menu, and clicking Translate. Yahoo! will display the page with all of the text translated.

Another quick way to translate entire pages is with the Yahoo! Toolbar [Hack #12] . If you’ve already installed the Yahoo! Toolbar, choose Add/Edit Buttons… from the Toolbar Settings button to bring up the Customize Yahoo! Toolbar page. Check the box next to Translate Current Page under Search & Navigation and then click Finished. You should now see a small yellow fish button, like the one shown in Figure 1-12, on your toolbar.

The Translate button on the Yahoo! Toolbar

Figure 1-12. The Translate button on the Yahoo! Toolbar

From any web page, you can click the Translate button and Yahoo! will display a version in English. Yahoo! will also automatically detect the source language, so you don’t need to choose a language from a menu. This is also handy if you can’t tell what language the page is in. If you just want to translate a block of text instead of the entire page, you can click the arrow next to the fish and choose Language and Translation Tools from the menu; you’ll go to the Yahoo! Language Tools page, where you can paste the text you want to translate into the translation form.

If the Yahoo! Toolbar isn’t your style and copying and pasting into the Language Tools isn’t fast enough, you can create your own Translate button with an understanding of Yahoo! Translation URLs and a JavaScript bookmarklet.

A Translation Bookmarklet

If you visit the Yahoo! Language Tools page and translate a page by URL, you’ll end up at a Yahoo! page that uses frames. The top frame includes the Yahoo! Search logo and several links: View Original, Print Translation, Language Tools, and so on. The bottom frame is the original web page, with all of the text translated into a different language. Figure 1-13 shows such a translated page, with the O’Reilly Hacks page in Spanish in the bottom frame.

The Hacks site translated into Spanish

Figure 1-13. The Hacks site translated into Spanish

Looking at the URL in the address bar, you’ll see that you’re at a Yahoo! page with some parameters passed to it in the querystring, including the URL of the page to be translated. The whole URL looks like this:

	http://tools.search.yahoo.com/language/translation/translatedPage. 
	php?tt=url&
                  urltext=http%3A%2F%2Fhacks.oreilly.com%2F&
                  lp=en_es

Here are the three variables passed in the URL:

tt

The type of translation to perform

urltext

The URL of the page to translate

lp

A code that represents the language to translate from and to

Knowing how to build Yahoo! Translate URLs, you can create a bookmarklet to fetch the current page URL, construct the proper Yahoo! Translate URL, and open it in a new window.

The code.

As with other Yahoo! bookmarklets throughout this book, this JavaScript is not very readable, but is condensed to work within the confines of a browser bookmark. Create a new bookmark in your browser and then bring it up for editing. Put the following code into the location field of the bookmark and be sure to give the bookmarklet a descriptive name, such as Yahoo! Translate:

	javascript:d=document;void(window.open('http://tools.search.yahoo.com/  
	language/translation/translatedPage.php?tt=url&urltext='+escape(d.location.  
	href)+'&lp=xx_en','_blank','width=640,height=480,status=yes,resizable=yes,  
	scrollbars=yes'))

The value of the lp variable in this code is important to note. The value xx_en tells Yahoo! to determine the language of the page you’ve sent it to translate. Of course, you could make this a language-specific bookmarklet by replacing xx_en with es_en (Spanish to English), fr_de (French to German), or any one of the other codes for the translations that Yahoo! supports. To view a full list of translation codes, simply view the source of the Language Tools page and look at the value attribute in the <option> tags that display the language choices. There are a few option lists on the page, so look for the list that contains values in the xx_yy format.

Running the hack.

To run the code, browse to a foreign-language page and click Yahoo! Translate from your bookmarks. A new browser window will open, showing a translated version of the original page. Figure 1-14 shows a Japanese page in the background that has been translated into English in the foreground.

A page that has been translated from Japanese to English

Figure 1-14. A page that has been translated from Japanese to English

The automatic translation might provide less-than-fluid English, but if you ever find your name on a page in a language you don’t speak, you can find out why with a single click.

Get Yahoo! Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.