Yii Framework Forum: soporte automatico i18n en la clase menssage comand - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

soporte automatico i18n en la clase menssage comand api google translator+ messagecomand class Rate Topic: -----

#1 User is offline   Sangitario 701 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 15-March 10

Posted 15 March 2010 - 09:17 AM

una simple clase con soporte de automatico de idiomas y con una config por defecto :D... pero el codigo habla por si solo
...

bueno esto a mi me ha sido muy util y espero q en un futuro venga en el comando message de yii

por ahora lo dejo como message2

actualizado unos errores y ahora soporta por defecto la carpeta themes :D

Quote

<?php
/**
* MessageCommand class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2010 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

/**
* MessageCommand extracts messages to be translated from source files.
* The extracted messages are saved as PHP message source files
* under the specified directory.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: MessageCommand.php 1878 2010-03-10 21:19:30Z qiang.xue $
* @package system.cli.commands
* @since 1.0
*/
class Message2Command extends CConsoleCommand
{
/** change log
*sorry for my poor english
*bool activate or deactivate autotranslate , array caching for speed
*/

private $_injectTranslate=false;
private $_cache=array();


public function getHelp()
{
return <<<EOD
USAGE
yiic message <config-file>

DESCRIPTION
This command searches for messages to be translated in the specified
source files and compiles them into PHP arrays as message source.

PARAMETERS
* config-file: required, the path of the configuration file. The file
must be a valid PHP script which returns an array of name-value pairs.
Each name-value pair represents a configuration option. The following
options must be specified:
- sourcePath: string, root directory of all source files.
- messagePath: string, root directory containing message translations.
- languages: array, list of language codes that the extracted messages
should be translated to. For example, array('zh_cn','en_au').
- fileTypes: array, a list of file extensions (e.g. 'php', 'xml').
Only the files whose extension name can be found in this list
will be processed. If empty, all files will be processed.
- exclude: array, a list of directory and file exclusions. Each
exclusion can be either a name or a path. If a file or directory name
or path matches the exclusion, it will not be copied. For example,
an exclusion of '.svn' will exclude all files and directories whose
name is '.svn'. And an exclusion of '/a/b' will exclude file or
directory 'sourcePath/a/b'.
- translator: the name of the function for translating messages.
Defaults to 'Yii::t'. This is used as a mark to find messages to be
translated.

EOD;
}

/**
* Execute the action.
* @param array command line parameters specific for this command
*/
public function run($args)
{
// change log: default for work with yiic in protected folder and languages with default or just with -t xD
if($args[0]=='default' || ($args[0]=='-t' && $args[1]=$args[0] )){
echo 'Are you sure?.. It will work under \''.realpath('./').'\'? [Yes|No] ';
if(strncasecmp(trim(fgets(STDIN)),'y',1)) return;
$config=array(
'sourcePath'=>'./',
'messagePath'=>'./messages',
'languages'=>array('es','fr','de','ja','zh-CN','it'),
'fileTypes'=>array('php'),
);
}else{
if(!isset($args[0]))
$this->usageError('the configuration file is not specified.');
if(!is_file($args[0]))
$this->usageError("the configuration file {$args[0]} does not exist.");

$config=require_once($args[0]);
}

$translator='Yii::t';
extract($config);

if(!isset($sourcePath,$messagePath,$languages))
$this->usageError('The configuration file must specify "sourcePath", "messagePath" and "languages".');
if(!is_dir($sourcePath))
$this->usageError("The source path $sourcePath is not a valid directory.");
if(!is_dir($messagePath))
$this->usageError("The message path $messagePath is not a valid directory.");
if(empty($languages))
$this->usageError("Languages cannot be empty.");

// change log: activate with argument '-t'

if($this->_injectTranslate=isset($args[1]) && $args[1]=='-t')
$this->loadCache($messagePath);


$options=array();
if(isset($fileTypes))
$options['fileTypes']=$fileTypes;
if(isset($exclude))
$options['exclude']=$exclude;

$files=CFileHelper::findFiles(realpath($sourcePath),$options);
$files=array_merge(CFileHelper::findFiles(realpath($sourcePath.'../themes/'),$options),$files);


$messages=array();
foreach($files as $file)
$messages=array_merge_recursive($messages,$this->extractMessages($file,$translator));

foreach($languages as $language)
{
$dir=$messagePath.DIRECTORY_SEPARATOR.$language;
if(!is_dir($dir))
@mkdir($dir);
foreach($messages as $category=>$msgs)
{
$msgs=array_values(array_unique($msgs));
$this->generateMessageFile($msgs,$dir.DIRECTORY_SEPARATOR.$category.'.php');
}
}
}

protected function extractMessages($fileName,$translator)
{
echo "Extracting messages from $fileName...\n";
$subject=file_get_contents($fileName);
$n=preg_match_all('/\b'.$translator.'\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',$subject,$matches,PREG_SET_ORDER);
$messages=array();
for($i=0;$i<$n;++$i)
{
if(($pos=strpos($matches[$i][1],'.'))!==false)
$category=substr($matches[$i][1],$pos+1,-1);
else
$category=substr($matches[$i][1],1,-1);
$message=$matches[$i][2];
$messages[$category][]=eval("return $message;"); // use eval to eliminate quote escape
}
return $messages;
}

protected function generateMessageFile($messages,$fileName)
{
echo "Saving messages to $fileName...";
if(is_file($fileName))
{
$translated=require($fileName);
sort($messages);
ksort($translated);
if(array_keys($translated)==$messages)
{
echo "nothing new...skipped.\n";
return;
}
$merged=array();
$untranslated=array();
foreach($messages as $message)
{
if(!empty($translated[$message]))
$merged[$message]=$translated[$message];
else
$untranslated[]=$message;
}
ksort($merged);
sort($untranslated);
$todo=array();
foreach($untranslated as $message)
// change log: call the function to autotranslate
$todo[$message]=($this->_injectTranslate)?$this->getTranslate($fileName,$message):'';

ksort($translated);
foreach($translated as $message=>$translation)
{
if(!isset($merged[$message]) && !isset($todo[$message]))
$todo[$message]='@@'.$translation.'@@';
}
$merged=array_merge($todo,$merged);
$fileName.='.merged';
echo "translation merged.\n";
}
else
{
$merged=array();
foreach($messages as $message)
$merged[$message]=($this->_injectTranslate)?$this->getTranslate($fileName,$message):'';
ksort($merged);
if($this->_injectTranslate)
echo "\n saved.\n";
else
echo " saved.\n";
}
$array=str_replace("\r",'',var_export($merged,true));
$content=<<<EOD
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yiic message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* NOTE, this file must be saved in UTF-8 encoding.
*
* @version \$Id: \$
*/
return $array;

EOD;
file_put_contents($fileName, $content);
}


// change log: here is a little more of coding



public function loadCache($messagePath)
{
echo "Loading messages to cache...\n";
$files=CFileHelper::findFiles(realpath($messagePath),array('fileTypes'=>array('php')));
$messages=array();
foreach($files as $file){
$l=$this->getLanguage($file);
$messages=include($file);
foreach($messages as $message=>$translation)
if(!empty($translation) && empty($this->_cache[$l][$message]))
$this->_cache[$l][$message]=$translation;
}

}

protected function getLanguage($filename)
{
preg_match('/.*\\'.DIRECTORY_SEPARATOR.'(.*)\\'.DIRECTORY_SEPARATOR.'(.*)\.php/',$filename,$matches);
$matches=explode('_',$matches[1]);
return $matches[0];
}

//here is the magic :)
protected function getTranslate($file,$msg)
{
$l=$this->getLanguage($file);
if(isset($this->_cache[$l][$msg]) && !empty($this->_cache[$l][$message]))
return $this->_cache[$l][$msg];
preg_match_all('/(\{[^\}]*\})/',$msg,$matches);
$matches=$matches[0];
$tags=array();
foreach($matches as $k=>$v)
$tags[]=':%'.$k.'%';
$translate=str_replace($matches,$tags,$msg);

//google translator here
echo "\ntry connecting with google to translate $msg en->$l ";

$result=@file_get_contents('http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.urlencode($translate).'&langpair=en%7C'.$l);

if(!$result){
echo "error: can't connect!";
return '';
}
$result=function_exists('json_decode') ?json_decode($result): CJSON::decode($result);
if(is_object($result) && isset($result->responseData) && !empty($result->responseData->translatedText)){
$translate=$result->responseData->translatedText;
}else
echo "error: can't translate";


$translate=str_replace($tags,$matches,$translate);

return $translate;
}
}

0

#2 User is offline   robregonm 

  • Expert Yii Developer
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 594
  • Joined: 30-July 09
  • Location:Colombia

Posted 26 March 2010 - 02:30 PM

Buena idea, además creo que le sería útil la extensión al Comando Crud que he creado con soporte de múltiples idiomas e inmensidad de mejoras: iCrud (http://www.yiiframew...xtension/icrud/ ), recomendaciones y sugerencias son bienvenidas.
Saludos.
Ricardo Obregón

YiiFramework en Español - http://yiiframework.co/ - http://yiiframeworkenespanol.org/ - Yii Code Generator for Bootstrap
http://obregon.co/ - https://1server.co/
PHP 5.5+, nginx 1.7, MySQL(MariaDB & PerconaDB), PostgreSQL 9, Yii 2, CanJS
Follow me: @robregonm & @obregonco & @1ServerCo.
0

#3 User is offline   Sangitario 701 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 15-March 10

Posted 27 March 2010 - 08:21 AM

claro que si.. adelante !! cualquier duda o ayuda posteela o mandela por mp
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users