<?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>Threadbare Canvas Productions &#187; google translate</title>
	<atom:link href="http://threadbarecanvas.com/tag/google-translate/feed/" rel="self" type="application/rss+xml" />
	<link>http://threadbarecanvas.com</link>
	<description>Online Journal of James Hogan the Web Developer</description>
	<lastBuildDate>Sun, 01 Aug 2010 19:32:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Universal Translator for your Tweets</title>
		<link>http://threadbarecanvas.com/java-web/universal-translator-for-your-tweets/</link>
		<comments>http://threadbarecanvas.com/java-web/universal-translator-for-your-tweets/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 10:21:19 +0000</pubDate>
		<dc:creator>Siriquelle</dc:creator>
				<category><![CDATA[Java Web]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[google translate]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[twitter api]]></category>

		<guid isPermaLink="false">http://threadbarecanvas.com/?p=98</guid>
		<description><![CDATA[I thought this was going to be easy&#8230; SO much for preconceptions. Today, I&#8217;m going to give you the code to create a twitter translation App using, HTML, CSS, JavaScript and Java, the Twitter API and the Google Translate API. In a future tutorial I will demonstrate cross domain Ajax to get this Application to [...]]]></description>
			<content:encoded><![CDATA[<p>I thought this was going to be easy&#8230; SO much for preconceptions. Today, I&#8217;m going to give you the code to create a twitter translation App using, HTML, CSS, JavaScript and Java, the Twitter API and the Google Translate API. In a future tutorial I will demonstrate cross domain Ajax to get this Application to work totally on the client side but in the mean time you will need to have a Java enabled server to execute the server-side <abbr title="Java Server Page">JSP</abbr> that requests the API for twitter.</p>
<p>As you type your tweet the the JavaScript will analyze your keystrokes and check for when a space is entered assuming you are done typing a word, at this point the text is passed to Google for translation. When your done typing hit tweet and it&#8217;s off to twitter with your tweet.</p>
<p>This is what your app will look like when it&#8217;s finished:</p>
<p><a href="http://threadbarecanvas.com/wp-content/uploads/2009/07/translateApp.png"><img class="aligncenter size-full wp-image-99" title="translateApp" src="http://threadbarecanvas.com/wp-content/uploads/2009/07/translateApp.png" alt="translateApp" width="399" height="395" /></a></p>
<p style="text-align: center; font-size: 24px;"><strong><a href="http://threadbarecanvas.com/wp-content/uploads/2009/07/twitterLang.zip" target="_blank">Source</a><br />
</strong></p>
<p>The Folder structure you will need is similar to all other projects. You can download the source folder for this project and open it in NetBeans to get a better idea of how the end result will look. There are no special libraries you need to get this going just java.io.* and java.net.* .</p>
<p>This is the HTML you will need, (/index.html):</p>
<pre class="brush:html">&lt;!DOCTYPE html&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
        &lt;link rel="Stylesheet" media="screen" href="assets/css/style.css" &gt;
        &lt;title&gt;Twitter Universal Translate&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div id="content"&gt;
            &lt;div class="head"&gt;Twitter Multilanguage&lt;/div&gt;

            &lt;form&gt;
                &lt;label class="textlabel" for="username"&gt;Username&lt;/label&gt;
                &lt;input class="textinput" type="text" id="username" name="username" value=""&gt;

                &lt;label class="textlabel" for="password"&gt;Password&lt;/label&gt;
                &lt;input class="textinput" type="password" id="password" name="password" value=""&gt;

                &lt;label class="textlabel" for="lang"&gt;Translate To&lt;/label&gt;
                &lt;select class="select" id="lang" name="lang" onclick="translateTwo()" &gt;
                    &lt;option value="en"&gt;English&lt;/option&gt;
                    &lt;option value="fr"&gt;French&lt;/option&gt;
                    &lt;option value="it"&gt;Italian&lt;/option&gt;
                    &lt;option value="de"&gt;German&lt;/option&gt;
                &lt;/select&gt;

                &lt;label class="textlabel" for="textarea"&gt;Tweet&lt;/label&gt;
                &lt;textarea id="textarea" class="textarea" name="textarea" cols="5" rows="5" onblur="translateTwo()" onkeydown="translate(event)"&gt;&lt;/textarea&gt;

                &lt;input class="textinput" type="button" value="Tweet" onclick="callLoad()"/&gt;
            &lt;/form&gt;
            &lt;div class="infobar"&gt;“Translate your tweet into a chosen language as you type.”&lt;/div&gt;
            &lt;span id="status"&gt;&lt;/span&gt;
        &lt;/div&gt;

        &lt;script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
        &lt;script type="text/javascript" src="assets/js/jscript.js"&gt;&lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>This is the CSS you will need, (/assets/css/style.css):</p>
<pre class="brush:css">* {
    margin: 0;
    padding: 0;
}

/*///////////////////////////////////////////////////*/

#content{
    color:#333;
    width:350px;
    margin:100px auto;
    border:15px inset #66ffff;
    padding:10px;
    font-family:sans-serif;
    -moz-border-radius:10px;
}

.textlabel,textinput{
    display:block;
}

.textlabel{
    font-weight:bold;
}

.textlabel,.textinput, .textarea,.select{
    width:200px;
}

.textinput, .textarea,.select{
    padding:1px;
    margin-bottom:5px;
}

.textinput:focus, .textarea:focus{
    background-color:#ffffcc;
}

.head{
    font-size:32px;
    font-family:sans-serif;
    font-weight:bold;
    margin-bottom:5px;
}

.infobar{
    width:120px;
    float:right;
    position:relative;
    top:-140px;
    left:0px;
    padding: 3px;
    border:5px #ffff99 solid;
    background-color:#ffffcc;
    color:#003333;
    font-size:14px;
    font-style:oblique;
    text-align:center;
}</pre>
<p>This is the JavaScript you will need, (/assets/js/jscript.js):</p>
<pre class="brush:jscript">google.load("language", "1");

function translate(event){
    var textarea = document.getElementById("textarea");
    var lang = document.getElementById("lang");

    if(event.keyCode == 32){
        google.language.translate(textarea.value, "", lang.value, function(result) {
            if (!result.error) {
                var container = document.getElementById("textarea");
                container.value = result.translation + " ";
            }
        });
    }
}

function translateTwo(){
    var textarea = document.getElementById("textarea");
    var lang = document.getElementById("lang");
    google.language.translate(textarea.value, "", lang.value, function(result) {
        if (!result.error) {
            var container = document.getElementById("textarea");
            container.value = result.translation + " ";
        }
    });
}

function load(url, callback) {
    var xhr = new XMLHttpRequest();

    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    var status = document.getElementById("textarea").value;
    var auth = "tweet.jsp?username="+username+"&amp;password="+password+"&amp;status="+status;

    xhr.onreadystatechange = function() {
        if((xhr.readyState &lt; 4) || xhr.status !== 200) return;
        callback(xhr);
    };
    try{
        xhr.open('GET', auth, true);
        xhr.send();
    }catch(e){
        document.write(e);
    }
}

function callLoad(){
    load('tweet.jsp', function(xhr) {
        var joke = document.getElementById("status");
        var textarea = document.getElementById("textarea");
        joke.innerHTML = '<strong>OK</strong>';
        textarea.value = '';
    });
}</pre>
<p>This is the Java you will need, (/tweet.jsp):</p>
<pre class="brush:java">&lt;%@page import="java.net.*, java.io.*"%&gt;
&lt;%
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String status = request.getParameter("status");
        try {
            String twitteruser = username;
            String twitterpass = password;
            String encodedData = URLEncoder.encode(status, "UTF-8");

            String credentials = twitteruser + ":" + twitterpass;
            String encodecredentials = new sun.misc.BASE64Encoder().encode(credentials.getBytes());

            URLConnection connection = null;
            URL url = new URL("http://twitter.com/statuses/update.xml");
            connection = url.openConnection();
            connection.setRequestProperty("Authorization", "Basic " + encodecredentials);
            connection.setDoOutput(true);

            OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
            output.write("status=" + encodedData);
            output.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String decodedString;
            while ((decodedString = in.readLine()) != null) {
                out.println(decodedString);
            }
            in.close();

        } catch (Exception e) {
            System.out.println(e.toString());
        }

%&gt;</pre>
<p>Ideally a Servlet would be best suited to processing the response from twitter but for this project it&#8217;s OK. And there you go you can now tweet in multiple languages like James Bond might. yay!</p>
<p><map name='google_ad_map_98_d0b0fa17a4b5b092'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/98?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_98_d0b0fa17a4b5b092' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=98&amp;url= http%3A%2F%2Fthreadbarecanvas.com%2Fjava-web%2Funiversal-translator-for-your-tweets%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://threadbarecanvas.com/java-web/universal-translator-for-your-tweets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
