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 ) {
31 # Rewrite the messages array
32 $messages = self::writeMessagesArray( $messages, $code == 'en' );
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'] ) ) {
54 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
55 foreach( $sortedMessages['unknown'] as $key => $value ) {
56 echo "* " . $key . "\n";
60 echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
65 * Write a messages array as a PHP text.
67 * @param $messages The messages array.
68 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
70 * @return Array of the PHP text and the sorted messages array.
72 public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false ) {
74 $dir = $prefix ? $prefix : dirname( __FILE__ );
76 require( $dir . '/messages.inc' );
77 self::$messageStructure = $wgMessageStructure;
78 self::$blockComments = $wgBlockComments;
79 self::$messageComments = $wgMessageComments;
81 require( $dir . '/messageTypes.inc' );
82 self::$ignoredMessages = $wgIgnoredMessages;
83 self::$optionalMessages = $wgOptionalMessages;
86 # Sort messages to blocks
87 $sortedMessages['unknown'] = $messages;
88 foreach( self::$messageStructure as $blockName => $block ) {
89 foreach( $block as $key ) {
90 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
91 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
92 unset( $sortedMessages['unknown'][$key] );
97 # Write all the messages
98 $messagesText = "\$messages = array(
100 foreach( $sortedMessages as $block => $messages ) {
101 # Skip if it's the block of unknown messages - handle that in the end of file
102 if( $block == 'unknown' ) {
106 if( $ignoredComments ) {
107 $ignored = self::$ignoredMessages;
108 $optional = self::$optionalMessages;
113 $comments = self::makeComments( array_keys($messages), self::$messageComments, $ignored, $optional );
116 $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
119 # Write the unknown messages, alphabetically sorted.
120 # Of course, we don't have any comments for them, because the are unknown.
121 ksort( $sortedMessages['unknown'] );
122 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
126 return array( $messagesText, $sortedMessages );
130 * Generates an array of comments for messages.
132 * @param $messages Key of messages.
133 * @param $comments Comments for messages, indexed by key.
134 * @param $ignored List of ingored message keys.
135 * @param $optional List of optional message keys.
137 public static function makeComments( $messages, $comments, $ignored, $optional ) {
139 $commentArray = array();
142 foreach( $messages as $key ) {
143 $commentsForKey = array();
145 # Add descriptive comment for this message if there is one
146 if( array_key_exists( $key, $comments ) ) {
147 $commentsForKey[] = $comments[$key];
150 # For translator information only
151 if( in_array( $key, $ignored ) ) {
152 $commentsForKey[] = self::$ignoredComment;
153 } elseif( in_array( $key, $optional ) ) {
154 $commentsForKey[] = self::$optionalComment;
157 # Format one or more comments nicely and store in array
158 if( count( $commentsForKey ) ) {
159 $commentArray[$key] = ' # ' . implode( '; ', $commentsForKey );
163 return $commentArray;
167 * Write a block of messages to PHP.
169 * @param $blockComment The comment of whole block.
170 * @param $messages The block messages.
171 * @param $messageComments Optional comments for messages in this block.
172 * @param $prefix Prefix for every line, for indenting purposes.
174 * @return The block, formatted in PHP.
176 public static function writeMessagesBlock( $blockComment, $messages,
177 $messageComments = array(), $prefix = '' ) {
181 # Skip the block if it includes no messages
182 if( empty( $messages ) ) {
186 # Format the block comment (if exists); check for multiple lines comments
187 if( !empty( $blockComment ) ) {
188 if( strpos( $blockComment, "\n" ) === false ) {
189 $blockText .= "$prefix# $blockComment
192 $blockText .= "$prefix/*
200 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
202 # Format the messages
203 foreach( $messages as $key => $value ) {
205 $blockText .= "$prefix'$key'";
207 # Add the appropriate block whitespace
208 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
211 $blockText .= ' => ';
213 # Check for the appropriate apostrophe and add the value
214 # Quote \ here, because it needs always escaping
215 $value = addcslashes( $value, '\\' );
221 if( strpos( $value, $single ) === false ) {
222 # Nothing ugly, just use '
223 $blockText .= $single.$value.$single;
224 } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
225 # No "-quotes, no variables that need quoting, use "
226 $blockText .= $double.$value.$double;
228 # Something needs quoting, pick the quote which causes less quoting
229 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
230 if( $quote === $double ) {
235 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
241 # Add comments, if there is any
242 if( array_key_exists( $key, $messageComments ) ) {
243 $blockText .= $messageComments[$key];
251 # Newline to end the block