From: <hug...@li...> - 2011-08-29 05:46:05
|
branch: translation details: http://hugin.hg.sourceforge.net/hgweb/hugin/hugin-web/hgrepo/h/hu/hugin/hugin-web/rev/fbdd5551c88c changeset: 312:fbdd5551c88c user: Vaclav Cerny <va...@se...> date: Sun Aug 28 22:31:34 2011 +0200 description: Added documentation diffstat: g.php | 214 +++++++++++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 142 insertions(+), 72 deletions(-) diffs (343 lines): diff -r 7cdafe7cc2b6 -r fbdd5551c88c g.php --- a/g.php Sat Aug 27 15:29:10 2011 +0200 +++ b/g.php Sun Aug 28 22:31:34 2011 +0200 @@ -1,10 +1,33 @@ <?php -//php g.php && +//run using php g.php +/* +@file +This all-in-one php script is proof-of-concept of gettext translation of hugin website +it works by defining content negotiation maps (see http://httpd.apache.org/docs/current/mod/mod_negotiation.html#typemaps ) +(enabled in .htaccess). +Instead of .html files, there are now .page files (we must change internal links) +Each .page file is internally handled by apache. - +.page files are generated by this script. + +You can also use .image files - eg. for translatable screenshots + + + + +@author Vaclav Cerny va...@se... +@todo add license +*/ + + +/* +by hand generated list of all translatable pages. + +*/ +$list=array(); $list[]='index.shtml';//repair $list[]='news.html'; $list[]='ssi/menu.html'; @@ -51,6 +74,9 @@ $list[]='community/authors/index.shtml'; $list[]='community/charter/index.shtml'; +/* +Sample minimal translation set. In each language in $Languages array, you can define to translate more pages. Or even less. +*/ $minimal_list=array( 'index.shtml', 'news.html', @@ -58,13 +84,36 @@ ); +/* +See czech language - its commented; + + + + +@todo missing it fr es nl translations ( there are release notes with some of them) +*/ $Languages=array( -//it fr es nl + 'cs'=>array( - 'language'=>'cs', - 'codeset'=>'cs_CZ.UTF-8', - 'pages'=>$list, //pages to translate by gettext - 'translated-pages'=>array( //before gettext translations + 'language'=>'cs', //needed constant + 'codeset'=>'cs_CZ.UTF-8', //needed constant + 'pages'=>$list, /* + pages to translate by gettext for this language. + each translator than can decide, what to translate, + In czech language i use full list, but in code, + i am unsetting all tutorials - just as sample. + + For each language, there can be different set of translatable pages +*/ + + 'translated-pages'=>array(/* + List of translated pages. + Now, there are all handmade html translations, + which will be used instead of gettext generated translation. + + For this language, catalog will not include text fragments from this files. + +*/ 'releases/2011.2.0/en.shtml'=>'releases/2011.2.0/cs.shtml', 'releases/2010.0.0/en.shtml'=>'releases/2010.0.0/cs.shtml', 'releases/2009.4.0/en.shtml'=>'releases/2009.4.0/cs.shtml', @@ -102,19 +151,37 @@ ), ); + +/* +Czech tutorials won't be translated now . +*/ Foreach($Languages['cs']['pages'] as $Key=>$Value){ if(substr($Value, 0,5)=='tutor') unset($Languages['cs']['pages'][$Key]); } +/* +I mimmicks some part of javascript DOM Model +*/ require_once 'JSLikeHTMLElement.php'; + +/* +Now i will in single run translate files and generate fresh catalog. + +*/ Foreach($Languages as $LangId =>$Lang){ + + //generate .mo file, so i can use it from previous translations. exec('msgfmt translation/'.$Lang['language'].'/LC_MESSAGES/messages.po'); - + +//======================================== +// long block of inicialization of gettext + + + $domain = "messages"; - - + putenv ( "LANG=" . $Lang['language'] ); putenv ( "LANGUAGE=" . $Lang['language'] ); @@ -127,8 +194,14 @@ // runs domain with translations textdomain($domain); +//======================================== +//I will write to catalog for this language. +//@todo This file is temporary and can be deleted $catalog=fopen('translation/'.$Lang['language'].'/LC_MESSAGES/catalog.pot','w+'); + +//header. +//@todo add exact date fwrite($catalog, '# Hugin web translation # @@ -145,11 +218,13 @@ "Content-Transfer-Encoding: 8bit\n" '); +//with each page, which can be translated in current language + foreach ($Lang['pages'] as $file){ + if(!isset($Languages[$LangId]['translated-pages'][$file])) //if this page is not already translated + { + - foreach ($Lang['pages'] as $file){ - if(!isset($Languages[$LangId]['translated-pages'][$file])) - { - + //I will preapere new filename $newfilename=''; if(substr($file,-6)=='.shtml'){ $newfilename=str_replace('.shtml','.'.$Lang['language'].'.shtml',$file); @@ -157,45 +232,59 @@ $newfilename=str_replace('.html','.'.$Lang['language'].'.html',$file); } if($newfilename!=''){ - HandleFile($file,$newfilename); - $Languages[$LangId]['translated-pages'][$file]=$newfilename; + HandleFile($file,$newfilename); //I will add text from this file to catalog and from mo file to translated wersion + $Languages[$LangId]['translated-pages'][$file]=$newfilename; //add file to list of translated pages in current language } } - } -fclose($catalog); +fclose($catalog); //ok. catalog ready, but there are some duplicates + //so i will create wersion without duplicates exec('msguniq translation/'.$Lang['language'].'/LC_MESSAGES/catalog.pot > translation/'.$Lang['language'].'/LC_MESSAGES/catalog-u.pot'); + + //if there is translation file if (file_exists('translation/'.$Lang['language'].'/LC_MESSAGES/messages.po')){ + //i will update it with strings from catalog exec('msgmerge -U translation/'.$Lang['language'].'/LC_MESSAGES/messages.po translation/'.$Lang['language'].'/LC_MESSAGES/catalog-u.pot'); }else{ + //else i will create new file exec('msginit -o translation/'.$Lang['language'].'/LC_MESSAGES/messages.po -i translation/'.$Lang['language'].'/LC_MESSAGES/catalog-u.pot --no-translator -l'.$Lang['codeset']); + //on first run is translation invalid, so idont want to use it anymore + unset($Languages[$LangId]); } - exec('msgfmt translation/'.$Lang['language'].'/LC_MESSAGES/messages.po'); - - } +//ok. al translated files are now in place,. +//time to generate apache maps + +//for each translatable file foreach ($list as $page){ - $newpage=$page.'.page'; - + //i will generate name of .page file + $newpage=$page.'.page'; if(substr($page,-6)=='.shtml'){ $newpage=str_replace('.shtml','.page',$page); }elseif(substr($page,-5)=='.html'){ $newpage=str_replace('.html','.page',$page); } + //i will open .page file + $f=fopen($newpage,'w+'); - $f=fopen($newpage,'w+'); + //i will need relative path + //there is small shotcoming - translation must be in same folder + //as original file $pos= strrpos($newpage,'/'); if ($pos===false){$pos=0;}else{$pos++;} fwrite($f, 'URI: '. substr($newpage,$pos)."\n\n"); + + //first, there will be english version fwrite($f, 'URI: '. substr($page,$pos)."\n"); fwrite($f, 'Content-type: text/html'."\n"); fwrite($f, 'Content-language: en'."\n\n"); + //and then all translated languages. foreach($Languages as $Language){ if(isset($Language['translated-pages'][$page])){ @@ -214,15 +303,20 @@ } - +/* +I will load html in DOM and then traverse it +@param filename Original filename +@param NewFilename Name for translated file. +*/ function HandleFile($Filename,$NewFilename){ - global $catalog; + global $catalog; //file opened earlier $dom_doc = new DOMDocument(); $dom_doc->registerNodeClass('DOMElement', 'JSLikeHTMLElement'); $html_file = file_get_contents($Filename); // The next line will likely generate lots of warnings if your html isn't perfect // Put an @ in front to suppress the warnings once you review them + // this is originall comment from code sample. $dom_doc->loadHTML( $html_file ); foreach(array('p','h1','h2','h3','title','li') as $tagname) { @@ -231,42 +325,31 @@ foreach($tags as $tag) { $tag_value = trim($tag->innerHTML); - + //write fragment in catalog + if($tag_value!=''){ + fwrite($catalog,"\n". + '#:'.$Filename."\n"); + $rows=explode("\n",$tag_value); + $cnt=count($rows); + if($cnt>1){ + fwrite($catalog, 'msgid ""'."\n"); + $i=1; + foreach($rows as $row){ + fwrite($catalog, ' "'.str_replace('"','\"',$row) .($i==$cnt?'':'\n').'"'."\n"); + $i++; + } + }else{ + fwrite($catalog, 'msgid "'.str_replace('"','\"',$tag_value) .'"'."\n"); + } + fwrite($catalog,'msgstr ""'."\n"); + } -if($tag_value!=''){ - - fwrite($catalog,"\n". - '#:'.$Filename."\n"); - $rows=explode("\n",$tag_value); - $cnt=count($rows); - if($cnt>1){ - fwrite($catalog, 'msgid ""'."\n"); - $i=1; - foreach($rows as $row){ - fwrite($catalog, ' "'.str_replace('"','\"',$row) .($i==$cnt?'':'\n').'"'."\n"); - $i++; - } - - - - }else{ - - fwrite($catalog, 'msgid "'.str_replace('"','\"',$tag_value) .'"'."\n"); - - } - - - - - fwrite($catalog,'msgstr ""'."\n"); -} - - + //load translated version and apply it $k=gettext($tag_value); $tag->innerHTML=$k; - - } - + + } + } @@ -275,19 +358,6 @@ file_put_contents($NewFilename,$translated_page); - //echo $translated_page; } - - - - - - - - - - - - |