php gum translate class, translate your site with using google translate api

Started on the old forum in July 2011 · view the original in the archive · old links redirect here

1 reply · 3,194 views · started 15 years ago · 1 min read

2 participants
GumSlone Administrator Principal · 1,690 posts #1

with this class you can translate some parts of your site into different languages, this class also has caching, to cache the translated results.

<?php
/*
Simple google translate class with caching by GumSlone

http://coding-talk.com/f46/php-gum-translate-class-translate-your-site-with-using-google-translate-api-11593/

v 0.01
*/

class gum_translate
{

    var $from_lang = 'en';
    var $to_lang = '';
    var $translate = true;
    var $cache = false; // enable cache
    var $cache_folder = 'translate/'; //cache folder path should be chomped 0777, don't forget the last /
     var $cache_file = ''; //you can leave it empty, script will generate an own file name
    var $text = '';
    var $arr_key ='';
    var $key_length = 25; // if the key should be auto generated define its length here
    var $languages = array(
    'af','it',
    'sq','ja',
    'ar','ko',
    'be','lv',
    'bg','lt',
    'ca','mk',
    'zh','ms',
    'mt','no',
    'hr','fa',
    'cs','pl',
    'da','pt',
    'nl','en',
    'ro','et',
    'ru','tl',
    'sr','fi',
    'sk','fr',
    'sl','gl',
    'es','de',
    'sw','el',
    'sv','ht',
    'tl','iw',
    'th','hi',
    'tr','hu',
    'uk','is',
    'vi','id',
    'cy','ga','yi');
    
        function translate($text='',$arr_key='')
        {
            if(!empty($text))$this->text=$text;
            if(!empty($arr_key))$this->arr_key=$arr_key;
            
            if(!in_array($this->from_lang, $this->languages))$this->translate=false;
            elseif(!in_array($this->to_lang, $this->languages))$this->translate=false;
            elseif($this->to_lang==$this->from_lang)$this->translate=false;
            
            if($this->translate==true && !empty($this->text))
            {
                if($this->cache==true)
                {
                    $key = $this->generate_key();
                    $cache_file_path = $this->create_cache_file_path();
                    
                    if(file_exists($cache_file_path))
                    {
                        $contents = file_get_contents($cache_file_path);
                        $arr = json_decode($contents, true);
                    }
                    if(isset($arr[$key]))
                    {
                        return $arr[$key];
                    }
                    else
                    {
                        $translated_text = $this->google_translate();
                        $arr[$key] = $translated_text;
                        file_put_contents($cache_file_path, json_encode($arr));
                        @chmod($cache_file_path, 0777);
                        @chown($cache_file_path, 'nobody');
                        return $translated_text;
                    }
                }
                else
                {
                    return $this->google_translate();
                }
            }
            else 
                return $this->text;
            
        }

        function google_translate()
        {
            $google_url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.urlencode($this->text).'&langpair='.$this->from_lang.'|'.$this->to_lang.'';
            $contents = file_get_contents($google_url);
            $arr = json_decode($contents, true);
            if($arr['responseStatus']==200 && !empty($arr['responseData']['translatedText']))
                return $arr['responseData']['translatedText'];
            else 
                return $this->text;
        }

        function create_cache_file_path()
        {
            if(empty($cache_file))
            {
                $cache_file = $this->from_lang.'_to_'.$this->to_lang;
                return $this->cache_folder.$cache_file;
            }
            else
            {
                return $this->cache_folder.$this->cache_file;
            }
        }

        function generate_key()
        {
            if(empty($this->arr_key))
            {
                return substr(md5($this->text),0,$this->key_length);
            }
            else return $this->arr_key;
        }

}

?>

usage example:

require_once('gtranslate.php');

$translate = new gum_translate;
$translate->to_lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2));
$translate->cache = true;
$translate->cache_folder = 'gum_translate_cache/'; //chmod 0777 it
//echo $translate->translate('everything is ok?');
echo '<a href="http://some.site/file.php">'.$translate->translate('Download free ringtones here').'</a>';

without cache

require_once('gtranslate.php');

$translate = new gum_translate;
$translate->to_lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2));
//echo $translate->translate('everything is ok?');
echo '<a href="http://some.site/file.php">'.$translate->translate('Download free ringtones here').'</a>';

the text "Download free ringtones here" will be automatically translated into the users language, default language is english.

all type of improvement suggestions are welcome, if you have some questions about the usage of this class - feel free to ask me.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

