29 'only translate this message to other languages if you have to change it';
30 protected static $ignoredComment =
"do not translate or duplicate this message to other languages";
51 # Rewrite the messages array
63 if ( file_exists( $filename ) ) {
64 $contents = file_get_contents( $filename );
72 if ( strpos( $contents,
'$messages' ) !==
false ) {
73 $contents = explode(
'$messages', $contents );
74 if ( $messagesText ==
'$messages' . $contents[1] ) {
75 echo
"Generated messages for language $code. Same as the current file.\n";
79 $new .= $messagesText;
80 file_put_contents( $filename, $new );
81 echo
"Generated and wrote messages for language $code.\n";
83 echo
"Generated messages for language $code.\n" .
84 "Please run the script again (without the parameter \"dry-run\") " .
85 "to write the array to the file.\n";
88 if ( $listUnknown && isset( $sortedMessages[
'unknown'] ) &&
89 !empty( $sortedMessages[
'unknown'] )
91 if ( $removeUnknown ) {
92 echo
"\nThe following " . count( $sortedMessages[
'unknown'] ) .
93 " unknown messages have been removed:\n";
95 echo
"\nThere are " . count( $sortedMessages[
'unknown'] ) .
96 " unknown messages, please check them:\n";
98 foreach ( $sortedMessages[
'unknown']
as $key =>
$value ) {
99 echo
"* " . $key .
"\n";
103 echo
"Generated messages for language $code. There seem to be no messages array in the file.\n";
120 $prefix =
false, $removeUnknown =
false
123 $dir = $prefix ? $prefix : __DIR__;
125 require
$dir .
'/messages.inc';
129 require
$dir .
'/messageTypes.inc';
133 # Sort messages to blocks
135 foreach ( self::$messageStructure
as $blockName => $block ) {
139 foreach ( $block
as $key ) {
140 if ( array_key_exists( $key, $sortedMessages[
'unknown'] ) ) {
141 $sortedMessages[$blockName][$key] = $sortedMessages[
'unknown'][$key];
142 unset( $sortedMessages[
'unknown'][$key] );
147 # Write all the messages
148 $messagesText =
"\$messages = array(
150 foreach ( $sortedMessages
as $block =>
$messages ) {
151 # Skip if it's the block of unknown messages - handle that in the end of file
152 if ( $block ==
'unknown' ) {
156 if ( $ignoredComments ) {
169 # Write the unknown messages, alphabetically sorted.
170 # Of course, we don't have any comments for them, because they are unknown.
171 if ( !$removeUnknown ) {
172 ksort( $sortedMessages[
'unknown'] );
178 return array( $messagesText, $sortedMessages );
191 $commentArray =
array();
195 if ( in_array( $key, $ignored ) ) {
197 } elseif ( in_array( $key, $optional ) ) {
202 return $commentArray;
216 $messageComments =
array(), $prefix =
'' ) {
220 # Skip the block if it includes no messages
225 # Format the block comment (if exists); check for multiple lines comments
226 if ( !empty( $blockComment ) ) {
227 if ( strpos( $blockComment,
"\n" ) ===
false ) {
228 $blockText .=
"$prefix# $blockComment
231 $blockText .=
"$prefix/*
239 $maxKeyLength = max( array_map(
'strlen', array_keys(
$messages ) ) );
241 # Format the messages
244 $blockText .=
"$prefix'$key'";
246 # Add the appropriate block whitespace
247 $blockText .= str_repeat(
' ', $maxKeyLength - strlen( $key ) );
250 $blockText .=
' => ';
252 # Check for the appropriate apostrophe and add the value
253 # Quote \ here, because it needs always escaping
260 if ( strpos(
$value, $single ) ===
false ) {
261 # Nothing ugly, just use '
262 $blockText .= $single .
$value . $single;
263 } elseif ( strpos(
$value, $double ) ===
false &&
264 !preg_match(
'/\$[a-zA-Z_\x7f-\xff]/',
$value )
266 # No "-quotes, no variables that need quoting, use "
267 $blockText .= $double .
$value . $double;
269 # Something needs quoting, pick the quote which causes less quoting
271 if ( substr_count(
$value, $double ) + substr_count(
$value,
'$' ) >=
272 substr_count(
$value, $single )
279 if ( $quote === $double ) {
284 $blockText .= $quote . addcslashes(
$value, $quote . $extra ) . $quote;
290 # Add comments, if there is any
291 if ( array_key_exists( $key, $messageComments ) ) {
292 $blockText .= $messageComments[$key];
300 # Newline to end the block