Difference between revisions of "Automatic Feed Translation"

From AwasuWiki
Jump to: navigation, search
(New page: =Automatic Feed Translation= Using the XSLT file listed below you can automatically translate a foreign language feed into your language, utilizing Google's Translation service, whenever ...)
 
Line 1: Line 1:
 
=Automatic Feed Translation=
 
=Automatic Feed Translation=
  
Using the XSLT file listed below you can automatically translate a foreign language feed into your language, utilizing Google's Translation service, whenever Awasu updates the associated Channel.
+
Using the XSLT file listed below you can automatically translate a foreign language feed into your language, utilizing [http://code.google.com/apis/ajaxlanguage/ Google's Translation API], whenever Awasu updates the associated Channel.  Here is the list of [http://code.google.com/apis/ajaxlanguage/documentation/#SupportedLanguages supported languages].
 +
 
 +
==Customising the XSLT file==
 +
 
 +
===Your language===
 +
There is a constant in the XSLT file called "yourLanguage" which you can change to a specific [http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray two-character language code] of your language. If your language is English, the XSLT file below should work well without any modification.
 +
 
 +
===Feed language===
 +
There is a constant in the XSLT file called "feedLanguage" which you can change to a specific [http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray two-character language code] if you know the language of the feed; this will improve Google's language translation accuracy.  Leaving the "feedLanguage" constant blank (''empty quotation marks: ""'') should still work as Google will attempt to guess the feed's langauge.
  
 
<pre>
 
<pre>
Line 18: Line 26:
 
   /*
 
   /*
 
   Change this constant (feedLanguage) to the two-character language code of the feed (if known).
 
   Change this constant (feedLanguage) to the two-character language code of the feed (if known).
   This will improve Google language translation accuracy.
+
   This will improve Google's language translation accuracy.
 
    
 
    
 
   Leave is blank (empty quotation marks: "") if the feed language is unknown or  
 
   Leave is blank (empty quotation marks: "") if the feed language is unknown or  
Line 27: Line 35:
 
   http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
 
   http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
 
   */
 
   */
   var feedLanguage = "pl";
+
   var feedLanguage = "";
 
    
 
    
 
   /*
 
   /*

Revision as of 07:42, 5 January 2010

Automatic Feed Translation

Using the XSLT file listed below you can automatically translate a foreign language feed into your language, utilizing Google's Translation API, whenever Awasu updates the associated Channel. Here is the list of supported languages.

Customising the XSLT file

Your language

There is a constant in the XSLT file called "yourLanguage" which you can change to a specific two-character language code of your language. If your language is English, the XSLT file below should work well without any modification.

Feed language

There is a constant in the XSLT file called "feedLanguage" which you can change to a specific two-character language code if you know the language of the feed; this will improve Google's language translation accuracy. Leaving the "feedLanguage" constant blank (empty quotation marks: "") should still work as Google will attempt to guess the feed's langauge.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:ktc="http://www.awasu.com/forums/profile.php?mode=viewprofile&u=24618" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <msxsl:script language="JScript" implements-prefix="ktc">
  <![CDATA[
  // Constants...

  // Don't edit these two constants until Google says otherwise.
  var baseURL = "http://ajax.googleapis.com/ajax/services/language/translate";
  var version = "v=1.0";

  /*
  Change this constant (feedLanguage) to the two-character language code of the feed (if known).
  This will improve Google's language translation accuracy.
  
  Leave is blank (empty quotation marks: "") if the feed language is unknown or 
  the feed contains multiple languages. Google will attempt to guess the langauge
  each time the translateLang() function is called.
  
  Supported language codes are listed here:
  http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
  */
  var feedLanguage = "";
  
  /*
  Change this constant (yourLanguage) to your desired two-character language code.
  Supported language codes are listed here:
  http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
  */
  var yourLanguage = "en";

  var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
  var cache = {}; // Case-sensitive results cache.
  
  function translateLang(textToTranslate) 
  {
    try
    {
      textToTranslate = textToTranslate.replace(/^\s+|\s+$/g,"");             // Remove leading and trailing whitespace.
      textToTranslate = textToTranslate.replace(/^\s*|\s(?=\s)|\s*$/g," "); // Replace repeated spaces, newlines and tabs with a single space.
      textToTranslate = textToTranslate.substr(0, 5000);

      if (cache[textToTranslate])      // It's it in the cache,
        return cache[textToTranslate]; // return it.

      if (textToTranslate)
      {
        //var fullURL = baseURL + languages + yourLanguage;

        xmlhttp.open("POST", baseURL, false);
        xmlhttp.setRequestHeader("Referer", "http://www.awasu.com/wiki/Feed_Auto_Translate");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.send("v=1.0&q=" + encodeURIComponent(textToTranslate) + "&langpair=" + feedLanguage + "|" + yourLanguage);
        eval("var response = " + xmlhttp.responseText);

        var results = new String(response.responseData.translatedText)
        cache[textToTranslate] = decodeURI(results); // Add the results to the cache.
        return decodeURI(results);
      }
      else // The element's value is a null string.
      {
        cache[textToTranslate] = "";
        return "";
      }
    }
    catch(e)
    {
      cache[textToTranslate] = e;
      return e;
    }
  }

  function itsBetterWithBacon(textToTranslate)
  {
    return "bacon: " + textToTranslate + " :bacon";
  }

]]>
</msxsl:script>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/rss/channel/title | /rss/channel/description | /rss/channel/item/title | /rss/channel/item/description | /rss/channel/item/content:encoded | /rss/channel/item/category">
    <xsl:variable name="elementName" select="name()" />
    <xsl:element name="{name(.)}"> <xsl:value-of select="ktc:translateLang(normalize-space(.))" /></xsl:element>
  </xsl:template>
</xsl:stylesheet>