3 * Write a messages array as a PHP text.
6 * @ingroup MaintenanceLanguage
10 * @ingroup MaintenanceLanguage
13 static $optionalComment = 'only translate this message to other languages if you have to change it';
14 static $ignoredComment = "do not translate or duplicate this message to other languages";
16 static $messageStructure;
17 static $blockComments;
18 static $messageComments;
19 static $ignoredMessages;
20 static $optionalMessages;
23 * Write a messages array as a PHP text and write it to the messages file.
25 * @param $messages The messages array.
26 * @param $code The language code.
27 * @param $write Write to the messages file?
28 * @param $listUnknown List the unknown messages?
30 public static function writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown ) {
31 # Rewrite the messages array
32 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
33 $messagesText = $messages[0];
34 $sortedMessages = $messages[1];
37 $filename = Language::getMessagesFileName( $code );
38 $contents = file_get_contents( $filename );
39 if( strpos( $contents, '$messages' ) !== false ) {
40 $contents = explode( '$messages', $contents );
41 if( $messagesText == '$messages' . $contents[1] ) {
42 echo "Generated messages for language $code. Same as the current file.\n";
46 $new .= $messagesText;
47 file_put_contents( $filename, $new );
48 echo "Generated and wrote messages for language $code.\n";
50 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
53 if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
55 echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n";
57 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
58 foreach( $sortedMessages['unknown'] as $key => $value ) {
59 echo "* " . $key . "\n";
63 echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
68 * Write a messages array as a PHP text.
70 * @param $messages The messages array.
71 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
73 * @return Array of the PHP text and the sorted messages array.
75 public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) {
77 $dir = $prefix ? $prefix : dirname( __FILE__ );
79 require( $dir . '/messages.inc' );
80 self::$messageStructure = $wgMessageStructure;
81 self::$blockComments = $wgBlockComments;
82 self::$messageComments = $wgMessageComments;
84 require( $dir . '/messageTypes.inc' );
85 self::$ignoredMessages = $wgIgnoredMessages;
86 self::$optionalMessages = $wgOptionalMessages;
89 # Sort messages to blocks
90 $sortedMessages['unknown'] = $messages;
91 foreach( self::$messageStructure as $blockName => $block ) {
92 foreach( $block as $key ) {
93 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
94 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
95 unset( $sortedMessages['unknown'][$key] );
100 # Write all the messages
101 $messagesText = "\$messages = array(
103 foreach( $sortedMessages as $block => $messages ) {
104 # Skip if it's the block of unknown messages - handle that in the end of file
105 if( $block == 'unknown' ) {
109 if( $ignoredComments ) {
110 $ignored = self::$ignoredMessages;
111 $optional = self::$optionalMessages;
116 $comments = self::makeComments( array_keys($messages), self::$messageComments, $ignored, $optional );
119 $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
122 # Write the unknown messages, alphabetically sorted.
123 # Of course, we don't have any comments for them, because they are unknown.
124 if ( !$removeUnknown ) {
125 ksort( $sortedMessages['unknown'] );
126 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
130 return array( $messagesText, $sortedMessages );
134 * Generates an array of comments for messages.
136 * @param $messages Key of messages.
137 * @param $comments Comments for messages, indexed by key.
138 * @param $ignored List of ingored message keys.
139 * @param $optional List of optional message keys.
141 public static function makeComments( $messages, $comments, $ignored, $optional ) {
143 $commentArray = array();
146 foreach( $messages as $key ) {
147 $commentsForKey = array();
149 # Add descriptive comment for this message if there is one
150 if( array_key_exists( $key, $comments ) ) {
151 $commentsForKey[] = $comments[$key];
154 # For translator information only
155 if( in_array( $key, $ignored ) ) {
156 $commentsForKey[] = self::$ignoredComment;
157 } elseif( in_array( $key, $optional ) ) {
158 $commentsForKey[] = self::$optionalComment;
161 # Format one or more comments nicely and store in array
162 if( count( $commentsForKey ) ) {
163 $commentArray[$key] = ' # ' . implode( '; ', $commentsForKey );
167 return $commentArray;
171 * Write a block of messages to PHP.
173 * @param $blockComment The comment of whole block.
174 * @param $messages The block messages.
175 * @param $messageComments Optional comments for messages in this block.
176 * @param $prefix Prefix for every line, for indenting purposes.
178 * @return The block, formatted in PHP.
180 public static function writeMessagesBlock( $blockComment, $messages,
181 $messageComments = array(), $prefix = '' ) {
185 # Skip the block if it includes no messages
186 if( empty( $messages ) ) {
190 # Format the block comment (if exists); check for multiple lines comments
191 if( !empty( $blockComment ) ) {
192 if( strpos( $blockComment, "\n" ) === false ) {
193 $blockText .= "$prefix# $blockComment
196 $blockText .= "$prefix/*
204 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
206 # Format the messages
207 foreach( $messages as $key => $value ) {
209 $blockText .= "$prefix'$key'";
211 # Add the appropriate block whitespace
212 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
215 $blockText .= ' => ';
217 # Check for the appropriate apostrophe and add the value
218 # Quote \ here, because it needs always escaping
219 $value = addcslashes( $value, '\\' );
225 if( strpos( $value, $single ) === false ) {
226 # Nothing ugly, just use '
227 $blockText .= $single.$value.$single;
228 } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
229 # No "-quotes, no variables that need quoting, use "
230 $blockText .= $double.$value.$double;
232 # Something needs quoting, pick the quote which causes less quoting
233 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
234 if( $quote === $double ) {
239 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
245 # Add comments, if there is any
246 if( array_key_exists( $key, $messageComments ) ) {
247 $blockText .= $messageComments[$key];
255 # Newline to end the block