3 * Write a messages array as a PHP text.
5 * @addtogroup Maintenance
9 static $optionalComment = 'only translate this message to other languages if you have to change it';
10 static $ignoredComment = "do not translate or duplicate this message to other languages";
12 static $loaded = false;
13 static $messageStructure;
14 static $blockComments;
15 static $messageComments;
16 static $ignoredMessages;
17 static $optionalMessages;
20 * Write a messages array as a PHP text and write it to the messages file.
22 * @param $messages The messages array.
23 * @param $code The language code.
24 * @param $write Write to the messages file?
25 * @param $listUnknown List the unknown messages?
27 public static function writeMessagesToFile( $messages, $code, $write, $listUnknown ) {
28 # Rewrite the messages array
29 $messages = self::writeMessagesArray( $messages, $code == 'en' );
30 $messagesText = $messages[0];
31 $sortedMessages = $messages[1];
34 $filename = Language::getMessagesFileName( $code );
35 $contents = file_get_contents( $filename );
36 if( strpos( $contents, '$messages' ) !== false ) {
37 $contents = explode( '$messages', $contents );
38 if( $messagesText == '$messages' . $contents[1] ) {
39 echo "Generated messages for language $code. Same as the current file.\n";
43 $new .= $messagesText;
44 file_put_contents( $filename, $new );
45 echo "Generated and wrote messages for language $code.\n";
47 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
50 if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
51 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
52 foreach( $sortedMessages['unknown'] as $key => $value ) {
53 echo "* " . $key . "\n";
57 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
62 * Write a messages array as a PHP text.
64 * @param $messages The messages array.
65 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
67 * @return Array of the PHP text and the sorted messages array.
69 public static function writeMessagesArray( $messages, $ignoredComments = false ) {
71 if( !self::$loaded ) {
72 require( dirname( __FILE__ ) . '/messages.inc' );
73 self::$messageStructure = $wgMessageStructure;
74 self::$blockComments = $wgBlockComments;
75 self::$messageComments = $wgMessageComments;
77 require( dirname( __FILE__ ) . '/messageTypes.inc' );
78 self::$ignoredMessages = $wgIgnoredMessages;
79 self::$optionalMessages = $wgOptionalMessages;
84 # Sort messages to blocks
85 $sortedMessages['unknown'] = $messages;
86 foreach( self::$messageStructure as $blockName => $block ) {
87 foreach( $block as $key ) {
88 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
89 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
90 unset( $sortedMessages['unknown'][$key] );
95 # Write all the messages
96 $messagesText = "\$messages = array(
98 foreach( $sortedMessages as $block => $messages ) {
99 # Skip if it's the block of unknown messages - handle that in the end of file
100 if( $block == 'unknown' ) {
104 if( $ignoredComments ) {
105 $ignored = self::$ignoredMessages;
106 $optional = self::$optionalMessages;
111 $comments = self::makeComments( array_keys($messages), self::$messageComments, $ignored, $optional );
114 $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
117 # Write the unknown messages, alphabetically sorted.
118 # Of course, we don't have any comments for them, because the are unknown.
119 ksort( $sortedMessages['unknown'] );
120 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
124 return array( $messagesText, $sortedMessages );
128 * Generates an array of comments for messages.
130 * @param $messages Key of messages.
131 * @param $comments Comments for messages, indexed by key.
132 * @param $ignored List of ingored message keys.
133 * @param $optional List of optional message keys.
135 public static function makeComments( $messages, $comments, $ignored, $optional ) {
137 $commentArray = array();
140 foreach( $messages as $key ) {
141 $commentsForKey = array();
143 # Add descriptive comment for this message if there is one
144 if( array_key_exists( $key, $comments ) ) {
145 $commentsForKey[] = $comments[$key];
148 # For translator information only
149 if( in_array( $key, $ignored ) ) {
150 $commentsForKey[] = self::$ignoredComment;
151 } elseif( in_array( $key, $optional ) ) {
152 $commentsForKey[] = self::$optionalComment;
155 # Format one or more comments nicely and store in array
156 if( count( $commentsForKey ) ) {
157 $commentArray[$key] = ' # ' . implode( '; ', $commentsForKey );
161 return $commentArray;
165 * Write a block of messages to PHP.
167 * @param $blockComment The comment of whole block.
168 * @param $messages The block messages.
169 * @param $messageComments Optional comments for messages in this block.
170 * @param $prefix Prefix for every line, for indenting purposes.
172 * @return The block, formatted in PHP.
174 public static function writeMessagesBlock( $blockComment, $messages,
175 $messageComments = array(), $prefix = '' ) {
179 # Skip the block if it includes no messages
180 if( empty( $messages ) ) {
184 # Format the block comment (if exists); check for multiple lines comments
185 if( !empty( $blockComment ) ) {
186 if( strpos( $blockComment, "\n" ) === false ) {
187 $blockText .= "$prefix# $blockComment
190 $blockText .= "$prefix/*
198 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
200 # Format the messages
201 foreach( $messages as $key => $value ) {
203 $blockText .= "$prefix'$key'";
205 # Add the appropriate block whitespace
206 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
209 $blockText .= ' => ';
211 # Check for the appropriate apostrophe and add the value
212 # Quote \ here, because it needs always escaping
213 $value = addcslashes( $value, '\\' );
219 if( strpos( $value, $single ) === false ) {
220 # Nothing ugly, just use '
221 $blockText .= $single.$value.$single;
222 } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
223 # No "-quotes, no variables that need quoting, use "
224 $blockText .= $double.$value.$double;
226 # Something needs quoting, pick the quote which causes less quoting
227 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
228 if( $quote === $double ) {
233 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
239 # Add comments, if there is any
240 if( array_key_exists( $key, $messageComments ) ) {
241 $blockText .= $messageComments[$key];
249 # Newline to end the block