From b37b4783fd5598c7c7948e3a9a75c5217f34cb1e Mon Sep 17 00:00:00 2001 From: Rotem Liss Date: Sun, 6 Aug 2006 14:06:57 +0000 Subject: [PATCH] =?utf8?q?*=20Completely=20rewriting=20and=20reformatting?= =?utf8?q?=20the=20code=20of=20the=20localisation=20statistics=20utility:?= =?utf8?q?=20removing=20several=20unuseful=20features=20(viewing=20the=20o?= =?utf8?q?bsolete=20messages=20which=20is=20already=20exist=20in=20checkLa?= =?utf8?q?nguage.php,=20and=20no=20output=20which=20is=20not=20useful=20wi?= =?utf8?q?thout=20that=20feature),=20using=20the=20languages=20object,=20r?= =?utf8?q?eplacing=20the=20Untranslated=20column=20by=20the=20Possibly=20u?= =?utf8?q?ntranslated=20(duplicate)=20column=20(written=20by=20Niklas=20La?= =?utf8?q?xstr=C3=B6m),=20ignoring=20the=20ignored=20messages,=20ignoring?= =?utf8?q?=20English=20and=20(temporarily)=20enRTL=20languages,=20echoing?= =?utf8?q?=20directly=20without=20using=20a=20buffer,=20merging=20some=20c?= =?utf8?q?hanges=20by=20Niklas=20Laxstr=C3=B6m.=20*=20Introducing=20the=20?= =?utf8?q?concept=20of=20translatable=20messages=20to=20the=20languages=20?= =?utf8?q?object,=20and=20using=20it=20instead=20of=20the=20English=20mess?= =?utf8?q?ages.=20*=20Adding=20ignored=20messages=20to=20the=20obsolete=20?= =?utf8?q?messages=20list.=20*=20Removing=20the=20message=20"metadata-fiel?= =?utf8?q?ds"=20from=20the=20ignored=20messages=20list,=20as=20it=20contai?= =?utf8?q?ns=20help=20in=20the=20local=20language.=20*=20Fixing=20a=20whit?= =?utf8?q?espace=20in=20zh-yue=20messages=20file.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- languages/MessagesZh_yue.php | 2 - maintenance/checkLanguage.php | 6 +- maintenance/languages.inc | 60 ++++++-- maintenance/transstat.php | 271 ++++++++++++++++------------------ 4 files changed, 176 insertions(+), 163 deletions(-) diff --git a/languages/MessagesZh_yue.php b/languages/MessagesZh_yue.php index b34dab4ddf..6bf4f6d5a3 100644 --- a/languages/MessagesZh_yue.php +++ b/languages/MessagesZh_yue.php @@ -2072,6 +2072,4 @@ $1 ); - ?> - diff --git a/maintenance/checkLanguage.php b/maintenance/checkLanguage.php index 5ccf0b6546..663550dd01 100644 --- a/maintenance/checkLanguage.php +++ b/maintenance/checkLanguage.php @@ -18,13 +18,13 @@ function checkLanguage( $code ) { global $wgLanguages, $wgHideMessages; # Get messages number - $englishMessagesNumber = count( $wgLanguages->getMessagesFor( 'en' ) ); + $translatableMessagesNumber = count( $wgLanguages->getTranslatableMessages() ); $localMessagesNumber = count( $wgLanguages->getMessagesFor( $code ) ); # Untranslated messages $untranslatedMessages = $wgLanguages->getUntranslatedMessages( $code ); $untranslatedMessagesNumber = count( $untranslatedMessages ); - $wgLanguages->outputMessagesList( $untranslatedMessages, "\n$untranslatedMessagesNumber messages of $englishMessagesNumber are not translated to $code, but exist in en:", $wgHideMessages ); + $wgLanguages->outputMessagesList( $untranslatedMessages, "\n$untranslatedMessagesNumber messages of $translatableMessagesNumber are not translated to $code, but exist in en:", $wgHideMessages ); # Duplicate messages $duplicateMessages = $wgLanguages->getDuplicateMessages( $code ); @@ -34,7 +34,7 @@ function checkLanguage( $code ) { # Obsolete messages $obsoleteMessages = $wgLanguages->getObsoleteMessages( $code ); $obsoleteMessagesNumber = count( $obsoleteMessages ); - $wgLanguages->outputMessagesList( $obsoleteMessages, "\n$obsoleteMessagesNumber messages of $localMessagesNumber are not exist in en, but still exist in $code:", $wgHideMessages ); + $wgLanguages->outputMessagesList( $obsoleteMessages, "\n$obsoleteMessagesNumber messages of $localMessagesNumber are not exist in en (or are in the ignored list), but still exist in $code:", $wgHideMessages ); # Messages without variables $messagesWithoutVariables = $wgLanguages->getMessagesWithoutVariables( $code ); diff --git a/maintenance/languages.inc b/maintenance/languages.inc index f6767da058..4f2031ca85 100644 --- a/maintenance/languages.inc +++ b/maintenance/languages.inc @@ -9,6 +9,7 @@ class languages { private $mList = array(); private $mMessages = array(); + private $mTranslatableMessages = array(); private $mIgnoredMessages = array( 'sidebar', 'addsection', @@ -24,7 +25,6 @@ class languages { 'loginend', 'loginlanguagelinks', 'markaspatrolledlink', - 'metadata-fields', // Objection, contains help 'newarticletextanon', 'noarticletextanon', 'number_of_watching_users_RCview', @@ -80,6 +80,14 @@ class languages { require( $filename ); if ( isset( $messages ) ) { $this->mMessages[$code] = $messages; + if ( $code == 'en' ) { + $this->mTranslatableMessages = $this->mMessages['en']; + foreach ( array_keys( $this->mTranslatableMessages ) as $key ) { + if ( in_array( $key, $this->mIgnoredMessages ) ) { + unset( $this->mTranslatableMessages[$key] ); + } + } + } } else { $this->mMessages[$code] = array(); } @@ -103,12 +111,34 @@ class languages { } /** - * Get all the messages of all the languages. + * Get all the messages which are translatable - not ignored messages. + * + * @param $code The langauge code. + * + * @return The messages in this language. + */ + public function getTranslatableMessages() { + $this->loadMessages( 'en' ); + return $this->mTranslatableMessages; + } + + /** + * Get the translated messages for a specific language. + * + * @param $code The langauge code. + * + * @return The translated messages for this language. */ - public function getAllMessages() { - foreach ( $this->mList as $code ) { - $this->getMessages( $code ); + public function getTranslatedMessages( $code ) { + $this->loadMessages( 'en' ); + $this->loadMessages( $code ); + $translatedMessages = array(); + foreach ( $this->mTranslatableMessages as $key => $value ) { + if ( isset( $this->mMessages[$code][$key] ) ) { + $translatedMessages[$key] = $value; + } } + return $translatedMessages; } /** @@ -122,8 +152,8 @@ class languages { $this->loadMessages( 'en' ); $this->loadMessages( $code ); $untranslatedMessages = array(); - foreach ( $this->mMessages['en'] as $key => $value ) { - if ( !isset( $this->mMessages[$code][$key] ) && !in_array( $key, $this->mIgnoredMessages ) ) { + foreach ( $this->mTranslatableMessages as $key => $value ) { + if ( !isset( $this->mMessages[$code][$key] ) ) { $untranslatedMessages[$key] = $value; } } @@ -142,7 +172,7 @@ class languages { $this->loadMessages( $code ); $duplicateMessages = array(); foreach ( $this->mMessages[$code] as $key => $value ) { - if ( @$this->mMessages['en'][$key] == $value ) { + if ( @$this->mTranslatableMessages[$key] == $value ) { $duplicateMessages[$key] = $value; } } @@ -161,7 +191,7 @@ class languages { $this->loadMessages( $code ); $obsoleteMessages = array(); foreach ( $this->mMessages[$code] as $key => $value ) { - if ( !isset( $this->mMessages['en'][$key] ) ) { + if ( !isset( $this->mTranslatableMessages[$key] ) ) { $obsoleteMessages[$key] = $value; } } @@ -181,10 +211,10 @@ class languages { $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' ); $messagesWithoutVariables = array(); foreach ( $this->mMessages[$code] as $key => $value ) { - if ( isset( $this->mMessages['en'][$key] ) ) { + if ( isset( $this->mTranslatableMessages[$key] ) ) { $missing = array(); foreach ( $variables as $var ) { - if ( preg_match( "/$var/sU", $this->mMessages['en'][$key] ) && + if ( preg_match( "/$var/sU", $this->mTranslatableMessages[$key] ) && !preg_match( "/$var/sU", $value ) ) { $missing[] = str_replace( '\$', '$', $var ); } @@ -209,7 +239,7 @@ class languages { $this->loadMessages( $code ); $emptyMessages = array(); foreach ( $this->mMessages[$code] as $key => $value ) { - if ( isset( $this->mMessages['en'][$key] ) && + if ( isset( $this->mTranslatableMessages[$key] ) && ( $this->mMessages[$code][$key] === '' || $this->mMessages[$code][$key] === '-' ) ) { $emptyMessages[$key] = $value; } @@ -229,7 +259,7 @@ class languages { $this->loadMessages( $code ); $messagesWithWhitespace = array(); foreach ( $this->mMessages[$code] as $key => $value ) { - if ( isset( $this->mMessages['en'][$key] ) && $this->mMessages['en'][$key] !== '' && + if ( isset( $this->mTranslatableMessages[$key] ) && $this->mTranslatableMessages[$key] !== '' && $value !== rtrim( $value ) ) { $messagesWithWhitespace[$key] = $value; } @@ -256,7 +286,7 @@ class languages { $wrongPhrases = '~(' . implode( '|', $wrongPhrases ) . ')~sDu'; $nonXHTMLMessages = array(); foreach ( $this->mMessages[$code] as $key => $value ) { - if ( isset( $this->mMessages['en'][$key] ) && preg_match( $wrongPhrases, $value ) ) { + if ( isset( $this->mTranslatableMessages[$key] ) && preg_match( $wrongPhrases, $value ) ) { $nonXHTMLMessages[$key] = $value; } } @@ -290,7 +320,7 @@ class languages { $wrongRegExp = '/(' . implode( '|', array_values( $wrongChars ) ) . ')/sDu'; $nonXHTMLMessages = array(); foreach ( $this->mMessages[$code] as $key => $value ) { - if ( isset( $this->mMessages['en'][$key] ) && preg_match( $wrongRegExp, $value ) ) { + if ( isset( $this->mTranslatableMessages[$key] ) && preg_match( $wrongRegExp, $value ) ) { foreach ( $wrongChars as $viewableChar => $hiddenChar ) { $value = str_replace( $hiddenChar, $viewableChar, $value ); } diff --git a/maintenance/transstat.php b/maintenance/transstat.php index ddcb8408c8..0af80b2f3b 100644 --- a/maintenance/transstat.php +++ b/maintenance/transstat.php @@ -1,202 +1,187 @@ * @author Ashar Voultoiz - * @bug 2499 * * Output is posted from time to time on: * http://meta.wikimedia.org/wiki/Localization_statistics */ -/** */ -require_once('commandLine.inc'); -require_once('languages.inc'); - -if( isset($options['help']) ) { usage(); wfDie(); } -// default output is WikiText -if( !isset($options['output']) ) { $options['output']='wiki'; } +require_once( 'commandLine.inc' ); +require_once( 'languages.inc' ); +if ( isset( $options['help'] ) ) { + showUsage(); +} +# Default output is WikiText +if ( !isset( $options['output'] ) ) { + $options['output'] = 'wiki'; +} /** Print a usage message*/ -function usage() { -print <<output='';} - function getContent() { return $this->output;} - - function formatPercent($subset, $total, $revert=false, $accuracy=2) { + function formatPercent( $subset, $total, $revert = false, $accuracy = 2 ) { return @sprintf( '%.' . $accuracy . 'f%%', 100 * $subset / $total ); } - // Override the next methods - function heading() {} - function footer() {} - function blockstart() {} - function blockend() {} - function element($in, $heading=false) {} -} - -/** Outputs nothing ! */ -class noneStatsOutput extends statsOutput { - function getContent() { return NULL;} + # Override the following methods + function heading() { + } + function footer() { + } + function blockstart() { + } + function blockend() { + } + function element( $in, $heading = false ) { + } } /** Outputs WikiText */ class wikiStatsOutput extends statsOutput { function heading() { - $this->output .= "{| border=2 cellpadding=4 cellspacing=0 style=\"background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;\" width=100%\n"; + echo "'''Note:''' These statistics can be generated by running php maintenance/transstat.php.\n\n"; + echo "{| border=2 cellpadding=4 cellspacing=0 style=\"background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;\" width=100%\n"; + } + function footer() { + echo "|}\n"; } - function footer() { $this->output .= "|}\n"; } - function blockstart() { $this->output .= "|-\n"; } - function blockend() { $this->output .= ''; } - function element($in, $heading = false) { - $this->output .= ($heading ? '!' : '|') . " $in\n"; + function blockstart() { + echo "|-\n"; } - function formatPercent($subset, $total, $revert=false, $accuracy=2) { + function blockend() { + echo ''; + } + function element( $in, $heading = false ) { + echo ($heading ? '!' : '|') . " $in\n"; + } + function formatPercent( $subset, $total, $revert = false, $accuracy = 2 ) { $v = @round(255 * $subset / $total); - if($revert) $v = 255 - $v; - if($v < 128) { - // red to yellow + if ( $revert ) { + $v = 255 - $v; + } + if ( $v < 128 ) { + # Red to Yellow $red = 'FF'; - $green = sprintf('%02X', 2*$v); + $green = sprintf( '%02X', 2 * $v ); } else { - // yellow to green - $red = sprintf('%02X', 2*(255 -$v) ); + # Yellow to Green + $red = sprintf('%02X', 2 * ( 255 - $v ) ); $green = 'FF'; } - $blue = '00'; - $color = $red.$green.$blue; + $blue = '00'; + $color = $red . $green . $blue; - $percent = statsOutput::formatPercent($subset, $total, $revert, $accuracy); - return 'bgcolor="#'.$color.'" | '.$percent; + $percent = statsOutput::formatPercent( $subset, $total, $revert, $accuracy ); + return 'bgcolor="#'. $color .'" | '. $percent; } } /** Output text. To be used on a terminal for example. */ class textStatsOutput extends statsOutput { - function element($in, $heading = false) { - $this->output .= $in."\t"; + function element( $in, $heading = false ) { + echo $in."\t"; + } + function blockend() { + echo "\n"; } - function blockend(){ $this->output .="\n";} } /** csv output. Some people love excel */ class csvStatsOutput extends statsOutput { - function element($in, $heading = false) { - $this->output .= $in.";"; + function element( $in, $heading = false ) { + echo $in . ";"; } - function blockend(){ $this->output .="\n";} -} - - -function redundant(&$arr, $langcode) { - $redundant = 0; - $englishMessages = Language::getMessagesFor( 'en' ); - foreach(array_keys($arr) as $key) { - if ( !isset( $englishMessages[$key] ) ) { - global $options; - if( isset( $options['showold'] ) ) { - print "Deprecated [$langcode]: $key\n"; - } - ++$redundant; - } + function blockend() { + echo "\n"; } - return $redundant; } -// Select an output engine -switch ($options['output']) { - case 'csv': - $out = new csvStatsOutput(); break; - case 'none': - $out = new noneStatsOutput(); break; - case 'text': - $out = new textStatsOutput(); break; +# Select an output engine +switch ( $options['output'] ) { case 'wiki': - $out = new wikiStatsOutput(); break; + $wgOut = new wikiStatsOutput(); + break; + case 'text': + $wgOut = new textStatsOutput(); + break; + case 'csv': + $wgOut = new csvStatsOutput(); + break; default: - usage(); wfDie(); - break; + showUsage(); } -$langTool = new languages(); - -// Load message and compute stuff -$msgs = array(); -foreach ( $langTool->getList() as $langcode ) { - // Since they aren't loaded by default.. - require( Language::getFileName( "$IP/languages/Messages", $langcode, ".php" ) ); - if( isset( $messages ) ) { - $msgs[$wgContLang->lcfirst($langcode)] = array( - 'total' => count( $messages ), - 'redundant' => redundant( $messages, $langcode ), - ); - } else { - $msgs[$wgContLang->lcfirst($langcode)] = array( - 'total' => 0, - 'redundant' => 0, - ); - } - unset( $messages ); -} +# Languages +$wgLanguages = new languages(); + +# Header +$wgOut->heading(); +$wgOut->blockstart(); +$wgOut->element( 'Language', true ); +$wgOut->element( 'Translated', true ); +$wgOut->element( '%', true ); +$wgOut->element( 'Possibly untranslated', true ); +$wgOut->element( '%', true ); +$wgOut->element( 'Obsolete', true ); +$wgOut->element( '%', true ); +$wgOut->blockend(); + +foreach ( $wgLanguages->getList() as $code ) { + # Don't check English + if ( $code == 'en' ) { + continue; + } + + # FIXME - temporary hack for this non-language won't appear + if ( $code == 'enRTL' ) { + continue; + } -// Top entry -$out->heading(); -$out->blockstart(); -$out->element('Language', true); -$out->element('Translated', true); -$out->element('%', true); -$out->element('Untranslated', true); -$out->element('%', true); -$out->element('Redundant', true); -$out->element('%', true); -$out->blockend(); - -// Generate rows -foreach($msgs as $lang => $stats) { - $out->blockstart(); - // Language - $out->element($wgContLang->getLanguageName(strtr($lang, '_', '-')) . " ($lang)"); - // Translated - $out->element($stats['total'] . '/' . $msgs['en']['total']); - // % Translated - $out->element($out->formatPercent($stats['total'], $msgs['en']['total'])); - // Untranslated - $out->element($msgs['en']['total'] - $stats['total']); - // % Untranslated - $out->element($out->formatPercent($msgs['en']['total'] - $stats['total'], $msgs['en']['total'], true)); - // Redundant & % Redundant - if($stats['redundant'] =='NC') { - $out->element('NC'); - $out->element('NC'); - } else { - $out->element($stats['redundant'] . '/' . $stats['total']); - $out->element($out->formatPercent($stats['redundant'], $stats['total'],true)); - } - $out->blockend(); + # Calculate the numbers + $name = $wgLang->getLanguageName( $code ); + $translatableMessagesNumber = count( $wgLanguages->getTranslatableMessages() ); + $localMessagesNumber = count( $wgLanguages->getMessagesFor( $code ) ); + $translatedMessagesNumber = count( $wgLanguages->getTranslatedMessages( $code ) ); + $translatedMessagesPercent = $wgOut->formatPercent( $translatedMessagesNumber, $translatableMessagesNumber ); + $duplicateMessagesNumber = count( $wgLanguages->getDuplicateMessages( $code ) ); + $duplicateMessagesPercent = $wgOut->formatPercent( $duplicateMessagesNumber, $translatedMessagesNumber, true ); + $obsoleteMessagesNumber = count( $wgLanguages->getObsoleteMessages( $code ) ); + $obsoleteMessagesPercent = $wgOut->formatPercent( $obsoleteMessagesNumber, $translatedMessagesNumber, true ); + + # Output them + $wgOut->blockstart(); + $wgOut->element( "$name ($code)" ); + $wgOut->element( "$translatedMessagesNumber/$translatableMessagesNumber" ); + $wgOut->element( $translatedMessagesPercent ); + $wgOut->element( "$duplicateMessagesNumber/$translatedMessagesNumber" ); + $wgOut->element( $duplicateMessagesPercent ); + $wgOut->element( "$obsoleteMessagesNumber/$translatedMessagesNumber" ); + $wgOut->element( $obsoleteMessagesPercent ); + $wgOut->blockend(); } -$out->footer(); -// Final output -echo $out->getContent(); +# Footer +$wgOut->footer(); + ?> -- 2.20.1