3 * Write a messages array as a PHP text.
5 * @addtogroup Maintenance
11 * Write a messages array as a PHP text and write it to the messages file.
13 * @param $messages The messages array.
14 * @param $code The language code.
15 * @param $write Write to the messages file?
16 * @param $listUnknown List the unknown messages?
18 public static function writeMessagesToFile( $messages, $code, $write, $listUnknown ) {
19 # Rewrite the messages array
20 $messages = self::writeMessagesArray( $messages, $code == 'en' );
21 $messagesText = $messages[0];
22 $sortedMessages = $messages[1];
25 $filename = Language::getMessagesFileName( $code );
26 $contents = file_get_contents( $filename );
27 if ( strpos( $contents, '$messages' ) !== false ) {
28 $contents = explode( '$messages', $contents );
29 if ( $messagesText == '$messages' . $contents[1] ) {
30 echo "Generated messages for language $code. Same as the current file.\n";
34 $new .= $messagesText;
35 file_put_contents( $filename, $new );
36 echo "Generated and wrote messages for language $code.\n";
38 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
41 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
42 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
43 foreach ( $sortedMessages['unknown'] as $key => $value ) {
44 echo "* " . $key . "\n";
48 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
53 * Write a messages array as a PHP text.
55 * @param $messages The messages array.
56 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
58 * @return Array of the PHP text and the sorted messages array.
60 public static function writeMessagesArray( $messages, $ignoredComments = false ) {
61 #$wgMessageStructure, $wgBlockComments;
62 require( dirname( __FILE__ ) . '/messages.inc' );
64 # Sort messages to blocks
65 $sortedMessages['unknown'] = $messages;
66 foreach ( $wgMessageStructure as $blockName => $block ) {
67 foreach ( $block as $key ) {
68 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
69 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
70 unset( $sortedMessages['unknown'][$key] );
75 # Write all the messages
76 $messagesText = "\$messages = array(
78 foreach( $sortedMessages as $block => $messages ) {
79 # Skip if it's the block of unknown messages - handle that in the end of file
80 if ( $block == 'unknown' ) {
85 $messagesText .= self::writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
87 ksort( $sortedMessages['unknown'] );
88 $messagesText .= self::writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'], $ignoredComments ); # Write the unknown messages, alphabetically sorted
92 return array( $messagesText, $sortedMessages );
96 * Write a block of messages to PHP.
98 * @param $name The block name.
99 * @param $comment The block comment.
100 * @param $messages The block messages.
101 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
103 * @return The block, formatted in PHP.
105 public static function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
107 require( dirname( __FILE__ ) . '/messages.inc' );
108 # $wgIgnoredMessages, $wgOptionalMessages;
109 require( dirname( __FILE__ ) . '/messageTypes.inc' );
113 # Skip the block if it includes no messages
114 if ( empty( $messages ) ) {
118 # Format the block comment (if exists); check for multiple lines comments
119 if ( !empty( $comment ) ) {
120 if ( strpos( $comment, "\n" ) === false ) {
121 $blockText .= "# $comment
132 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
134 # Format the messages
135 foreach( $messages as $key => $value ) {
137 $blockText .= "'$key'";
139 # Add the appropriate block whitespace
140 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
143 $blockText .= ' => ';
145 # Check for the appropriate apostrophe and add the value
146 if ( strpos( $value, "'" ) === false ) {
147 $blockText .= "'$value'";
148 } elseif ( strpos( $value, '"' ) === false && !preg_match('/\$[a-zA-Z]/', $value) ) {
149 $blockText .= "\"$value\"";
151 # Pick the less numerous one to escape
152 $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
153 if ('"' == $quote) { $extra = '$'; }
154 else { $extra = ''; }
155 $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
161 $ignoredComment = "don't translate or duplicate this message to other languages";
162 $optionalComment = "only translate this message to other languages if you have to change it";
163 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
164 if ( $ignoredComments ) {
165 if ( array_key_exists( $key, $wgMessageComments ) ) {
166 $blockText .= ' # ' . $wgMessageComments[$key];
167 if ( $showIgnoredOrOptionalComment ) {
170 } elseif ( $showIgnoredOrOptionalComment ) {
173 if ( in_array( $key, $wgIgnoredMessages ) ) {
174 $blockText .= $ignoredComment;
175 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
176 $blockText .= $optionalComment;
178 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
179 $blockText .= ' # ' . $wgMessageComments[$key];
187 # Newline to end the block
194 } // CLASS MessageWriter