07925538b3a0ddc2873fc4134e91e2f8e15ec5c2
[lhc/web/wiklou.git] / maintenance / language / writeMessagesArray.inc
1 <?php
2 /**
3 * Write a messages array as a PHP text.
4 *
5 * @package MediaWiki
6 * @subpackage Maintenance
7 */
8
9 require_once( 'messages.inc' );
10 require_once( 'messageTypes.inc' );
11
12 /**
13 * Write a messages array as a PHP text.
14 *
15 * @param $messages The messages array.
16 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
17 *
18 * @return The PHP text.
19 */
20 function writeMessagesArray( $messages, $ignoredComments = false ) {
21 global $wgMessageStrucutre, $wgBlockComments;
22
23 # Sort messages to blocks
24 $sortedMessages['unknown'] = $messages;
25 foreach ( $wgMessageStrucutre as $blockName => $block ) {
26 foreach ( $block as $key ) {
27 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
28 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
29 unset( $sortedMessages['unknown'][$key] );
30 }
31 }
32 }
33
34 # Write all the messages
35 $messagesText = "\$messages = array(\n";
36 foreach( $sortedMessages as $block => $messages ) {
37 # Skip if it's the block of unknown messages - handle that in the end of file
38 if ( $block == 'unknown' ) {
39 continue;
40 }
41
42 # Write the block
43 $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
44 }
45 ksort( $sortedMessages['unknown'] );
46 $messagesText .= writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'], $ignoredComments ); # Write the unknown messages, alphabetically sorted
47 $messagesText .= ");\n";
48
49 return $messagesText;
50 }
51
52 /**
53 * Write a block of messages to PHP.
54 *
55 * @param $name The block name.
56 * @param $comment The block comment.
57 * @param $messages The block messages.
58 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
59 *
60 * @return The block, formatted in PHP.
61 */
62 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
63 global $wgMessageComments, $wgMessagseWithDollarSigns;
64 global $wgIgnoredMessages, $wgOptionalMessages;
65 $blockText = '';
66
67 # Skip the block if it includes no messages
68 if ( empty( $messages ) ) {
69 return '';
70 }
71
72 # Format the block comment (if exists); check for multiple lines comments
73 if ( !empty( $comment ) ) {
74 if ( strpos( $comment, "\n" ) === false ) {
75 $blockText .= "# $comment\n";
76 } else {
77 $blockText .= "/*\n$comment\n*/\n";
78 }
79 }
80
81 # Get max key length
82 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
83
84 # Format the messages
85 foreach( $messages as $key => $value ) {
86 # Add the key name
87 $blockText .= "'$key'";
88
89 # Add the appropriate block whitespace
90 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
91
92 # Refer to the value
93 $blockText .= ' => ';
94
95 # Check for the appropriate apostrophe and add the value
96 if ( strpos( $value, "'" ) === false ) {
97 $blockText .= "'$value'";
98 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
99 $blockText .= "\"$value\"";
100 } else {
101 # Pick the less numerous one to escape
102 $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
103 if ('"' == $quote) { $extra = '$'; }
104 else { $extra = ''; }
105 $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
106 }
107
108 # Comma
109 $blockText .= ',';
110
111 $ignoredComment = "don't translate or duplicate this message to other languages";
112 $optionalComment = "only translate this message to other languages if you have to change it";
113 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
114 if ( $ignoredComments ) {
115 if ( array_key_exists( $key, $wgMessageComments ) ) {
116 $blockText .= ' # ' . $wgMessageComments[$key];
117 if ( $showIgnoredOrOptionalComment ) {
118 $blockText .= '; ';
119 }
120 } elseif ( $showIgnoredOrOptionalComment ) {
121 $blockText .= ' # ';
122 }
123 if ( in_array( $key, $wgIgnoredMessages ) ) {
124 $blockText .= $ignoredComment;
125 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
126 $blockText .= $optionalComment;
127 }
128 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
129 $blockText .= ' # ' . $wgMessageComments[$key];
130 }
131
132 # Newline
133 $blockText .= "\n";
134 }
135
136 # Newline to end the block
137 $blockText .= "\n";
138
139 return $blockText;
140 }
141
142 ?>