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 . "\n?>\n" == '$messages' . $contents[1] ) {
31 echo "Generated messages for language $code. Same to the current file.\n";
35 $new .= $messagesText;
37 file_put_contents( $filename, $new );
38 echo "Generated and wrote messages for language $code.\n";
40 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
43 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
44 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
45 foreach ( $sortedMessages['unknown'] as $key => $value ) {
46 echo "* " . $key . "\n";
50 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
55 * Write a messages array as a PHP text.
57 * @param $messages The messages array.
58 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
60 * @return Array of the PHP text and the sorted messages array.
62 function writeMessagesArray( $messages, $ignoredComments = false ) {
63 global $wgMessageStructure, $wgBlockComments;
65 # Sort messages to blocks
66 $sortedMessages['unknown'] = $messages;
67 foreach ( $wgMessageStructure as $blockName => $block ) {
68 foreach ( $block as $key ) {
69 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
70 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
71 unset( $sortedMessages['unknown'][$key] );
76 # Write all the messages
77 $messagesText = "\$messages = array(\n";
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
89 $messagesText .= ");\n";
91 return array( $messagesText, $sortedMessages );
95 * Write a block of messages to PHP.
97 * @param $name The block name.
98 * @param $comment The block comment.
99 * @param $messages The block messages.
100 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
102 * @return The block, formatted in PHP.
104 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
105 global $wgMessageComments, $wgMessagseWithDollarSigns;
106 global $wgIgnoredMessages, $wgOptionalMessages;
109 # Skip the block if it includes no messages
110 if ( empty( $messages ) ) {
114 # Format the block comment (if exists); check for multiple lines comments
115 if ( !empty( $comment ) ) {
116 if ( strpos( $comment, "\n" ) === false ) {
117 $blockText .= "# $comment\n";
119 $blockText .= "/*\n$comment\n*/\n";
124 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
126 # Format the messages
127 foreach( $messages as $key => $value ) {
129 $blockText .= "'$key'";
131 # Add the appropriate block whitespace
132 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
135 $blockText .= ' => ';
137 # Check for the appropriate apostrophe and add the value
138 if ( strpos( $value, "'" ) === false ) {
139 $blockText .= "'$value'";
140 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
141 $blockText .= "\"$value\"";
143 # Pick the less numerous one to escape
144 $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
145 if ('"' == $quote) { $extra = '$'; }
146 else { $extra = ''; }
147 $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
153 $ignoredComment = "don't translate or duplicate this message to other languages";
154 $optionalComment = "only translate this message to other languages if you have to change it";
155 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
156 if ( $ignoredComments ) {
157 if ( array_key_exists( $key, $wgMessageComments ) ) {
158 $blockText .= ' # ' . $wgMessageComments[$key];
159 if ( $showIgnoredOrOptionalComment ) {
162 } elseif ( $showIgnoredOrOptionalComment ) {
165 if ( in_array( $key, $wgIgnoredMessages ) ) {
166 $blockText .= $ignoredComment;
167 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
168 $blockText .= $optionalComment;
170 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
171 $blockText .= ' # ' . $wgMessageComments[$key];
178 # Newline to end the block