1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168:
<?php
class PHPShopLang {
var $langFile;
var $doLang=true;
var $UndefinedLangValue=array();
var $createUndefined=true;
var $charset="windows-1251";
var $API_KEY='ABQIAAAA6omrd4WlIP7ZNsUTTc6uYhS_ps6EECQtukvcCEaHG66DCa_iExReO2aXIGbXVvsP9gKzBbXapVrZWQ';
function __construct($_classPath) {
if(!empty($_SESSION['lang'])) {
PHPShopObj::loadClass("string");
$this->langFile = $_classPath.'locale/'.$_SESSION['lang'].'.ini';
if(is_file($this->langFile)) {
if($langArray = parse_ini_file($this->langFile,1)) {
$this->doLang = $this->check($langArray);
}
else echo "Error parsing locale ".$this->langFile;
}
}
else {
$this->doLang=false;
}
}
function check($langArray) {
$langName = array_keys($langArray);
if($_SESSION['lang'] != 'russian') {
$this->LangValue['lang'] = $langArray['locale'];
if(!empty($langArray['charset']['html'])) {
$this->charset = $langArray['charset']['html'];
$this->lang_name = $langArray['charset']['code'];
}
return true;
}
}
function gettext($value) {
if($this->doLang and !empty($value) and !preg_match("/([a-zA-Z0-9_\.-]){4,15}/",$value) and !is_numeric($value)) {
$sourceValue = $value;
$value = PHPShopString::toLatin($value);
if(isset($this->LangValue['lang'][$value])) $locValue = $this->LangValue['lang'][$value];
else {
$locValue = 'Und: '.strip_tags($value);
$this->UndefinedLangValue[strip_tags($value)] = $this->translate(strip_tags($sourceValue),$this->lang_name);
}
}else $locValue = $value;
if(!empty($locValue))
return $locValue;
else return $value;
}
function translate($text,$lang_name) {
$text = urlencode(PHPShopString::win_utf8($text));
$domain = "ajax.googleapis.com";
$result='';
$fp = fsockopen($domain, 80, $errno, $errstr);
if (!$fp) {
return exit("");
} else {
fputs($fp, "GET /ajax/services/language/translate?v=1.0&q=".$text."&langpair=ru%7C".$lang_name."&callback=foo&context=bar&key=".$this->API_KEY." HTTP/1.0\r\n");
fputs($fp, "Host: $domain\r\n");
fputs($fp, "Connection: close\r\n\r\n");
while (!feof($fp)) {
$result.=fgets($fp, 1000);
}
fclose($fp);
}
preg_match('|"translatedText":"(.*?)"|is', $result, $locale);
return $locale[1];
}
function write() {
$updateLang='';
if(is_array($this->UndefinedLangValue)) {
foreach($this->UndefinedLangValue as $key=>$val)
$updateLang.= $key.'="'.$val.'";
';
if($this->doLang) {
if (is_writable($this->langFile)) {
$fp = fopen($this->langFile, "a");
if ($fp) {
fputs($fp, $updateLang);
fclose($fp);
}
}
}
}
}
}
$GLOBALS['PHPShopLang'] = new PHPShopLang($GLOBALS['_classPath']);
$GLOBALS['PHPShopLangCharset'] = $GLOBALS['PHPShopLang']->charset;
function __($value) {
global $PHPShopLang;
return $PHPShopLang->gettext($value);
}
function writeLangFile() {
global $PHPShopLang;
if($PHPShopLang->createUndefined) $PHPShopLang->write();
}
?>