3 * Handle messages in the language files.
6 * @subpackage Maintenance
10 private $mLanguages; # List of languages
11 private $mRawMessages; # Raw list of the messages in each language
12 private $mMessages; # Messages in each language (except for English), divided to groups
13 private $mGeneralMessages; # General messages in English, divided to groups
14 private $mIgnoredMessages = array(
22 'exif-software-value',
27 'markaspatrolledlink',
30 'number_of_watching_users_RCview',
35 'shareddescriptionfollows',
44 ); # All the messages which should be exist only in the English file
45 private $mOptionalMessages = array(
49 'booksources-summary',
50 'ipblocklist-summary',
53 'preferences-summary',
54 'specialpages-summary',
55 'whatlinkshere-summary',
56 ); # All the messages which may be translated or not, depending on the language
57 private $mEXIFMessages = array(
62 'exif-photometricinterpretation',
64 'exif-samplesperpixel',
65 'exif-planarconfiguration',
66 'exif-ycbcrsubsampling',
67 'exif-ycbcrpositioning',
70 'exif-resolutionunit',
73 'exif-stripbytecounts',
74 'exif-jpeginterchangeformat',
75 'exif-jpeginterchangeformatlength',
76 'exif-transferfunction',
78 'exif-primarychromaticities',
79 'exif-ycbcrcoefficients',
80 'exif-referenceblackwhite',
82 'exif-imagedescription',
89 'exif-flashpixversion',
91 'exif-componentsconfiguration',
92 'exif-compressedbitsperpixel',
93 'exif-pixelydimension',
94 'exif-pixelxdimension',
97 'exif-relatedsoundfile',
98 'exif-datetimeoriginal',
99 'exif-datetimedigitized',
101 'exif-subsectimeoriginal',
102 'exif-subsectimedigitized',
104 'exif-exposuretime-format',
106 'exif-fnumber-format',
107 'exif-exposureprogram',
108 'exif-spectralsensitivity',
109 'exif-isospeedratings',
111 'exif-shutterspeedvalue',
112 'exif-aperturevalue',
113 'exif-brightnessvalue',
114 'exif-exposurebiasvalue',
115 'exif-maxaperturevalue',
116 'exif-subjectdistance',
121 'exif-focallength-format',
124 'exif-spatialfrequencyresponse',
125 'exif-focalplanexresolution',
126 'exif-focalplaneyresolution',
127 'exif-focalplaneresolutionunit',
128 'exif-subjectlocation',
129 'exif-exposureindex',
130 'exif-sensingmethod',
134 'exif-customrendered',
137 'exif-digitalzoomratio',
138 'exif-focallengthin35mmfilm',
139 'exif-scenecapturetype',
144 'exif-devicesettingdescription',
145 'exif-subjectdistancerange',
146 'exif-imageuniqueid',
148 'exif-gpslatituderef',
150 'exif-gpslongituderef',
152 'exif-gpsaltituderef',
155 'exif-gpssatellites',
157 'exif-gpsmeasuremode',
163 'exif-gpsimgdirectionref',
164 'exif-gpsimgdirection',
166 'exif-gpsdestlatituderef',
167 'exif-gpsdestlatitude',
168 'exif-gpsdestlongituderef',
169 'exif-gpsdestlongitude',
170 'exif-gpsdestbearingref',
171 'exif-gpsdestbearing',
172 'exif-gpsdestdistanceref',
173 'exif-gpsdestdistance',
174 'exif-gpsprocessingmethod',
175 'exif-gpsareainformation',
177 'exif-gpsdifferential',
178 'exif-compression-1',
179 'exif-compression-6',
180 'exif-photometricinterpretation-2',
181 'exif-photometricinterpretation-6',
182 'exif-orientation-1',
183 'exif-orientation-2',
184 'exif-orientation-3',
185 'exif-orientation-4',
186 'exif-orientation-5',
187 'exif-orientation-6',
188 'exif-orientation-7',
189 'exif-orientation-8',
190 'exif-planarconfiguration-1',
191 'exif-planarconfiguration-2',
192 'exif-xyresolution-i',
193 'exif-xyresolution-c',
195 'exif-colorspace-ffff.h',
196 'exif-componentsconfiguration-0',
197 'exif-componentsconfiguration-1',
198 'exif-componentsconfiguration-2',
199 'exif-componentsconfiguration-3',
200 'exif-componentsconfiguration-4',
201 'exif-componentsconfiguration-5',
202 'exif-componentsconfiguration-6',
203 'exif-exposureprogram-0',
204 'exif-exposureprogram-1',
205 'exif-exposureprogram-2',
206 'exif-exposureprogram-3',
207 'exif-exposureprogram-4',
208 'exif-exposureprogram-5',
209 'exif-exposureprogram-6',
210 'exif-exposureprogram-7',
211 'exif-exposureprogram-8',
212 'exif-subjectdistance-value',
213 'exif-meteringmode-0',
214 'exif-meteringmode-1',
215 'exif-meteringmode-2',
216 'exif-meteringmode-3',
217 'exif-meteringmode-4',
218 'exif-meteringmode-5',
219 'exif-meteringmode-6',
220 'exif-meteringmode-255',
221 'exif-lightsource-0',
222 'exif-lightsource-1',
223 'exif-lightsource-2',
224 'exif-lightsource-3',
225 'exif-lightsource-4',
226 'exif-lightsource-9',
227 'exif-lightsource-10',
228 'exif-lightsource-11',
229 'exif-lightsource-12',
230 'exif-lightsource-13',
231 'exif-lightsource-14',
232 'exif-lightsource-15',
233 'exif-lightsource-17',
234 'exif-lightsource-18',
235 'exif-lightsource-19',
236 'exif-lightsource-20',
237 'exif-lightsource-21',
238 'exif-lightsource-22',
239 'exif-lightsource-23',
240 'exif-lightsource-24',
241 'exif-lightsource-255',
242 'exif-focalplaneresolutionunit-2',
243 'exif-sensingmethod-1',
244 'exif-sensingmethod-2',
245 'exif-sensingmethod-3',
246 'exif-sensingmethod-4',
247 'exif-sensingmethod-5',
248 'exif-sensingmethod-7',
249 'exif-sensingmethod-8',
252 'exif-customrendered-0',
253 'exif-customrendered-1',
254 'exif-exposuremode-0',
255 'exif-exposuremode-1',
256 'exif-exposuremode-2',
257 'exif-whitebalance-0',
258 'exif-whitebalance-1',
259 'exif-scenecapturetype-0',
260 'exif-scenecapturetype-1',
261 'exif-scenecapturetype-2',
262 'exif-scenecapturetype-3',
263 'exif-gaincontrol-0',
264 'exif-gaincontrol-1',
265 'exif-gaincontrol-2',
266 'exif-gaincontrol-3',
267 'exif-gaincontrol-4',
277 'exif-subjectdistancerange-0',
278 'exif-subjectdistancerange-1',
279 'exif-subjectdistancerange-2',
280 'exif-subjectdistancerange-3',
281 'exif-gpslatitude-n',
282 'exif-gpslatitude-s',
283 'exif-gpslongitude-e',
284 'exif-gpslongitude-w',
287 'exif-gpsmeasuremode-2',
288 'exif-gpsmeasuremode-3',
292 'exif-gpsdirection-t',
293 'exif-gpsdirection-m',
294 ); # All the EXIF messages, may be set as optional if defined as such
297 * Load the list of languages: all the Messages*.php
298 * files in the languages directory.
300 * @param $exif Treat the EXIF messages?
302 function __construct( $exif = true ) {
303 $this->mLanguages = array_keys( Language::getLanguageNames( true ) );
304 sort( $this->mLanguages );
306 $this->mOptionalMessages = array_merge( $this->mOptionalMessages, $this->mEXIFMessages );
311 * Get the language list.
313 * @return The language list.
315 public function getLanguages() {
316 return $this->mLanguages;
320 * Load the raw messages for a specific langauge from the messages file.
322 * @param $code The langauge code.
324 private function loadRawMessages( $code ) {
325 if ( isset( $this->mRawMessages[$code] ) ) {
328 $filename = Language::getMessagesFileName( $code );
329 if ( file_exists( $filename ) ) {
330 require( $filename );
331 if ( isset( $messages ) ) {
332 $this->mRawMessages[$code] = $messages;
334 $this->mRawMessages[$code] = array();
337 $this->mRawMessages[$code] = array();
342 * Load the messages for a specific language (which is not English) and divide them to groups:
343 * all - all the messages.
344 * required - messages which should be translated in order to get a complete translation.
345 * optional - messages which can be translated, the fallback translation is used if not translated.
346 * obsolete - messages which should not be translated, either because they are not exist, or they are ignored messages.
347 * translated - messages which are either required or optional, but translated from English and needed.
349 * @param $code The language code.
351 private function loadMessages( $code ) {
352 if ( isset( $this->mMessages[$code] ) ) {
355 $this->loadRawMessages( $code );
356 $this->loadGeneralMessages();
357 $this->mMessages[$code]['all'] = $this->mRawMessages[$code];
358 $this->mMessages[$code]['required'] = array();
359 $this->mMessages[$code]['optional'] = array();
360 $this->mMessages[$code]['obsolete'] = array();
361 $this->mMessages[$code]['translated'] = array();
362 foreach ( $this->mMessages[$code]['all'] as $key => $value ) {
363 if ( isset( $this->mGeneralMessages['required'][$key] ) ) {
364 $this->mMessages[$code]['required'][$key] = $value;
365 $this->mMessages[$code]['translated'][$key] = $value;
366 } else if ( isset( $this->mGeneralMessages['optional'][$key] ) ) {
367 $this->mMessages[$code]['optional'][$key] = $value;
368 $this->mMessages[$code]['translated'][$key] = $value;
370 $this->mMessages[$code]['obsolete'][$key] = $value;
376 * Load the messages for English and divide them to groups:
377 * all - all the messages.
378 * required - messages which should be translated to other languages in order to get a complete translation.
379 * optional - messages which can be translated to other languages, but it's not required for a complete translation.
380 * ignored - messages which should not be translated to other languages.
381 * translatable - messages which are either required or optional, but can be translated from English.
383 private function loadGeneralMessages() {
384 if ( isset( $this->mGeneralMessages ) ) {
387 $this->loadRawMessages( 'en' );
388 $this->mGeneralMessages['all'] = $this->mRawMessages['en'];
389 $this->mGeneralMessages['required'] = array();
390 $this->mGeneralMessages['optional'] = array();
391 $this->mGeneralMessages['ignored'] = array();
392 $this->mGeneralMessages['translatable'] = array();
393 foreach ( $this->mGeneralMessages['all'] as $key => $value ) {
394 if ( in_array( $key, $this->mIgnoredMessages ) ) {
395 $this->mGeneralMessages['ignored'][$key] = $value;
396 } else if ( in_array( $key, $this->mOptionalMessages ) ) {
397 $this->mGeneralMessages['optional'][$key] = $value;
398 $this->mGeneralMessages['translatable'][$key] = $value;
400 $this->mGeneralMessages['required'][$key] = $value;
401 $this->mGeneralMessages['translatable'][$key] = $value;
407 * Get all the messages for a specific langauge (not English), without the
408 * fallback language messages, divided to groups:
409 * all - all the messages.
410 * required - messages which should be translated in order to get a complete translation.
411 * optional - messages which can be translated, the fallback translation is used if not translated.
412 * obsolete - messages which should not be translated, either because they are not exist, or they are ignored messages.
413 * translated - messages which are either required or optional, but translated from English and needed.
415 * @param $code The langauge code.
417 * @return The messages in this language.
419 public function getMessages( $code ) {
420 $this->loadMessages( $code );
421 return $this->mMessages[$code];
425 * Get all the general English messages, divided to groups:
426 * all - all the messages.
427 * required - messages which should be translated to other languages in order to get a complete translation.
428 * optional - messages which can be translated to other languages, but it's not required for a complete translation.
429 * ignored - messages which should not be translated to other languages.
430 * translatable - messages which are either required or optional, but can be translated from English.
432 * @return The general English messages.
434 public function getGeneralMessages() {
435 $this->loadGeneralMessages();
436 return $this->mGeneralMessages;
440 * Get the untranslated messages for a specific language.
442 * @param $code The langauge code.
444 * @return The untranslated messages for this language.
446 public function getUntranslatedMessages( $code ) {
447 $this->loadGeneralMessages();
448 $this->loadMessages( $code );
449 $requiredGeneralMessages = array_keys( $this->mGeneralMessages['required'] );
450 $requiredMessages = array_keys( $this->mMessages[$code]['required'] );
451 $untranslatedMessages = array();
452 foreach ( array_diff( $requiredGeneralMessages, $requiredMessages ) as $key ) {
453 $untranslatedMessages[$key] = $this->mGeneralMessages['required'][$key];
455 return $untranslatedMessages;
459 * Get the duplicate messages for a specific language.
461 * @param $code The langauge code.
463 * @return The duplicate messages for this language.
465 public function getDuplicateMessages( $code ) {
466 $this->loadGeneralMessages();
467 $this->loadMessages( $code );
468 $duplicateMessages = array();
469 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
470 if ( $this->mGeneralMessages['translatable'][$key] == $value ) {
471 $duplicateMessages[$key] = $value;
474 return $duplicateMessages;
478 * Get the messages which do not use some variables.
480 * @param $code The langauge code.
482 * @return The messages which do not use some variables in this language.
484 public function getMessagesWithoutVariables( $code ) {
485 $this->loadGeneralMessages();
486 $this->loadMessages( $code );
487 $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' );
488 $messagesWithoutVariables = array();
489 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
491 foreach ( $variables as $var ) {
492 if ( preg_match( "/$var/sU", $this->mGeneralMessages['translatable'][$key] ) &&
493 !preg_match( "/$var/sU", $value ) ) {
498 $messagesWithoutVariables[$key] = $value;
501 return $messagesWithoutVariables;
505 * Get the empty messages.
507 * @param $code The langauge code.
509 * @return The empty messages for this language.
511 public function getEmptyMessages( $code ) {
512 $this->loadGeneralMessages();
513 $this->loadMessages( $code );
514 $emptyMessages = array();
515 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
516 if ( $value === '' || $value === '-' ) {
517 $emptyMessages[$key] = $value;
520 return $emptyMessages;
524 * Get the messages with trailing whitespace.
526 * @param $code The langauge code.
528 * @return The messages with trailing whitespace in this language.
530 public function getMessagesWithWhitespace( $code ) {
531 $this->loadGeneralMessages();
532 $this->loadMessages( $code );
533 $messagesWithWhitespace = array();
534 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
535 if ( $this->mGeneralMessages['translatable'][$key] !== '' && $value !== rtrim( $value ) ) {
536 $messagesWithWhitespace[$key] = $value;
539 return $messagesWithWhitespace;
543 * Get the non-XHTML messages.
545 * @param $code The langauge code.
547 * @return The non-XHTML messages for this language.
549 public function getNonXHTMLMessages( $code ) {
550 $this->loadGeneralMessages();
551 $this->loadMessages( $code );
552 $wrongPhrases = array(
558 $wrongPhrases = '~(' . implode( '|', $wrongPhrases ) . ')~sDu';
559 $nonXHTMLMessages = array();
560 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
561 if ( preg_match( $wrongPhrases, $value ) ) {
562 $nonXHTMLMessages[$key] = $value;
565 return $nonXHTMLMessages;
569 * Get the messages which include wrong characters.
571 * @param $code The langauge code.
573 * @return The messages which include wrong characters in this language.
575 public function getMessagesWithWrongChars( $code ) {
576 $this->loadGeneralMessages();
577 $this->loadMessages( $code );
579 '[LRM]' => "\xE2\x80\x8E",
580 '[RLM]' => "\xE2\x80\x8F",
581 '[LRE]' => "\xE2\x80\xAA",
582 '[RLE]' => "\xE2\x80\xAB",
583 '[POP]' => "\xE2\x80\xAC",
584 '[LRO]' => "\xE2\x80\xAD",
585 '[RLO]' => "\xE2\x80\xAB",
586 '[ZWSP]'=> "\xE2\x80\x8B",
587 '[NBSP]'=> "\xC2\xA0",
588 '[WJ]' => "\xE2\x81\xA0",
589 '[BOM]' => "\xEF\xBB\xBF",
590 '[FFFD]'=> "\xEF\xBF\xBD",
592 $wrongRegExp = '/(' . implode( '|', array_values( $wrongChars ) ) . ')/sDu';
593 $wrongCharsMessages = array();
594 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
595 if ( preg_match( $wrongRegExp, $value ) ) {
596 foreach ( $wrongChars as $viewableChar => $hiddenChar ) {
597 $value = str_replace( $hiddenChar, $viewableChar, $value );
599 $wrongCharsMessages[$key] = $value;
602 return $wrongCharsMessages;
606 * Output a messages list
608 * @param $messages The messages list
609 * @param $code The language code
610 * @param $text The text to show before the list (optional)
611 * @param $level The display level (optional)
612 * @param $links Show links (optional)
613 * @param $wikilang The langauge of the wiki to display the list in, for the links (optional)
615 public function outputMessagesList( $messages, $code, $text = '', $level = 2, $links = false, $wikilang = null ) {
616 if ( count( $messages ) == 0 ) {
623 echo "[messages are hidden]\n";
625 foreach ( $messages as $key => $value ) {
627 $displayKey = ucfirst( $key );
628 if ( !isset( $wikilang ) ) {
630 $wikilang = $wgContLang->getCode();
632 if ( $code == $wikilang ) {
633 $displayKey = "[[MediaWiki:$displayKey|$key]]";
635 $displayKey = "[[MediaWiki:$displayKey/$code|$key]]";
641 echo "* $displayKey\n";
643 echo "* $displayKey: '$value'\n";