Blog

/

Instructions

/

Machine (automated) translation in Open Real Estate CMS

Machine (automated) translation in Open Real Estate CMS

Machine translation is used after "Translate" button is clicked

After this button is clicked, script sends a request to Google Translate with cUrl, and then the translated line returns. High quality of this translation is not guaranteed.

Recently this functionlaity stopped working.

What happened? Officially Google Translate API was shut down in 2011. However, with library GoogleTranslate we kept the function working for a few years more.

Even considering the fact that using Google Translate API is not free (https://cloud.google.com/translate/v2/pricing), configuring Google translator requires a few steps. Non-experienced users will find this procedure complicated, and maybe even unachievable.

That's why we decided to give up using Google Translate API and started working with MyMemory API

Translation request may contain up to 500 bytes of data (http://mymemory.translated.net/doc/spec.php). This means that a line length can reach 80 - 500 characters (depending on the language and some parameters).

The service can be used free of charge and doesn't require registration and a key, what is an advantage for those, who use our product.

However, free plan of the service is limited by 1000 words per day (http://mymemory.translated.net/doc/usagelimits.php)



To start using the new translator, you should perform the following steps:

  • Download file MyMemoryTranslated.php
  • Login to FTP of the site
  • Copy file MyMemoryTranslated.php in folder protected/components
  • Change contents of the file protected\modules\lang\controllers\MainController.php to:

    <?php
    /* * ********************************************************************************************
    *								Open Real Estate
    *								----------------
    * 	version				:	%TAG%
    * 	copyright			:	(c) %YEAR% Monoray
    * 							https://monoray.net
    *							https://monoray.ru
    *
    * 	website				:	https://open-real-estate.info/en
    *
    * 	contact us			:	https://open-real-estate.info/en/contact-us
    *
    * 	license:			:	https://open-real-estate.info/en/license
    * 							https://open-real-estate.info/ru/license
    *
    * This file is part of Open Real Estate
    *
    * ********************************************************************************************* */
     
    class MainController extends ModuleUserController {
    public $modelName = 'Lang';
     
    public function actionAjaxTranslate(){
    if(!Yii::app()->request->isAjaxRequest)
    throw404();
     
    $fromLang = Yii::app()->request->getPost('fromLang');
    $fields = Yii::app()->request->getPost('fields');
    $errors = false;
    $translateField = array();
     
    if(!$fromLang || !$fields)
    throw new CException('Lang no req data');
     
    $translate = new MyMemoryTranslated();
    $fromVal = $fields[$fromLang];
     
    foreach($fields as $lang=>$val){
    if($lang == $fromLang)
    continue;
     
    if ($answer = $translate->translateText($fromVal, $fromLang, $lang))
    $translateField[$lang] = $answer;
    else
    $errors = true;
    }
     
    if ($errors) {
    echo json_encode(array(
    'result' => 'no',
    'fields' => ''
    ));
    }
    else {
    echo json_encode(array(
    'result' => 'ok',
    'fields' => $translateField
    ));
    }
    Yii::app()->end();
    }
    }



For version < 1.15.0 ( 1.14.x, 1.12.x, 1.11.x etc ) add the following code to the end of your protected\helpers\common.php file:

function getRemoteDataInfo($apiURL, $returnWithRes = false){
$rawData = '';
if( function_exists('curl_version')  ){
$ch = curl_init();
 
if(strtolower(substr($apiURL, 0, 5))=="https"){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}		
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
 
$rawData = curl_exec($ch);
 
if (!$returnWithRes)
curl_close($ch);
}
else {
$ctx = stream_context_create(array('http'=>
array(
'timeout' => 10, // 10 Seconds
)
));
$rawData = @file_get_contents($apiURL, false, $ctx);
}
 
 
if ($returnWithRes && isset($ch) && $ch) {
$answer = curl_getinfo($ch, $returnWithRes);
curl_close($ch);
return compact("rawData", "answer");
}
 
return $rawData;
}

Before changing the files we recommend you to make a backup (to save the copies of old files).