3 * Handle messages in the language files.
6 * @ingroup MaintenanceLanguage
10 * @ingroup MaintenanceLanguage
13 protected $mLanguages; # List of languages
15 protected $mRawMessages; # Raw list of the messages in each language
16 protected $mMessages; # Messages in each language (except for English), divided to groups
17 protected $mFallback; # Fallback language in each language
18 protected $mGeneralMessages; # General messages in English, divided to groups
19 protected $mIgnoredMessages; # All the messages which should be exist only in the English file
20 protected $mOptionalMessages; # All the messages which may be translated or not, depending on the language
22 protected $mNamespaceNames; # Namespace names
23 protected $mNamespaceAliases; # Namespace aliases
24 protected $mMagicWords; # Magic words
25 protected $mSpecialPageAliases; # Special page aliases
28 * Load the list of languages: all the Messages*.php
29 * files in the languages directory.
31 * @param $exif Treat the EXIF messages?
33 function __construct( $exif = true ) {
34 require( dirname(__FILE__) . '/messageTypes.inc' );
35 $this->mIgnoredMessages = $wgIgnoredMessages;
37 $this->mOptionalMessages = array_merge( $wgOptionalMessages );
39 $this->mOptionalMessages = array_merge( $wgOptionalMessages, $wgEXIFMessages );
42 $this->mLanguages = array_keys( Language::getLanguageNames( true ) );
43 sort( $this->mLanguages );
47 * Get the language list.
49 * @return The language list.
51 public function getLanguages() {
52 return $this->mLanguages;
56 * Get the ignored messages list.
58 * @return The ignored messages list.
60 public function getIgnoredMessages() {
61 return $this->mIgnoredMessages;
65 * Get the optional messages list.
67 * @return The optional messages list.
69 public function getOptionalMessages() {
70 return $this->mOptionalMessages;
74 * Load the language file.
76 * @param $code The language code.
78 protected function loadFile( $code ) {
79 if ( isset( $this->mRawMessages[$code] ) &&
80 isset( $this->mFallback[$code] ) &&
81 isset( $this->mNamespaceNames[$code] ) &&
82 isset( $this->mNamespaceAliases[$code] ) &&
83 isset( $this->mMagicWords[$code] ) &&
84 isset( $this->mSpecialPageAliases[$code] ) ) {
87 $this->mRawMessages[$code] = array();
88 $this->mFallback[$code] = '';
89 $this->mNamespaceNames[$code] = array();
90 $this->mNamespaceAliases[$code] = array();
91 $this->mMagicWords[$code] = array();
92 $this->mSpecialPageAliases[$code] = array();
93 $filename = Language::getMessagesFileName( $code );
94 if ( file_exists( $filename ) ) {
96 if ( isset( $messages ) ) {
97 $this->mRawMessages[$code] = $messages;
99 if ( isset( $fallback ) ) {
100 $this->mFallback[$code] = $fallback;
102 if ( isset( $namespaceNames ) ) {
103 $this->mNamespaceNames[$code] = $namespaceNames;
105 if ( isset( $namespaceAliases ) ) {
106 $this->mNamespaceAliases[$code] = $namespaceAliases;
108 if ( isset( $magicWords ) ) {
109 $this->mMagicWords[$code] = $magicWords;
111 if ( isset( $specialPageAliases ) ) {
112 $this->mSpecialPageAliases[$code] = $specialPageAliases;
118 * Load the messages for a specific language (which is not English) and divide them to groups:
119 * all - all the messages.
120 * required - messages which should be translated in order to get a complete translation.
121 * optional - messages which can be translated, the fallback translation is used if not translated.
122 * obsolete - messages which should not be translated, either because they do not exist, or they are ignored messages.
123 * translated - messages which are either required or optional, but translated from English and needed.
125 * @param $code The language code.
127 private function loadMessages( $code ) {
128 if ( isset( $this->mMessages[$code] ) ) {
131 $this->loadFile( $code );
132 $this->loadGeneralMessages();
133 $this->mMessages[$code]['all'] = $this->mRawMessages[$code];
134 $this->mMessages[$code]['required'] = array();
135 $this->mMessages[$code]['optional'] = array();
136 $this->mMessages[$code]['obsolete'] = array();
137 $this->mMessages[$code]['translated'] = array();
138 foreach ( $this->mMessages[$code]['all'] as $key => $value ) {
139 if ( isset( $this->mGeneralMessages['required'][$key] ) ) {
140 $this->mMessages[$code]['required'][$key] = $value;
141 $this->mMessages[$code]['translated'][$key] = $value;
142 } else if ( isset( $this->mGeneralMessages['optional'][$key] ) ) {
143 $this->mMessages[$code]['optional'][$key] = $value;
144 $this->mMessages[$code]['translated'][$key] = $value;
146 $this->mMessages[$code]['obsolete'][$key] = $value;
152 * Load the messages for English and divide them to groups:
153 * all - all the messages.
154 * required - messages which should be translated to other languages in order to get a complete translation.
155 * optional - messages which can be translated to other languages, but it's not required for a complete translation.
156 * ignored - messages which should not be translated to other languages.
157 * translatable - messages which are either required or optional, but can be translated from English.
159 private function loadGeneralMessages() {
160 if ( isset( $this->mGeneralMessages ) ) {
163 $this->loadFile( 'en' );
164 $this->mGeneralMessages['all'] = $this->mRawMessages['en'];
165 $this->mGeneralMessages['required'] = array();
166 $this->mGeneralMessages['optional'] = array();
167 $this->mGeneralMessages['ignored'] = array();
168 $this->mGeneralMessages['translatable'] = array();
169 foreach ( $this->mGeneralMessages['all'] as $key => $value ) {
170 if ( in_array( $key, $this->mIgnoredMessages ) ) {
171 $this->mGeneralMessages['ignored'][$key] = $value;
172 } else if ( in_array( $key, $this->mOptionalMessages ) ) {
173 $this->mGeneralMessages['optional'][$key] = $value;
174 $this->mGeneralMessages['translatable'][$key] = $value;
176 $this->mGeneralMessages['required'][$key] = $value;
177 $this->mGeneralMessages['translatable'][$key] = $value;
183 * Get all the messages for a specific language (not English), without the
184 * fallback language messages, divided to groups:
185 * all - all the messages.
186 * required - messages which should be translated in order to get a complete translation.
187 * optional - messages which can be translated, the fallback translation is used if not translated.
188 * obsolete - messages which should not be translated, either because they do not exist, or they are ignored messages.
189 * translated - messages which are either required or optional, but translated from English and needed.
191 * @param $code The language code.
193 * @return The messages in this language.
195 public function getMessages( $code ) {
196 $this->loadMessages( $code );
197 return $this->mMessages[$code];
201 * Get all the general English messages, divided to groups:
202 * all - all the messages.
203 * required - messages which should be translated to other languages in order to get a complete translation.
204 * optional - messages which can be translated to other languages, but it's not required for a complete translation.
205 * ignored - messages which should not be translated to other languages.
206 * translatable - messages which are either required or optional, but can be translated from English.
208 * @return The general English messages.
210 public function getGeneralMessages() {
211 $this->loadGeneralMessages();
212 return $this->mGeneralMessages;
216 * Get fallback language code for a specific language.
218 * @param $code The language code.
220 * @return Fallback code.
222 public function getFallback( $code ) {
223 $this->loadFile( $code );
224 return $this->mFallback[$code];
228 * Get namespace names for a specific language.
230 * @param $code The language code.
232 * @return Namespace names.
234 public function getNamespaceNames( $code ) {
235 $this->loadFile( $code );
236 return $this->mNamespaceNames[$code];
240 * Get namespace aliases for a specific language.
242 * @param $code The language code.
244 * @return Namespace aliases.
246 public function getNamespaceAliases( $code ) {
247 $this->loadFile( $code );
248 return $this->mNamespaceAliases[$code];
252 * Get magic words for a specific language.
254 * @param $code The language code.
256 * @return Magic words.
258 public function getMagicWords( $code ) {
259 $this->loadFile( $code );
260 return $this->mMagicWords[$code];
264 * Get special page aliases for a specific language.
266 * @param $code The language code.
268 * @return Special page aliases.
270 public function getSpecialPageAliases( $code ) {
271 $this->loadFile( $code );
272 return $this->mSpecialPageAliases[$code];
276 * Get the untranslated messages for a specific language.
278 * @param $code The language code.
280 * @return The untranslated messages for this language.
282 public function getUntranslatedMessages( $code ) {
283 $this->loadGeneralMessages();
284 $this->loadMessages( $code );
285 return array_diff_key( $this->mGeneralMessages['required'], $this->mMessages[$code]['required'] );
289 * Get the duplicate messages for a specific language.
291 * @param $code The language code.
293 * @return The duplicate messages for this language.
295 public function getDuplicateMessages( $code ) {
296 $this->loadGeneralMessages();
297 $this->loadMessages( $code );
298 $duplicateMessages = array();
299 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
300 if ( $this->mGeneralMessages['translatable'][$key] == $value ) {
301 $duplicateMessages[$key] = $value;
304 return $duplicateMessages;
308 * Get the obsolete messages for a specific language.
310 * @param $code The language code.
312 * @return The obsolete messages for this language.
314 public function getObsoleteMessages( $code ) {
315 $this->loadGeneralMessages();
316 $this->loadMessages( $code );
317 return $this->mMessages[$code]['obsolete'];
321 * Get the messages whose variables do not match the original ones.
323 * @param $code The language code.
325 * @return The messages whose variables do not match the original ones.
327 public function getMessagesWithMismatchVariables( $code ) {
328 $this->loadGeneralMessages();
329 $this->loadMessages( $code );
330 $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' );
331 $mismatchMessages = array();
332 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
334 foreach ( $variables as $var ) {
335 if ( preg_match( "/$var/sU", $this->mGeneralMessages['translatable'][$key] ) &&
336 !preg_match( "/$var/sU", $value ) ) {
339 if ( !preg_match( "/$var/sU", $this->mGeneralMessages['translatable'][$key] ) &&
340 preg_match( "/$var/sU", $value ) ) {
345 $mismatchMessages[$key] = $value;
348 return $mismatchMessages;
352 * Get the messages which do not use plural.
354 * @param $code The language code.
356 * @return The messages which do not use plural in this language.
358 public function getMessagesWithoutPlural( $code ) {
359 $this->loadGeneralMessages();
360 $this->loadMessages( $code );
361 $messagesWithoutPlural = array();
362 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
363 if ( stripos( $this->mGeneralMessages['translatable'][$key], '{{plural:' ) !== false && stripos( $value, '{{plural:' ) === false ) {
364 $messagesWithoutPlural[$key] = $value;
367 return $messagesWithoutPlural;
371 * Get the empty messages.
373 * @param $code The language code.
375 * @return The empty messages for this language.
377 public function getEmptyMessages( $code ) {
378 $this->loadGeneralMessages();
379 $this->loadMessages( $code );
380 $emptyMessages = array();
381 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
382 if ( $value === '' || $value === '-' ) {
383 $emptyMessages[$key] = $value;
386 return $emptyMessages;
390 * Get the messages with trailing whitespace.
392 * @param $code The language code.
394 * @return The messages with trailing whitespace in this language.
396 public function getMessagesWithWhitespace( $code ) {
397 $this->loadGeneralMessages();
398 $this->loadMessages( $code );
399 $messagesWithWhitespace = array();
400 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
401 if ( $this->mGeneralMessages['translatable'][$key] !== '' && $value !== rtrim( $value ) ) {
402 $messagesWithWhitespace[$key] = $value;
405 return $messagesWithWhitespace;
409 * Get the non-XHTML messages.
411 * @param $code The language code.
413 * @return The non-XHTML messages for this language.
415 public function getNonXHTMLMessages( $code ) {
416 $this->loadGeneralMessages();
417 $this->loadMessages( $code );
418 $wrongPhrases = array(
426 $wrongPhrases = '~(' . implode( '|', $wrongPhrases ) . ')~sDu';
427 $nonXHTMLMessages = array();
428 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
429 if ( preg_match( $wrongPhrases, $value ) ) {
430 $nonXHTMLMessages[$key] = $value;
433 return $nonXHTMLMessages;
437 * Get the messages which include wrong characters.
439 * @param $code The language code.
441 * @return The messages which include wrong characters in this language.
443 public function getMessagesWithWrongChars( $code ) {
444 $this->loadGeneralMessages();
445 $this->loadMessages( $code );
447 '[LRM]' => "\xE2\x80\x8E",
448 '[RLM]' => "\xE2\x80\x8F",
449 '[LRE]' => "\xE2\x80\xAA",
450 '[RLE]' => "\xE2\x80\xAB",
451 '[POP]' => "\xE2\x80\xAC",
452 '[LRO]' => "\xE2\x80\xAD",
453 '[RLO]' => "\xE2\x80\xAB",
454 '[ZWSP]'=> "\xE2\x80\x8B",
455 '[NBSP]'=> "\xC2\xA0",
456 '[WJ]' => "\xE2\x81\xA0",
457 '[BOM]' => "\xEF\xBB\xBF",
458 '[FFFD]'=> "\xEF\xBF\xBD",
460 $wrongRegExp = '/(' . implode( '|', array_values( $wrongChars ) ) . ')/sDu';
461 $wrongCharsMessages = array();
462 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
463 if ( preg_match( $wrongRegExp, $value ) ) {
464 foreach ( $wrongChars as $viewableChar => $hiddenChar ) {
465 $value = str_replace( $hiddenChar, $viewableChar, $value );
467 $wrongCharsMessages[$key] = $value;
470 return $wrongCharsMessages;
474 * Get the messages which include dubious links.
476 * @param $code The language code.
478 * @return The messages which include dubious links in this language.
480 public function getMessagesWithDubiousLinks( $code ) {
481 $this->loadGeneralMessages();
482 $this->loadMessages( $code );
483 $tc = Title::legalChars() . '#%{}';
485 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
487 preg_match_all( "/\[\[([{$tc}]+)(?:\\|(.+?))?]]/sDu", $value, $matches );
488 for ($i = 0; $i < count($matches[0]); $i++ ) {
489 if ( preg_match( "/.*project.*/isDu", $matches[1][$i] ) ) {
490 $messages[$key][] = $matches[0][$i];
495 if ( isset( $messages[$key] ) ) {
496 $messages[$key] = implode( $messages[$key],", " );
503 * Get the messages which include unbalanced brackets.
505 * @param $code The language code.
507 * @return The messages which include unbalanced brackets in this language.
509 public function getMessagesWithUnbalanced( $code ) {
510 $this->loadGeneralMessages();
511 $this->loadMessages( $code );
513 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
514 $a = $b = $c = $d = 0;
515 foreach ( preg_split( '//', $value ) as $char ) {
532 if ( $a !== $b || $c !== $d ) {
533 $messages[$key] = "$a, $b, $c, $d";
541 * Get the untranslated namespace names.
543 * @param $code The language code.
545 * @return The untranslated namespace names in this language.
547 public function getUntranslatedNamespaces( $code ) {
548 $this->loadFile( 'en' );
549 $this->loadFile( $code );
550 return array_flip( array_diff_key( $this->mNamespaceNames['en'], $this->mNamespaceNames[$code] ) );
554 * Get the project talk namespace names with no $1.
556 * @param $code The language code.
558 * @return The problematic project talk namespaces in this language.
560 public function getProblematicProjectTalks( $code ) {
561 $this->loadFile( $code );
562 $namespaces = array();
564 # Check default namespace name
565 if( isset( $this->mNamespaceNames[$code][NS_PROJECT_TALK] ) ) {
566 $default = $this->mNamespaceNames[$code][NS_PROJECT_TALK];
567 if ( strpos( $default, '$1' ) === FALSE ) {
568 $namespaces[$default] = 'default';
572 # Check namespace aliases
573 foreach( $this->mNamespaceAliases[$code] as $key => $value ) {
574 if ( $value == NS_PROJECT_TALK && strpos( $key, '$1' ) === FALSE ) {
575 $namespaces[$key] = '';
583 * Get the untranslated magic words.
585 * @param $code The language code.
587 * @return The untranslated magic words in this language.
589 public function getUntranslatedMagicWords( $code ) {
590 $this->loadFile( 'en' );
591 $this->loadFile( $code );
592 $magicWords = array();
593 foreach ( $this->mMagicWords['en'] as $key => $value ) {
594 if ( !isset( $this->mMagicWords[$code][$key] ) ) {
595 $magicWords[$key] = $value[1];
602 * Get the obsolete magic words.
604 * @param $code The language code.
606 * @return The obsolete magic words in this language.
608 public function getObsoleteMagicWords( $code ) {
609 $this->loadFile( 'en' );
610 $this->loadFile( $code );
611 $magicWords = array();
612 foreach ( $this->mMagicWords[$code] as $key => $value ) {
613 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
614 $magicWords[$key] = $value[1];
621 * Get the magic words that override the original English magic word.
623 * @param $code The language code.
625 * @return The overriding magic words in this language.
627 public function getOverridingMagicWords( $code ) {
628 $this->loadFile( 'en' );
629 $this->loadFile( $code );
630 $magicWords = array();
631 foreach ( $this->mMagicWords[$code] as $key => $local ) {
632 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
633 # Unrecognized magic word
636 $en = $this->mMagicWords['en'][$key];
637 array_shift( $local );
639 foreach ( $en as $word ) {
640 if ( !in_array( $word, $local ) ) {
641 $magicWords[$key] = $word;
650 * Get the magic words which do not match the case-sensitivity of the original words.
652 * @param $code The language code.
654 * @return The magic words whose case does not match in this language.
656 public function getCaseMismatchMagicWords( $code ) {
657 $this->loadFile( 'en' );
658 $this->loadFile( $code );
659 $magicWords = array();
660 foreach ( $this->mMagicWords[$code] as $key => $local ) {
661 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
662 # Unrecognized magic word
665 if ( $local[0] != $this->mMagicWords['en'][$key][0] ) {
666 $magicWords[$key] = $local[0];
673 * Get the untranslated special page names.
675 * @param $code The language code.
677 * @return The untranslated special page names in this language.
679 public function getUntraslatedSpecialPages( $code ) {
680 $this->loadFile( 'en' );
681 $this->loadFile( $code );
682 $specialPageAliases = array();
683 foreach ( $this->mSpecialPageAliases['en'] as $key => $value ) {
684 if ( !isset( $this->mSpecialPageAliases[$code][$key] ) ) {
685 $specialPageAliases[$key] = $value[0];
688 return $specialPageAliases;
692 * Get the obsolete special page names.
694 * @param $code The language code.
696 * @return The obsolete special page names in this language.
698 public function getObsoleteSpecialPages( $code ) {
699 $this->loadFile( 'en' );
700 $this->loadFile( $code );
701 $specialPageAliases = array();
702 foreach ( $this->mSpecialPageAliases[$code] as $key => $value ) {
703 if ( !isset( $this->mSpecialPageAliases['en'][$key] ) ) {
704 $specialPageAliases[$key] = $value[0];
707 return $specialPageAliases;
711 class extensionLanguages extends languages {
712 private $mMessageGroup; # The message group
715 * Load the messages group.
716 * @param $group The messages group.
718 function __construct( MessageGroup $group ) {
719 $this->mMessageGroup = $group;
721 $bools = $this->mMessageGroup->getBools();
722 $this->mIgnoredMessages = $bools['ignored'];
723 $this->mOptionalMessages = $bools['optional'];
727 * Get the extension name.
729 * @return The extension name.
731 public function name() {
732 return $this->mMessageGroup->getLabel();
736 * Load the language file.
738 * @param $code The language code.
740 protected function loadFile( $code ) {
741 if( !isset( $this->mRawMessages[$code] ) ) {
742 $this->mRawMessages[$code] = $this->mMessageGroup->load( $code );
743 if( empty( $this->mRawMessages[$code] ) ) {
744 $this->mRawMessages[$code] = array();