3 * Write a messages array as a PHP text.
5 * @addtogroup Maintenance
8 require_once( 'messages.inc' );
9 require_once( 'messageTypes.inc' );
12 * Write a messages array as a PHP text and write it to the messages file.
14 * @param $messages The messages array.
15 * @param $code The language code.
16 * @param $write Write to the messages file?
17 * @param $listUnknown List the unknown messages?
19 function writeMessagesToFile( $messages, $code, $write, $listUnknown ) {
20 # Rewrite the messages array
21 $messages = writeMessagesArray( $messages, $code == 'en' );
22 $messagesText = $messages[0];
23 $sortedMessages = $messages[1];
26 $filename = Language::getMessagesFileName( $code );
27 $contents = file_get_contents( $filename );
28 if ( strpos( $contents, '$messages' ) !== false ) {
29 $contents = explode( '$messages', $contents );
30 if ( $messagesText == '$messages' . $contents[1] ) {
31 echo "Generated messages for language $code. Same as the current file.\n";
35 $new .= $messagesText;
36 file_put_contents( $filename, $new );
37 echo "Generated and wrote messages for language $code.\n";
39 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
42 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
43 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
44 foreach ( $sortedMessages['unknown'] as $key => $value ) {
45 echo "* " . $key . "\n";
49 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
54 * Write a messages array as a PHP text.
56 * @param $messages The messages array.
57 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
59 * @return Array of the PHP text and the sorted messages array.
61 function writeMessagesArray( $messages, $ignoredComments = false ) {
62 global $wgMessageStructure, $wgBlockComments;
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 .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
87 ksort( $sortedMessages['unknown'] );
88 $messagesText .= 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 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
106 global $wgMessageComments, $wgMessagseWithDollarSigns;
107 global $wgIgnoredMessages, $wgOptionalMessages;
110 # Skip the block if it includes no messages
111 if ( empty( $messages ) ) {
115 # Format the block comment (if exists); check for multiple lines comments
116 if ( !empty( $comment ) ) {
117 if ( strpos( $comment, "\n" ) === false ) {
118 $blockText .= "# $comment
129 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
131 # Format the messages
132 foreach( $messages as $key => $value ) {
134 $blockText .= "'$key'";
136 # Add the appropriate block whitespace
137 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
140 $blockText .= ' => ';
142 # Check for the appropriate apostrophe and add the value
143 if ( strpos( $value, "'" ) === false ) {
144 $blockText .= "'$value'";
145 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
146 $blockText .= "\"$value\"";
148 # Pick the less numerous one to escape
149 $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
150 if ('"' == $quote) { $extra = '$'; }
151 else { $extra = ''; }
152 $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
158 $ignoredComment = "don't translate or duplicate this message to other languages";
159 $optionalComment = "only translate this message to other languages if you have to change it";
160 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
161 if ( $ignoredComments ) {
162 if ( array_key_exists( $key, $wgMessageComments ) ) {
163 $blockText .= ' # ' . $wgMessageComments[$key];
164 if ( $showIgnoredOrOptionalComment ) {
167 } elseif ( $showIgnoredOrOptionalComment ) {
170 if ( in_array( $key, $wgIgnoredMessages ) ) {
171 $blockText .= $ignoredComment;
172 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
173 $blockText .= $optionalComment;
175 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
176 $blockText .= ' # ' . $wgMessageComments[$key];
184 # Newline to end the block