--- mambo/branches/4.6/includes/mambolanguage.class.php 2006/03/20 23:41:15 407 +++ mambo/branches/4.6/includes/mambolanguage.class.php 2007/02/02 05:02:04 948 @@ -1,5 +1,13 @@ '','mon'=>'','tue'=>'','wed'=>'','thu'=>'','fri'=>'','sat'=>''); + var $months = array('jan'=>'','feb'=>'','mar'=>'','apr'=>'','may'=>'','jun'=>'','jul'=>'','aug'=>'','sep'=>'','oct'=>'','nov'=>'','dec'=>''); var $files = array(); function mamboLanguage($lang, $path = null) { $this->name = $lang; $this->path = $path; - if (is_null($this->path)) $this->path = realpath('../language/') . DIRECTORY_SEPARATOR; + if (is_null($this->path)) $this->path = mamboCore::get('rootPath').'/language/'; $this->load(); } - function &getInstance($lang, $path = null) { - static $languages; - if (!isset($languages[$lang]) || is_null($languages[$lang])) { - $lobj = new mamboLanguage($lang, $path); - $languages[$lang] = $lobj; - } - return $languages[$lang]; - } + function getFileName() { $file = $this->iso639; $file .= strlen($this->iso3166_2) == 2 ? '_' . $this->iso3166_2 : ''; @@ -53,9 +55,20 @@ if (isset($this->$var)) $this->$var = $value; } function save() { - $this->updateFiles(); + global $page_,$task; + $this->updateFiles(); + $xml = $this->toXML(); + if( (($page_=="addpage") && ($task=="save")) || ($task=="convert") ){ + if (strtolower($this->charset) != 'utf-8') { + if(trim($this->wincodepage)!="") + $xml = $this->iconvert("utf-8",$this->wincodepage,$xml); + else + $xml = $this->iconvert("utf-8",$this->charset,$xml,TRUE); + } + } + /**/ $fp = fopen($this->path . $this->getFileName() . '.xml', 'w+'); - fwrite($fp, $this->toXML()); + fwrite($fp, $xml); fclose($fp); } @@ -64,7 +77,7 @@ foreach($langfiles as $xml) { $xml = str_replace($this->path, '', $xml); if (substr($xml, 0, -4) != 'locales') { - $lobj = &mamboLanguage::getInstance(substr($xml, 0, -4), $this->path) ; + $lobj = &new mamboLanguage(substr($xml, 0, -4), $this->path) ; $langs[$lobj->name] = $lobj; } } @@ -90,16 +103,48 @@ function load($load_catalogs = false) { if (is_readable($this->path . $this->name . ".xml")) { - $p = xml_parser_create(); - xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); - xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1); - xml_parse_into_struct($p, implode("", file($this->path . $this->name . ".xml")), $values); - xml_parser_free($p); + $source = file_get_contents($this->path . $this->name . ".xml"); + if (preg_match('//m', $source, $m)) { + $encoding = strtoupper($m[1]); + } else { + $encoding = "UTF-8"; + } + + if($encoding == "UTF-8" || $encoding == "US-ASCII" || $encoding == "ISO-8859-1") { + $parser = xml_parser_create($encoding); + } else { + + #if(function_exists('mb_convert_encoding')) { + if(trim($this->wincodepage)!="") + $encoded_source = $this->iconvert($this->wincodepage,"UTF-8",$source); + else + $encoded_source = $this->iconvert($encoding,"UTF-8",$source,TRUE); + # } + + if($encoded_source != NULL) { + $source = str_replace ( $m[0],'', $encoded_source); + } + + $parser = xml_parser_create("UTF-8"); + } + + xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); + xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); + if (!xml_parse_into_struct($parser, implode("", file($this->path . $this->name . ".xml")), $values)) { + die(sprintf("XML error: %s at ".$this->path . $this->name.".xml in line %d", + xml_error_string(xml_get_error_code($parser)), + xml_get_current_line_number($parser))); + } + xml_parser_free($parser); + + foreach($values as $key => $value) { $tag = strtolower($value['tag']); switch ($tag) { case 'name': $this->title = $value['value']; + $this->lang = strtolower($value['value']); break; case 'author': case 'creationdate': @@ -128,6 +173,9 @@ case 'charset': $this->codesets[] = $value['value']; break; + case 'wincodepage': + $this->wincodepage = $value['value']; + break; case 'days': $this->days = $value['attributes']; break; @@ -143,21 +191,74 @@ break; } } - + $this->codesets = array_unique($this->codesets); + if( class_exists('ConvertCharset') || function_exists('iconv') ) + { + if(strtolower($encoding)!="utf-8") + { + $attrs = get_object_vars($this); + if(trim($this->wincodepage)!="") + { + $this->arrayEncoding($attrs,$this->wincodepage); + $this->bindAttributes($attrs); + + }else{ + $this->arrayEncoding($attrs,$encoding,TRUE); + $this->bindAttributes($attrs); + } + } + } return true; } return false; } + function iconvert($fromcharset,$tocharset,$source,$useiconv=FALSE) + { + if(strtolower($fromcharset)==strtolower($tocharset)) + return $source; + if($useiconv) + { + if(function_exists('iconv')) + { + return iconv($fromcharset,$tocharset,$source); + }else + { + return FALSE; + } + } + $NewEncoding = new ConvertCharset(); + return $NewEncoding->Convert($source,$fromcharset,$tocharset,false); + } + function bindAttributes($attrs) + { + if(!is_array($attrs)) + return false; + foreach($attrs as $key=>$v) + { + $this->$key = $v; + } + } + function arrayEncoding(&$attrs,$encoding,$useiconv=FALSE) + { + if(is_array($attrs)) + { + foreach($attrs as $key=>$val) + { + if(is_array($val)) + $this->arrayEncoding($attrs[$key],$encoding,$useiconv); + else + { + $attrs[$key] = $this->iconvert("utf-8",$encoding,$val,$useiconv); + } + } + } + } function updateFiles() { $dir = $this->path . $this->name . '/'; $langfiles = mosReadDirectory($dir, '.po$'); - /*set_time_limit(360);*/ - - $admin = new PHPGettextAdmin(); - $cmd = "msgcat --use-first -o $dir"."compendium.po ".$dir.implode(" $dir", $langfiles); - $admin->execute($cmd); - + set_time_limit(60); + foreach ($langfiles as $lf) { $domain = substr($lf, 0, -3); $catalog = new PHPGettext_Catalog($domain, $this->path); @@ -170,23 +271,35 @@ $file['percent'] = ''; $file['translated'] = 0; $file['fuzzy'] = 0; + $file['filetype'] = 'po'; $pluralfuzz = false; foreach ($catalog->strings as $msg) { + if (is_array($msg->msgstr)) { + foreach ($msg->msgstr as $i) { + $unt = empty($i); + } + if (!$unt) { + $file['translated']++; + } + } if (!is_array($msg->msgstr) && !empty($msg->msgstr) && !$msg->is_fuzzy) { $file['translated']++; - } + } if ($msg->is_fuzzy) { $file['fuzzy']++; } } - - + + $nonfuzzy = $file['strings'] - $file['fuzzy']; if (!$nonfuzzy) $nonfuzzy = 1; $file['percent'] = round($file['translated'] * 100 / $nonfuzzy, 2); unset($nonfuzzy); $this->files[] = $file; } + $this->files[] = array('filename'=>"language/" . $this->name . '.xml','domain'=>"",'strings'=>"",'percent'=>"",'translated'=>0,'fuzzy'=>0,'filetype'=>'xml'); + $this->files[] = array('filename'=>"language/" . $this->name . '/LC_MESSAGES/' . $this->name.".mo",'domain'=>"",'strings'=>"",'percent'=>"",'translated'=>0,'fuzzy'=>0,'filetype'=>'mo'); + $this->files[] = array('filename'=>"language/glossary/" . $this->name.".".$this->charset.".po",'domain'=>"",'strings'=>"",'percent'=>"",'translated'=>0,'fuzzy'=>0,'filetype'=>'gl'); } function toXML() { @@ -213,6 +326,7 @@ $array[] = array('tag' => 'codesets', 'type' => 'open', 'level' => 3); foreach ($this->codesets as $charset) $array[] = array('tag' => 'charset', 'type' => 'complete', 'level' => 4, 'value' => $charset); $array[] = array('tag' => 'codesets', 'type' => 'close', 'level' => 3); + $array[] = array('tag' => 'wincodepage', 'type' => 'complete', 'level' => 3,'value'=>$this->wincodepage); foreach ($this->days as $name => $day) $days[$name] = $day; $array[] = array('tag' => 'days', 'type' => 'complete', 'level' => 3, 'attributes' => $days); foreach ($this->months as $name => $month) $months[$name] = $month; @@ -220,7 +334,7 @@ $array[] = array('tag' => 'locale', 'type' => 'close', 'level' => 2); $array[] = array('tag' => 'files', 'type' => 'open', 'level' => 2); foreach ($this->files as $file) { - $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'])); + $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'])); } $array[] = array('tag' => 'files', 'type' => 'close', 'level' => 2); $array[] = array('tag' => 'mosinstall', 'type' => 'close', 'level' => 1); @@ -262,61 +376,75 @@ } return $xml; } + + function getLocales() { + $xmlfile = "../language/locales.xml"; + $p = xml_parser_create(); + xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); + xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1); + xml_parse_into_struct($p, implode("", file($xmlfile)), $values); + xml_parser_free($p); + $locales = array(); + foreach($values as $key => $value) { + switch ($value['tag']) { + case 'locale': + if ($value['type'] == 'open') { + $iso639 = $value['attributes']['iso639']; + $language[$iso639] = $value['attributes']['title']; + $locale[$iso639] = $value['attributes']; + $directions[$iso639] = $value['attributes']['text_direction']; + } + break; + case 'territory': + $t['iso3166_2'] = $value['attributes']['iso3166_2']; + $t['iso3166_3'] = $value['attributes']['iso3166_3']; + $t['territory'] = $value['value']; + $territories[$iso639][] = $t; + break; + case 'charset': + $locale[$iso639]['codesets'][] = $codesets[$iso639][] = $value['value']; + break; + case 'date_format': + $locale[$iso639]['dateformats'] = $dateformats[$iso639] = $value['value']; + break; + case 'wincodepage': + $locale[$iso639]['wincodepage'] = $value['value']; + break; + case 'days': + $locale[$iso639]['days'] = $value['attributes']; + break; + case 'months': + $locale[$iso639]['months'] = $value['attributes']; + break; + case 'plural_form': + $exp = ''; + if (!empty($value['attributes']['expression'])) { + $locale[$iso639]['plural_form'] = $value['attributes']; + $plural_forms[$iso639] = $value['attributes']['expression']; + } + break; + } + } + $locales['locales'] = $locale; + $locales['languages'] = $language; + $locales['territories'] = $territories; + $locales['codesets'] = $codesets; + $locales['dateformats'] = $dateformats; + $locales['directions'] = $directions; + $locales['plural_forms'] = $plural_forms; + return $locales; + } + + function getSystemLocale(){ + if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN'){ + return strtolower($this->title).'_'.strtolower($this->iso3166_3); + } else { + return $this->locale; + } + } } function getlocales() { - $xmlfile = "../language/locales.xml"; - $p = xml_parser_create(); - xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); - xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1); - xml_parse_into_struct($p, implode("", file($xmlfile)), $values); - xml_parser_free($p); - $locales = array(); - foreach($values as $key => $value) { - switch ($value['tag']) { - case 'locale': - if ($value['type'] == 'open') { - $iso639 = $value['attributes']['iso639']; - $language[$iso639] = $value['attributes']['title']; - $locale[$iso639] = $value['attributes']; - $directions[$iso639] = $value['attributes']['text_direction']; - } - break; - case 'territory': - $t['iso3166_2'] = $value['attributes']['iso3166_2']; - $t['iso3166_3'] = $value['attributes']['iso3166_3']; - $t['territory'] = $value['value']; - $territories[$iso639][] = $t; - break; - case 'charset': - $locale[$iso639]['codesets'][] = $codesets[$iso639][] = $value['value']; - break; - case 'date_format': - $locale[$iso639]['dateformats'] = $dateformats[$iso639] = $value['value']; - break; - case 'days': - $locale[$iso639]['days'] = $value['attributes']; - break; - case 'months': - $locale[$iso639]['months'] = $value['attributes']; - break; - case 'plural_form': - $exp = ''; - if (!empty($value['attributes']['expression'])) { - $locale[$iso639]['plural_form'] = $value['attributes']; - $plural_forms[$iso639] = $value['attributes']['expression']; - } - break; - } - } - $locales['locales'] = $locale; - $locales['languages'] = $language; - $locales['territories'] = $territories; - $locales['codesets'] = $codesets; - $locales['dateformats'] = $dateformats; - $locales['directions'] = $directions; - $locales['plural_forms'] = $plural_forms; - return $locales; + return mamboLanguage::getLocales(); } - ?> \ No newline at end of file