Source for file mambolanguage.class.php

Documentation is available at mambolanguage.class.php

  1. <?php
  2. /**
  3. * Language Class for Mambo
  4. @package Mambo
  5. @copyright Refer to copyright.php
  6. @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  7. @author Mambo Foundation Inc see README.php
  8. */
  9.  
  10. defined'_VALID_MOS' or die'Direct Access to this location is not allowed.' );
  11. class mamboLanguage {
  12.     var $name = '';
  13.     var $path = '';
  14.     var $version = '4.6';
  15.     var $title = '';
  16.     var $description = '';
  17.     var $creationdate = '';
  18.     var $author = '';
  19.     var $authorurl = '';
  20.     var $authoremail = '';
  21.     var $copyright = '';
  22.     var $license = '';
  23.     var $territory = '';
  24.     var $text_direction = '';
  25.     var $date_format = '';
  26.     var $iso639 = '';
  27.     var $iso3166_2 = '';
  28.     var $iso3166_3 = '';
  29.     var $locale = '';
  30.     var $charset = '';
  31.     var $codesets = array();
  32.     var $plural_form = array();
  33.     var $days = array('sun'=>'','mon'=>'','tue'=>'','wed'=>'','thu'=>'','fri'=>'','sat'=>'');
  34.     var $months = array('jan'=>'','feb'=>'','mar'=>'','apr'=>'','may'=>'','jun'=>'','jul'=>'','aug'=>'','sep'=>'','oct'=>'','nov'=>'','dec'=>'');
  35.     var $files = array();
  36.  
  37.     function mamboLanguage($lang$path null{
  38.         $this->name = $lang;
  39.         $this->path = $path;
  40.         if (is_null($this->path)) $this->path = mamboCore::get('rootPath').'/language/';
  41.         $this->load();
  42.     }
  43.  
  44.     function getFileName({
  45.         $file $this->iso639;
  46.         $file .= strlen($this->iso3166_2== '_' $this->iso3166_2 : '';
  47.         return $file;
  48.     }
  49.  
  50.     function get($var{
  51.         return isset($this->$var$this->$var null;
  52.     }
  53.     function set($var$value{
  54.         if (isset($this->$var)) $this->$var $value;
  55.     }
  56.     function save({
  57.     global $page_,$task,$mapcharset;
  58.     $this->updateFiles();
  59.     $xml $this->toXML();
  60.     if( (($page_=="addpage"&& ($task=="save")) || ($task=="convert") ){
  61.         if (strtolower($this->charset!= 'utf-8'{
  62.             $xml $this->iconvert("utf-8",$mapcharset[$this->charset],$xml);
  63.         }
  64.     }
  65.         /**/
  66.         $fp fopen($this->path . $this->getFileName('.xml''w+');
  67.         fwrite($fp$xml);
  68.         fclose($fp);
  69.     }
  70.  
  71.     function getLanguages({
  72.         $langfiles glob($this->path . "*.xml");
  73.         foreach($langfiles as $xml{
  74.             $xml str_replace($this->path''$xml);
  75.             if (substr($xml0-4!= 'locales'{
  76.                 $lobj &new mamboLanguage(substr($xml0-4)$this->path;
  77.                 $langs[$lobj->name$lobj;
  78.             }
  79.         }
  80.         return $langs;
  81.     }
  82.  
  83.     function setPlurals($exp{
  84.         preg_match('/nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*(.*)\s*;/'$exp$plurals);
  85.         $this->plural_form = array('nplurals' => $plurals[1]'plural' => $plurals[2]'expression' => $plurals[0]);
  86.     }
  87.  
  88.     function getDate($format null$timestamp null{
  89.         if (is_null($format)) $format $this->date_format;
  90.         if (is_null($timestamp)) {
  91.             $timestamp time();
  92.         }
  93.         $days array_values($this->days);
  94.         $months array_values($this->months);
  95.         $date preg_replace('/%[aA]/'$days[(int)strftime('%w'$timestamp)]$format);
  96.         $date preg_replace('/%[bB]/'$months[(int)strftime('%m'$timestamp)-1]$date);
  97.         return strftime($date$timestamp);
  98.     }
  99.  
  100.     function load($load_catalogs false{
  101.         global $mapcharset;
  102.         if (is_readable($this->path . $this->name . ".xml")) {
  103.             $source file_get_contents($this->path . $this->name . ".xml");
  104.             if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m'$source$m)) {
  105.                 $encoding strtoupper($m[1]);
  106.             else {
  107.                 $encoding "UTF-8";
  108.             }
  109.  
  110.             if($encoding == "UTF-8" || $encoding == "US-ASCII" || $encoding == "ISO-8859-1"{
  111.                 $parser xml_parser_create($encoding);
  112.             else {
  113.  
  114.                 #if(function_exists('mb_convert_encoding')) {
  115.                     if(trim($this->charset)!="")
  116.                         $encoded_source $this->iconvert($mapcharset[$this->charset],"UTF-8",$source);
  117.                     else
  118.                         $encoded_source $this->iconvert($mapcharset[strtolower($encoding)],"UTF-8",$source);
  119.                # }
  120.  
  121.                 if($encoded_source != NULL{
  122.                     $source str_replace $m[0],'<?xml version="1.0" encoding="utf-8"?>'$encoded_source);
  123.                 }
  124.  
  125.                 $parser xml_parser_create("UTF-8");
  126.             }
  127.  
  128.             xml_parser_set_option($parserXML_OPTION_TARGET_ENCODING"UTF-8");
  129.             xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
  130.             xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
  131.             if (!xml_parse_into_struct($parserimplode(""file($this->path . $this->name . ".xml"))$values)) {
  132.                 die(sprintf("XML error: %s at ".$this->path . $this->name.".xml in line %d",
  133.                 xml_error_string(xml_get_error_code($parser)),
  134.                 xml_get_current_line_number($parser)));
  135.             }
  136.             xml_parser_free($parser);
  137.  
  138.             
  139.             foreach($values as $key => $value{
  140.                 $tag strtolower($value['tag']);
  141.                 switch ($tag{
  142.                     case 'name':
  143.                     $this->title = $value['value'];
  144.                     $this->lang strtolower($value['value']);
  145.                     break;
  146.                     case 'author':
  147.                     case 'creationdate':
  148.                     case 'copyright':
  149.                     case 'license':
  150.                     case 'authoremail':
  151.                     case 'authorurl':
  152.                     case 'version':
  153.                     case 'description':
  154.                     $this->$tag $value['value'];
  155.                     break;
  156.                     case 'locale':
  157.                     if ($value['type'== 'open'{
  158.                         foreach ($value['attributes'as $k => $v)
  159.                         $this->$k $v;
  160.                     }
  161.                     break;
  162.                     case 'date_format':
  163.                     $this->date_format = $value['value'];
  164.                     break;
  165.                     case 'plural_form':
  166.                     if (!empty($value['attributes']['expression'])) {
  167.                         $this->plural_form = $value['attributes'];
  168.                     }
  169.                     break;
  170.                     case 'charset':
  171.                     $this->codesets[$value['value'];
  172.                     break;
  173.                     case 'days':
  174.                     $this->days = $value['attributes'];
  175.                     break;
  176.                     case 'months':
  177.                     $this->months = $value['attributes'];
  178.                     break;
  179.                     case 'filename':
  180.                     if ($load_catalogs{
  181.                         $file $value['attributes'];
  182.                         $file['filename'$value['value'];
  183.                         $this->files[$file;
  184.                     }
  185.                     break;
  186.                 }
  187.             }
  188.             $this->codesets = array_unique($this->codesets);
  189.             ifclass_exists('ConvertCharset'|| function_exists('iconv') )
  190.             {
  191.                 if(strtolower($encoding)!="utf-8")
  192.                 {
  193.                     $attrs get_object_vars($this);
  194.                     $this->arrayEncoding($attrs,$mapcharset[$this->charset]);
  195.                     $this->bindAttributes($attrs);
  196.                 }
  197.             }
  198.             return true;
  199.         }
  200.         return false;
  201.     }
  202.     function iconvert($fromcharset,$tocharset,$source,$useiconv=FALSE)
  203.     {
  204.         if(strtolower($fromcharset)==strtolower($tocharset)) 
  205.             return $source;
  206.         if($useiconv)
  207.         {
  208.             if(function_exists('iconv'))
  209.             {
  210.                 return iconv($fromcharset,$tocharset,$source);
  211.             }else
  212.             {
  213.                 return FALSE;
  214.             }
  215.         }
  216.         $NewEncoding new ConvertCharset();
  217.         return $NewEncoding->Convert($source,$fromcharset,$tocharset,false);
  218.     }
  219.     function bindAttributes($attrs)
  220.     {
  221.         if(!is_array($attrs))
  222.             return false;
  223.         foreach($attrs as $key=>$v)
  224.         {
  225.             $this->$key $v;
  226.         }
  227.     }
  228.     function arrayEncoding(&$attrs,$encoding,$useiconv=FALSE)
  229.     {
  230.         if(is_array($attrs))
  231.         {
  232.             foreach($attrs as $key=>$val)
  233.             {
  234.                 if(is_array($val))
  235.                     $this->arrayEncoding($attrs[$key],$encoding,$useiconv);
  236.                 else
  237.                 {
  238.                     $attrs[$key$this->iconvert("utf-8",$encoding,$val,$useiconv);
  239.                 }
  240.             }
  241.         }
  242.     }
  243.  
  244.     function updateFiles({
  245.         $dir $this->path . $this->name . '/';
  246.         $langfiles mosReadDirectory($dir'.po$');
  247.         set_time_limit(60);
  248.  
  249.         foreach ($langfiles as $lf{
  250.             $domain substr($lf0-3);
  251.             $catalog new PHPGettext_Catalog($domain$this->path);
  252.             $catalog->setproperty('lang'$this->name);
  253.             $catalog->setproperty('mode'_MODE_PO_);
  254.             $catalog->load();
  255.             $file['filename'"language/" $this->name . '/' $lf;
  256.             $file['domain'$domain;
  257.             $file['strings'count($catalog->strings);
  258.             $file['percent''';
  259.             $file['translated'0;
  260.             $file['fuzzy'0;
  261.             $file['filetype''po';
  262.             $pluralfuzz false;
  263.             foreach ($catalog->strings as $msg{
  264.                 if (is_array($msg->msgstr)) {
  265.                     foreach ($msg->msgstr as $i{
  266.                         $unt empty($i);
  267.                     }
  268.                     if (!$unt{
  269.                         $file['translated']++;
  270.                     }
  271.                 }
  272.                 if (!is_array($msg->msgstr&& !empty($msg->msgstr&& !$msg->is_fuzzy{
  273.                     $file['translated']++;
  274.                 }
  275.                 if ($msg->is_fuzzy{
  276.                     $file['fuzzy']++;
  277.                 }
  278.             }
  279.  
  280.  
  281.             $nonfuzzy $file['strings'$file['fuzzy'];
  282.             if (!$nonfuzzy$nonfuzzy 1;
  283.             $file['percent'round($file['translated'100 $nonfuzzy2);
  284.             unset($nonfuzzy);
  285.             $this->files[$file;
  286.         }
  287.         $this->files[array('filename'=>"language/" $this->name . '.xml','domain'=>"",'strings'=>"",'percent'=>"",'translated'=>0,'fuzzy'=>0,'filetype'=>'xml');
  288.         $langfiles mosReadDirectory($dir.'LC_MESSAGES/''.mo$');
  289.         set_time_limit(60);
  290.  
  291.         foreach ($langfiles as $lf{
  292.             $this->files[array('filename'=>"language/" $this->name . '/LC_MESSAGES/' $lf,'domain'=>"",'strings'=>"",'percent'=>"",'translated'=>0,'fuzzy'=>0,'filetype'=>'mo');
  293.         }
  294.         if(file_exists($this->path.'/glossary/'.  $this->name.".".$this->charset.".po"))
  295.         $this->files[array('filename'=>"language/glossary/" $this->name.".".$this->charset.".po",'domain'=>"",'strings'=>"",'percent'=>"",'translated'=>0,'fuzzy'=>0,'filetype'=>'gl');
  296.     }
  297.  
  298.     function toXML({
  299.         $array[array('tag' => 'mosinstall''type' => 'open''level' => 1'attributes' => array('version' => '4.6''type' => 'language'));
  300.         $array[array('tag' => 'name''type' => 'complete''level' => 2'value' => $this->title);
  301.         $array[array('tag' => 'version''type' => 'complete''level' => 2'value' => $this->version);
  302.         $array[array('tag' => 'description''type' => 'complete''level' => 2'value' => $this->description);
  303.         $array[array('tag' => 'creationdate''type' => 'complete''level' => 2'value' => $this->creationdate);
  304.         $array[array('tag' => 'author''type' => 'complete''level' => 2'value' => $this->author);
  305.         $array[array('tag' => 'authorurl''type' => 'complete''level' => 2'value' => $this->authorurl);
  306.         $array[array('tag' => 'authoremail''type' => 'complete''level' => 2'value' => $this->authoremail);
  307.         $array[array('tag' => 'copyright''type' => 'complete''level' => 2'value' => $this->copyright);
  308.         $array[array('tag' => 'license''type' => 'complete''level' => 2'value' => $this->license);
  309.         $array[array('tag' => 'params''type' => 'open''level' => 2);
  310.         $array[array('tag' => 'param''type' => 'complete''level' => 3'attributes' => array('name' => 'locale''type' => 'text''default' => $this->locale'label' => 'Locale String''description' => 'Locale string for setlocale() (eg. en, english)'));
  311.         $array[array('tag' => 'param''type' => 'complete''level' => 3'attributes' => array('name' => 'charset''type' => 'text''default' => $this->charset'label' => 'Character Set''description' => 'Character set for this language.'));
  312.         $array[array('tag' => 'param''type' => 'complete''level' => 3'attributes' => array('name' => 'text_direction''type' => 'text''default' => $this->text_direction'label' => 'Text Direction''description' => 'left-to-right or light-to-left'));
  313.         $array[array('tag' => 'param''type' => 'complete''level' => 3'attributes' => array('name' => 'date_format''type' => 'text''default' => $this->date_format'label' => 'Date Format''description' => 'Date format for strftime() (eg. %A, %d %B %Y)'));
  314.         $array[array('tag' => 'param''type' => 'complete''level' => 3'attributes' => array('name' => 'plural_form''type' => 'text''default' => htmlentities($this->plural_form['expression'])'label' => 'Plural Forms''description' => 'Plural Forms expression'));
  315.         $array[array('tag' => 'params''type' => 'close''level' => 2);
  316.         $array[array('tag' => 'locale''type' => 'open''level' => 2'attributes' => array('name' => $this->name'title' => $this->title'territory' => $this->territory'locale' => $this->locale'text_direction' => $this->text_direction'iso639' => $this->iso639'iso3166_2' => $this->iso3166_2'iso3166_3' => $this->iso3166_3'charset' => $this->charset));
  317.         $array[array('tag' => 'plural_form''type' => 'complete''level' => 3'attributes' => array('nplurals' => $this->plural_form['nplurals''plural' => htmlentities($this->plural_form['plural'])'expression' => htmlentities($this->plural_form['expression'])));
  318.         $array[array('tag' => 'date_format''type' => 'complete''level' => 3'value' => $this->date_format);
  319.         $array[array('tag' => 'codesets''type' => 'open''level' => 3);
  320.         foreach ($this->codesets as $charset$array[array('tag' => 'charset''type' => 'complete''level' => 4'value' => $charset);
  321.         $array[array('tag' => 'codesets''type' => 'close''level' => 3);
  322.         foreach ($this->days as $name => $day$days[$name$day;
  323.         $array[array('tag' => 'days''type' => 'complete''level' => 3'attributes' => $days);
  324.         foreach ($this->months as $name => $month$months[$name$month;
  325.         $array[array('tag' => 'months''type' => 'complete''level' => 3'attributes' => $months);
  326.         $array[array('tag' => 'locale''type' => 'close''level' => 2);
  327.         $array[array('tag' => 'files''type' => 'open''level' => 2);
  328.         foreach ($this->files as $file{
  329.             $array[array('tag' => 'filename''type' => 'complete''level' => 3'value' => $file['filename']'attributes' => array('domain' => $file['domain''strings' => $file['strings''translated' => $file['translated''fuzzy' => $file['fuzzy''percent' => $file['percent']'filetype' => $file['filetype']));
  330.         }
  331.         $array[array('tag' => 'files''type' => 'close''level' => 2);
  332.         $array[array('tag' => 'mosinstall''type' => 'close''level' => 1);
  333.  
  334.         $xml "<?xml version=\"1.0\" encoding=\"$this->charset\"?>\n";
  335.         if ((!empty($array)) AND (is_array($array))) {
  336.             foreach ($array as $key => $value{
  337.                 switch ($value["type"]{
  338.                     case "open":
  339.                     $xml .= str_repeat("\t"$value["level"1);
  340.                     $xml .= "<" strtolower($value["tag"]);
  341.                     if (isset($value["attributes"])) {
  342.                         foreach ($value["attributes"as $k => $v{
  343.                             $xml .= sprintf(' %s="%s"'strtolower($k)$v);
  344.                         }
  345.                     }
  346.                     $xml .= ">\n";
  347.                     break;
  348.                     case "complete":
  349.                     $xml .= str_repeat("\t"$value["level"1);
  350.                     $xml .= "<" strtolower($value["tag"]);
  351.                     if (isset($value["attributes"])) {
  352.                         foreach ($value["attributes"as $k => $v{
  353.                             $xml .= sprintf(' %s="%s"'strtolower($k)$v);
  354.                         }
  355.                     }
  356.                     $xml .= ">";
  357.                     $xml .= isset($value['value']$value['value'false;
  358.                     $xml .= "</" strtolower($value["tag"]">\n";
  359.                     break;
  360.                     case "close":
  361.                     $xml .= str_repeat("\t"$value["level"1);
  362.                     $xml .= "</" strtolower($value["tag"]">\n";
  363.                     break;
  364.                     default:
  365.                     break;
  366.                 }
  367.             }
  368.         }
  369.         return $xml;
  370.     }
  371.  
  372.     function getLocales({
  373.         $xmlfile "../language/locales.xml";
  374.         $p xml_parser_create();
  375.         xml_parser_set_option($pXML_OPTION_CASE_FOLDING0);
  376.         xml_parser_set_option($pXML_OPTION_SKIP_WHITE1);
  377.         xml_parse_into_struct($pimplode(""file($xmlfile))$values);
  378.         xml_parser_free($p);
  379.         $locales array();
  380.         foreach($values as $key => $value{
  381.             switch ($value['tag']{
  382.                 case 'locale':
  383.                 if ($value['type'== 'open'{
  384.                     $iso639 $value['attributes']['iso639'];
  385.                     $language[$iso639$value['attributes']['title'];
  386.                     $locale[$iso639$value['attributes'];
  387.                     $directions[$iso639$value['attributes']['text_direction'];
  388.                 }
  389.                 break;
  390.                 case 'territory':
  391.                 $t['iso3166_2'$value['attributes']['iso3166_2'];
  392.                 $t['iso3166_3'$value['attributes']['iso3166_3'];
  393.                 $t['territory'$value['value'];
  394.                 $territories[$iso639][$t;
  395.                 break;
  396.                 case 'charset':
  397.                 $locale[$iso639]['codesets'][$codesets[$iso639][$value['value'];
  398.                 break;
  399.                 case 'date_format':
  400.                 $locale[$iso639]['dateformats'$dateformats[$iso639$value['value'];
  401.                 break;
  402.                 case 'days':
  403.                 $locale[$iso639]['days'$value['attributes'];
  404.                 break;
  405.                 case 'months':
  406.                 $locale[$iso639]['months'$value['attributes'];
  407.                 break;
  408.                 case 'plural_form':
  409.                 $exp '';
  410.                 if (!empty($value['attributes']['expression'])) {
  411.                     $locale[$iso639]['plural_form'$value['attributes'];
  412.                     $plural_forms[$iso639$value['attributes']['expression'];
  413.                 }
  414.                 break;
  415.             }
  416.         }
  417.         $locales['locales'$locale;
  418.         $locales['languages'$language;
  419.         $locales['territories'$territories;
  420.         $locales['codesets'$codesets;
  421.         $locales['dateformats'$dateformats;
  422.         $locales['directions'$directions;
  423.         $locales['plural_forms'$plural_forms;
  424.         return $locales;
  425.     }
  426.  
  427.     function getSystemLocale(){
  428.         if (substr(strtoupper(PHP_OS)03== 'WIN'){
  429.             return strtolower($this->title).($this->iso3166_3?'_'.strtolower($this->iso3166_3):'');
  430.         else {
  431.             return $this->locale;
  432.         }
  433.     }
  434. }
  435.  
  436. function getlocales({
  437.     return mamboLanguage::getLocales();
  438. }
  439. ?>

Documentation generated on Mon, 05 May 2008 16:21:06 +0400 by phpDocumentor 1.4.0