Google translation utilities

view a random? 987 views

I was going to use the global translator plugin for my translation needs, but this two has a couple of problems:

  1. It tends to make Google (and other sites) suspect that it is a virus, so they stop supplying translation services.
  2. It uses the translations services in a deceitful manner. Parsing the translation response, removing links etc.

The tool does have bonuses:

  1. Translated pages look like they are part of your site!
  2. Caching can be used to optimise response time (reducing the suspicions of viral activity).

I decided the benefits of the tool are outweighed by the negatives and I didn’t want to implement a tool that exploited Google services in any way; considering I may run Google AdSense on this blog as well! So what other options are there?

Google has recently released an AJAX translation API that can be used to translate an text on the fly. This is very cool, but it doesn’t like translating blocks of HTML.

I could have just placed translation links on the sidebar or even pasted in the google languages dropdown widget. Though I’ve seen the coolness of the Global translation widget, so the Google component looked less appealing. It isn’t that easy to put your own links in.

  1. Google translate finds all of those and adds translations to them, so the French flag will still translate into German, if you translate into German first.
  2. How do you know what page you are viewing?

Luckily, I had the global translation plugin to leverage off. I didn’t want to follow the same implementation strategy, but I did want the same information about the content and I wanted the flags!

Google translation utility implementation

  1. I created a new directory under my blogs root directory called ‘images’, I moved all of the flags images (from the global translation plugin) there.
  2. I added the get_self_url function from the global translation plugin to the functions.php file of my theme, I also renamed it:
    function translate_get_self_url()
    {
    $full_url = 'http';
    $script_name = '';
    if (isset($_SERVER['REQUEST_URI'])) {
    $script_name = $_SERVER['REQUEST_URI'];
    } else {
    $script_name = $_SERVER['PHP_SELF'];
    if ($_SERVER['QUERY_STRING'] > ' ') {
    $script_name .= '?' . $_SERVER['QUERY_STRING'];
    }
    }
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
    $full_url .= 's';
    }
    $full_url .= '://';
    if ($_SERVER['SERVER_PORT'] != '80') {
    $full_url .= $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $script_name;
    } else {
    $full_url .= $_SERVER['HTTP_HOST'] . $script_name;
    }
    return $full_url;
    }
  3. I added a new DIV into my sidebar template, to be drawn regardless of widgets that I might have added:
    <div id="langs" style="text-align:center;">
    <br/>
    </div>
  4. I added some JavaScript to the end of my Footer template, just above my Google analytics scripts:
    <script type="text/javascript">
    <?php $full_self = translate_get_self_url(); ?>
    var langElement = document.getElementById("langs");
    if(langElement){
    langElement.innerHTML = '<a href="<?php echo $full_self;?>"><img src="/james/images/flag_en.png" alt="English" title="English"/></a> ' +
    '<a href="http://translate.google.com/translate_c?hl=en&langpair=en|de&u=<?php echo $full_self;?>"><img src="/james/images/flag_de.png" alt="Deutsch" title="Deutsch" /></a> ' +
    '<a href="http://translate.google.com/translate_c?hl=en&langpair=en|fr&u=<?php echo $full_self;?>"><img src="/james/images/flag_fr.png" alt="Français" title="Français" /></a> ' +
    '<a href="http://translate.google.com/translate_c?hl=en&langpair=en|es&u=<?php echo $full_self;?>"><img src="/james/images/flag_es.png" alt="Español" title="Español" /></a> ' +
    '<a href="http://translate.google.com/translate_c?hl=en&langpair=en|zh-CN&u=<?php echo $full_self;?>"><img src="/james/images/flag_zh-CN.png" alt="中文" title="中文" /></a>';
    }
    </script>

Then everything worked nicely. Writing the translations buttons after load, using JavaScript, allows them to bypass Google Translation. So the user can read in French and return to English, or try Chinese.

Last 5 posts by James Little
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MisterWong
  • Reddit
  • Scoopit
  • StumbleUpon
  • Technorati
  • rss

Related Posts:

Post a Comment

Your email is never published nor shared.