From 9d8ef635c4cb0497174f7d0642a614286cedae78 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Thu, 11 Jan 2007 20:15:03 +0000 Subject: [PATCH] * When both ' and " occur in a message, escape the less common of the two * Use a couple of simpler constructs in place of fors/foreaches * Why don't we use this script routinely for all message files? --- maintenance/language/writeMessagesArray.inc | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/maintenance/language/writeMessagesArray.inc b/maintenance/language/writeMessagesArray.inc index b0d17c59ca..cb0cb89c8f 100644 --- a/maintenance/language/writeMessagesArray.inc +++ b/maintenance/language/writeMessagesArray.inc @@ -79,12 +79,7 @@ function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) { } # Get max key length - $maxKeyLength = 0; - foreach( array_keys( $messages ) as $key ) { - if ( strlen( $key ) > $maxKeyLength ) { - $maxKeyLength = strlen( $key ); - } - } + $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) ); # Format the messages foreach( $messages as $key => $value ) { @@ -92,9 +87,7 @@ function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) { $blockText .= "'$key'"; # Add the appropriate block whitespace - for ( $i = 1; $i <= ( $maxKeyLength - strlen( $key ) ); $i++ ) { - $blockText .= ' '; - } + $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) ); # Refer to the value $blockText .= ' => '; @@ -105,7 +98,9 @@ function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) { } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) { $blockText .= "\"$value\""; } else { - $blockText .= "'" . str_replace( "'", "\'", $value ) . "'"; + # Pick the less numerous one to escape + $quote = substr_count( $value, '"' ) >= substr_count( $value, "'" ) ? "'" : '"'; + $blockText .= $quote . str_replace( $quote, '\\'.$quote, $value ) . $quote; } # Comma -- 2.20.1