<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>techno.blog(&#34;Dion&#34;) &#187; translation</title>
	<atom:link href="http://almaer.com/blog/tag/translation/feed" rel="self" type="application/rss+xml" />
	<link>http://almaer.com/blog</link>
	<description>blogging about life, the universe, and everything tech</description>
	<lastBuildDate>Tue, 28 Aug 2012 14:41:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Translate: Select any text in the browser and have it convert to English (or your language)</title>
		<link>http://almaer.com/blog/translate-select-any-text-in-the-browser-and-have-it-convert-to-english-or-your-language</link>
		<comments>http://almaer.com/blog/translate-select-any-text-in-the-browser-and-have-it-convert-to-english-or-your-language#comments</comments>
		<pubDate>Fri, 25 Apr 2008 07:01:37 +0000</pubDate>
		<dc:creator>dion</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[translation]]></category>

		<guid isPermaLink="false">http://almaer.com/blog/translate-select-any-text-in-the-browser-and-have-it-convert-to-english-or-your-language</guid>
		<description><![CDATA[
I really liked getting the Ajax Language API out into developers hands as god knows we shouldn&#8217;t have to worry about translations. Now we can use the API and have the Google back-end do all of the work.
I have recently had a couple of scenarios where I really wanted a quick translation. I had a [...]]]></description>
			<content:encoded><![CDATA[<p><img src='http://almaer.com/blog/uploads/translatebookmarklet.png' alt='Translate Bookmarklet' border='0'/></p>
<p>I really liked getting the <a href="http://code.google.com/apis/ajaxlanguage/">Ajax Language API</a> out into developers hands as god knows we shouldn&#8217;t have to worry about translations. Now we can use the API and have the Google back-end do all of the work.</p>
<p>I have recently had a couple of scenarios where I really wanted a quick translation. I had a few twitter messages pass through my stream in French and Spanish. I had the answer to some technical issues show up on foreign forums.</p>
<p>So, I decided to create a <a href="http://code.google.com/p/translate-bookmarklet/">Translate</a> bookmarklet that allows me to select any foreign text, click on the bookmark, and a little window pops up with the English translation if it can work it out. Automatic translation is far from perfect yet, but for many scenarios you can easily get the gist (e.g. you wouldn&#8217;t want to automatically convert a book).</p>
<p>This is how I created the bookmarklet:</p>
<p><b>The source</b></p>
<p>First, I have the raw JavaScript source that will become the bookmarklet. There are a few sections of the code. First, we setup a method that will go off and call the Ajax Language API, passing in the translation and language that we want. This is where you would <a href="http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray">change the language code</a> for non-English.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>window<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'apiLoaded'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  window.<span style="color: #660066;">apiLoaded</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> language <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;en&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> window.<span style="color: #660066;">getSelection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;language&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;callback&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        google.<span style="color: #660066;">language</span>.<span style="color: #660066;">detect</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>dresult<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>dresult.<span style="color: #660066;">error</span> <span style="color: #339933;">&amp;&amp;</span> dresult.<span style="color: #660066;">language</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            google.<span style="color: #660066;">language</span>.<span style="color: #660066;">translate</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">,</span> dresult.<span style="color: #660066;">language</span><span style="color: #339933;">,</span> language<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>tresult<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tresult.<span style="color: #660066;">translation</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                translationWindow<span style="color: #009900;">&#40;</span>tresult<span style="color: #339933;">,</span> dresult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'No translation found for &quot;'</span> <span style="color: #339933;">+</span> text <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; guessing the language: '</span> <span style="color: #339933;">+</span> dresult.<span style="color: #660066;">language</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then we setup a method that is able to display a window showing the result. I used the <a href="http://docs.prototype-ui.com/rc0/Window">Prototype UI Window</a> object if available, and good old <code>alert()</code> if not:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>window<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'translationWindow'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  window.<span style="color: #660066;">translationWindow</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>tresult<span style="color: #339933;">,</span> dresult<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'UI'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">new</span> UI.<span style="color: #660066;">Window</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>theme<span style="color: #339933;">:</span>  <span style="color: #3366CC;">&quot;black_hud&quot;</span><span style="color: #339933;">,</span>
                   shadow<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
                   width<span style="color: #339933;">:</span>  <span style="color: #CC0000;">350</span><span style="color: #339933;">,</span>
                   height<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setContent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;div style='padding:6px'&gt;&quot;</span> <span style="color: #339933;">+</span> tresult.<span style="color: #660066;">translation</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span>
                   .<span style="color: #660066;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;English Translation&quot;</span><span style="color: #009900;">&#41;</span>
                   .<span style="color: #660066;">setFooter</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Language detected: &quot;</span> <span style="color: #339933;">+</span> dresult.<span style="color: #660066;">language</span><span style="color: #009900;">&#41;</span>
                   .<span style="color: #660066;">center</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>top<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>tresult.<span style="color: #660066;">translation</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; [lang = &quot;</span> <span style="color: #339933;">+</span> dresult.<span style="color: #660066;">language</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Next, we load the Prototype UI window code, and accompanying CSS resources by dynamically adding the resources to the DOM:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>window<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'UI'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> pw <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'script'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pw.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://almaer.com/downloads/protowindow/protowin.js'</span><span style="color: #339933;">;</span>
  pw.<span style="color: #660066;">type</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">;</span>
  document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>pw<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> pwdefault <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pwdefault.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rel'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'stylesheet'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pwdefault.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'type'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'text/css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pwdefault.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://almaer.com/downloads/protowindow/themes/window.css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>pwdefault<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> pwblack <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pwblack.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rel'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'stylesheet'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pwblack.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'type'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'text/css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pwblack.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'http://almaer.com/downloads/protowindow/themes/black_hud.css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>pwblack<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, we load the Google API loader, and use the <a href="http://code.google.com/apis/ajax/documentation/#Dynamic">dynamic loading option</a> with the <code>?callback=apiLoaded</code>. This kicks off the main driver that we saw first, and if it is already loaded we call it directly (for multiple translations on the same page).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>window<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'google'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'script'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  s.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://www.google.com/jsapi?callback=apiLoaded'</span><span style="color: #339933;">;</span>
  s.<span style="color: #660066;">type</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">;</span>
  document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  apiLoaded<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>&#8220;Compilation&#8221;</b></p>
<p>This is the raw form, and we need to get <a href="http://code.google.com/p/translate-bookmarklet/source/browse/trunk/bookmarklet/translate-bookmarklet.js">the bookmarklet form</a>, which you can just use right away if you are wanting English. For this, I use John Grubber&#8217;s <a href="http://code.google.com/p/translate-bookmarklet/source/browse/trunk/bin/makebookmarklet">makebookmarklet</a> Perl script to do the conversion.</p>
<p><b>The Server</b></p>
<p>The Prototype UI code lives on the server, so I put a <a href="http://code.google.com/p/translate-bookmarklet/source/browse/trunk/server">striped down version over there</a> which just contains a combined Prototype + Window JavaScript file, and just the one theme CSS set.</p>
<p><b>In Action</b></p>
<p>Unsure what I am talking about? Just watch it in action:</p>
<p><object type="application/x-shockwave-flash" width="600" height="457" data="http://www.vimeo.com/moogaloop.swf?clip_id=939238&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=01AAEA"><param name="quality" value="best" /><param name="allowfullscreen" value="true" /><param name="scale" value="showAll" /><param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=939238&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=01AAEA" /></object></p>
<p><b>UPDATE:</b> I also implemented <a href="http://almaer.com/blog/twitter-translate-automatically-convert-tweets-to-your-language">Twitter Translate</a> to automatically convert tweets to your language.</p>
]]></content:encoded>
			<wfw:commentRss>http://almaer.com/blog/translate-select-any-text-in-the-browser-and-have-it-convert-to-english-or-your-language/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