tkarpagamoorthy Newbie · 1 posts #2

how can i implement in my whole site

how can i implement in my whole site. we can give language name from database. so the overall site will be convert into that selected language. I need your suggestion ???

GumSlone wrote: with this class you can translate some parts of your site into different languages, this class also has caching, to cache the translated results.

<?php
/*
Simple google translate class with caching by GumSlone

http://coding-talk.com/f46/php-gum-translate-class-translate-your-site-with-using-google-translate-api-11593/

v 0.01
*/

class gum_translate
{

    var $from_lang = 'en';
    var $to_lang = '';
    var $translate = true;
    var $cache = false; // enable cache
    var $cache_folder = 'translate/'; //cache folder path should be chomped 0777, don't forget the last /
     var $cache_file = ''; //you can leave it empty, script will generate an own file name
    var $text = '';
    var $arr_key ='';
    var $key_length = 25; // if the key should be auto generated define its length here
    var $languages = array(
    'af','it',
    'sq','ja',
    'ar','ko',
    'be','lv',
    'bg','lt',
    'ca','mk',
    'zh','ms',
    'mt','no',
    'hr','fa',
    'cs','pl',
    'da','pt',
    'nl','en',
    'ro','et',
    'ru','tl',
    'sr','fi',
    'sk','fr',
    'sl','gl',
    'es','de',
    'sw','el',
    'sv','ht',
    'tl','iw',
    'th','hi',
    'tr','hu',
    'uk','is',
    'vi','id',
    'cy','ga','yi');
    
        function translate($text='',$arr_key='')
        {
            if(!empty($text))$this->text=$text;
            if(!empty($arr_key))$this->arr_key=$arr_key;
            
            if(!in_array($this->from_lang, $this->languages))$this->translate=false;
            elseif(!in_array($this->to_lang, $this->languages))$this->translate=false;
            elseif($this->to_lang==$this->from_lang)$this->translate=false;
            
            if($this->translate==true && !empty($this->text))
            {
                if($this->cache==true)
                {
                    $key = $this->generate_key();
                    $cache_file_path = $this->create_cache_file_path();
                    
                    if(file_exists($cache_file_path))
                    {
                        $contents = file_get_contents($cache_file_path);
                        $arr = json_decode($contents, true);
                    }
                    if(isset($arr[$key]))
                    {
                        return $arr[$key];
                    }
                    else
                    {
                        $translated_text = $this->google_translate();
                        $arr[$key] = $translated_text;
                        file_put_contents($cache_file_path, json_encode($arr));
                        @chmod($cache_file_path, 0777);
                        @chown($cache_file_path, 'nobody');
                        return $translated_text;
                    }
                }
                else
                {
                    return $this->google_translate();
                }
            }
            else 
                return $this->text;
            
        }

        function google_translate()
        {
            $google_url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.urlencode($this->text).'&langpair='.$this->from_lang.'|'.$this->to_lang.'';
            $contents = file_get_contents($google_url);
            $arr = json_decode($contents, true);
            if($arr['responseStatus']==200 && !empty($arr['responseData']['translatedText']))
                return $arr['responseData']['translatedText'];
            else 
                return $this->text;
        }

        function create_cache_file_path()
        {
            if(empty($cache_file))
            {
                $cache_file = $this->from_lang.'_to_'.$this->to_lang;
                return $this->cache_folder.$cache_file;
            }
            else
            {
                return $this->cache_folder.$this->cache_file;
            }
        }

        function generate_key()
        {
            if(empty($this->arr_key))
            {
                return substr(md5($this->text),0,$this->key_length);
            }
            else return $this->arr_key;
        }

}

?>

usage example:

require_once('gtranslate.php');

$translate = new gum_translate;
$translate->to_lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2));
$translate->cache = true;
$translate->cache_folder = 'gum_translate_cache/'; //chmod 0777 it
//echo $translate->translate('everything is ok?');
echo '<a href="http://some.site/file.php">'.$translate->translate('Download free ringtones here').'</a>';

without cache

require_once('gtranslate.php');

$translate = new gum_translate;
$translate->to_lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2));
//echo $translate->translate('everything is ok?');
echo '<a href="http://some.site/file.php">'.$translate->translate('Download free ringtones here').'</a>';

the text "Download free ringtones here" will be automatically translated into the users language, default language is english.

all type of improvement suggestions are welcome, if you have some questions about the usage of this class - feel free to ask me.

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close