cvs: pear /I18N DateTime.php

From: Date: Mon, 02 Dec 2002 15:46:29 +0000
Subject: cvs: pear /I18N DateTime.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-7721@lists.php.net to get a copy of this message
cain Mon Dec 2 10:46:29 2002 EDT Modified files: /pear/I18N DateTime.php Log: - fix a bug, which was translating things in a strange way in special cases Index: pear/I18N/DateTime.php diff -u pear/I18N/DateTime.php:1.4 pear/I18N/DateTime.php:1.5 --- pear/I18N/DateTime.php:1.4 Sat Nov 30 09:41:30 2002 +++ pear/I18N/DateTime.php Mon Dec 2 10:46:29 2002 @@ -15,7 +15,7 @@ // | Authors: Wolfram Kriesing <wk@visionp.de> | // | | // +----------------------------------------------------------------------+// -// $Id: DateTime.php,v 1.4 2002/11/30 14:41:30 cain Exp $ +// $Id: DateTime.php,v 1.5 2002/12/02 15:46:29 cain Exp $ require_once 'I18N/Format.php'; @@ -41,7 +41,7 @@ const CUSTOM_FORMATS_OFFSET = 100; */ - + var $days = array( 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' ); var $daysAbbreviated = array( 'Sun','Mon','Tue','Wed','Thu','Fri','Sat'); @@ -226,12 +226,39 @@ // dont put the abbreviated's in this array .... $translateArrays = array( 'days' , 'months' , 'daysAbbreviated' , 'monthsAbbreviated' ); -// FIXXME optimize the localized arrays, so we only need to put the words in there which really need translation, such -// as November is the same in german and english, but still it is translated here - foreach( $translateArrays as $aArray ){ - if( isset($this->_localeObj->{$aArray}) ) - $string = str_replace( $this->{$aArray} , $this->_localeObj->{$aArray} , $string ); + // this seems a bit difficult i guess, + // but i had problems with the way i did it before, which way simply str_replace the + // src-lang array and the dest-lang array, this caused stuff like + // translating 'Monday' => 'Montag' and then 'Montag' => 'Motag', since 'Mon' + // is the abbreviated weekday for Monday. + // if i would turn it around and translate the abbreviated words first it would screw up worse + + // so waht i do now is searching for the position of words which can be translated and + // remember the position (using strpos) and dont translate a word at this position a second + // time. this at least prevents the case described above. i hope it covers everything else too + // for me it works quite well now + $translateSrc = array(); + $translateDest = array(); + $prevPositions = array(); + foreach( $translateArrays as $aArray ) + { + if( isset($this->_localeObj->{$aArray})) + { + foreach( $this->{$aArray} as $index=>$aWord ) + { + if( ($pos=strpos( $string , $aWord ))!==false && + !in_array( $pos , $prevPositions ) ) + { + $translateSrc[] = $aWord; + $translateDest[] = $this->_localeObj->{$aArray}[$index]; + $prevPositions[] = $pos; + } + } + } } + + // here we actually replace the strings (translate:-)) that we found, when checking for their position + $string = str_replace( $translateSrc , $translateDest , $string ); return $string; }

« previous php.pear.cvs (#7721) next »