with this class you can translate some parts of your site into different languages,
this class also has caching, to cache the translated results.
usage example:
without cache
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.
this class also has caching, to cache the translated results.
PHP Code:
<?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;
}
}
?>
PHP Code:
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>';
PHP Code:
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>';
all type of improvement suggestions are welcome, if you have some questions about the usage of this class - feel free to ask me.
Comment