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' );
36 global $wgIgnoredMessages, $wgOptionalMessages, $wgEXIFMessages;
38 $this->mIgnoredMessages = $wgIgnoredMessages;
40 $this->mOptionalMessages = array_merge( $wgOptionalMessages );
42 $this->mOptionalMessages = array_merge( $wgOptionalMessages, $wgEXIFMessages );
45 $this->mLanguages = array_keys( Language::getLanguageNames( true ) );
46 sort( $this->mLanguages );
50 * Get the language list.
52 * @return The language list.
54 public function getLanguages() {
55 return $this->mLanguages;
59 * Get the ignored messages list.
61 * @return The ignored messages list.
63 public function getIgnoredMessages() {
64 return $this->mIgnoredMessages;
68 * Get the optional messages list.
70 * @return The optional messages list.
72 public function getOptionalMessages() {
73 return $this->mOptionalMessages;
77 * Load the language file.
79 * @param $code The language code.
81 protected function loadFile( $code ) {
82 if ( isset( $this->mRawMessages[$code] ) &&
83 isset( $this->mFallback[$code] ) &&
84 isset( $this->mNamespaceNames[$code] ) &&
85 isset( $this->mNamespaceAliases[$code] ) &&
86 isset( $this->mMagicWords[$code] ) &&
87 isset( $this->mSpecialPageAliases[$code] ) ) {
90 $this->mRawMessages[$code] = array();
91 $this->mFallback[$code] = '';
92 $this->mNamespaceNames[$code] = array();
93 $this->mNamespaceAliases[$code] = array();
94 $this->mMagicWords[$code] = array();
95 $this->mSpecialPageAliases[$code] = array();
96 $filename = Language::getMessagesFileName( $code );
97 if ( file_exists( $filename ) ) {
99 if ( isset( $messages ) ) {
100 $this->mRawMessages[$code] = $messages;
102 if ( isset( $fallback ) ) {
103 $this->mFallback[$code] = $fallback;
105 if ( isset( $namespaceNames ) ) {
106 $this->mNamespaceNames[$code] = $namespaceNames;
108 if ( isset( $namespaceAliases ) ) {
109 $this->mNamespaceAliases[$code] = $namespaceAliases;
111 if ( isset( $magicWords ) ) {
112 $this->mMagicWords[$code] = $magicWords;
114 if ( isset( $specialPageAliases ) ) {
115 $this->mSpecialPageAliases[$code] = $specialPageAliases;
121 * Load the messages for a specific language (which is not English) and divide them to groups:
122 * all - all the messages.
123 * required - messages which should be translated in order to get a complete translation.
124 * optional - messages which can be translated, the fallback translation is used if not translated.
125 * obsolete - messages which should not be translated, either because they do not exist, or they are ignored messages.
126 * translated - messages which are either required or optional, but translated from English and needed.
128 * @param $code The language code.
130 private function loadMessages( $code ) {
131 if ( isset( $this->mMessages[$code] ) ) {
134 $this->loadFile( $code );
135 $this->loadGeneralMessages();
136 $this->mMessages[$code]['all'] = $this->mRawMessages[$code];
137 $this->mMessages[$code]['required'] = array();
138 $this->mMessages[$code]['optional'] = array();
139 $this->mMessages[$code]['obsolete'] = array();
140 $this->mMessages[$code]['translated'] = array();
141 foreach ( $this->mMessages[$code]['all'] as $key => $value ) {
142 if ( isset( $this->mGeneralMessages['required'][$key] ) ) {
143 $this->mMessages[$code]['required'][$key] = $value;
144 $this->mMessages[$code]['translated'][$key] = $value;
145 } else if ( isset( $this->mGeneralMessages['optional'][$key] ) ) {
146 $this->mMessages[$code]['optional'][$key] = $value;
147 $this->mMessages[$code]['translated'][$key] = $value;
149 $this->mMessages[$code]['obsolete'][$key] = $value;
155 * Load the messages for English and divide them to groups:
156 * all - all the messages.
157 * required - messages which should be translated to other languages in order to get a complete translation.
158 * optional - messages which can be translated to other languages, but it's not required for a complete translation.
159 * ignored - messages which should not be translated to other languages.
160 * translatable - messages which are either required or optional, but can be translated from English.
162 private function loadGeneralMessages() {
163 if ( isset( $this->mGeneralMessages ) ) {
166 $this->loadFile( 'en' );
167 $this->mGeneralMessages['all'] = $this->mRawMessages['en'];
168 $this->mGeneralMessages['required'] = array();
169 $this->mGeneralMessages['optional'] = array();
170 $this->mGeneralMessages['ignored'] = array();
171 $this->mGeneralMessages['translatable'] = array();
172 foreach ( $this->mGeneralMessages['all'] as $key => $value ) {
173 if ( in_array( $key, $this->mIgnoredMessages ) ) {
174 $this->mGeneralMessages['ignored'][$key] = $value;
175 } else if ( in_array( $key, $this->mOptionalMessages ) ) {
176 $this->mGeneralMessages['optional'][$key] = $value;
177 $this->mGeneralMessages['translatable'][$key] = $value;
179 $this->mGeneralMessages['required'][$key] = $value;
180 $this->mGeneralMessages['translatable'][$key] = $value;
186 * Get all the messages for a specific language (not English), without the
187 * fallback language messages, divided to groups:
188 * all - all the messages.
189 * required - messages which should be translated in order to get a complete translation.
190 * optional - messages which can be translated, the fallback translation is used if not translated.
191 * obsolete - messages which should not be translated, either because they do not exist, or they are ignored messages.
192 * translated - messages which are either required or optional, but translated from English and needed.
194 * @param $code The language code.
196 * @return The messages in this language.
198 public function getMessages( $code ) {
199 $this->loadMessages( $code );
200 return $this->mMessages[$code];
204 * Get all the general English messages, divided to groups:
205 * all - all the messages.
206 * required - messages which should be translated to other languages in order to get a complete translation.
207 * optional - messages which can be translated to other languages, but it's not required for a complete translation.
208 * ignored - messages which should not be translated to other languages.
209 * translatable - messages which are either required or optional, but can be translated from English.
211 * @return The general English messages.
213 public function getGeneralMessages() {
214 $this->loadGeneralMessages();
215 return $this->mGeneralMessages;
219 * Get fallback language code for a specific language.
221 * @param $code The language code.
223 * @return Fallback code.
225 public function getFallback( $code ) {
226 $this->loadFile( $code );
227 return $this->mFallback[$code];
231 * Get namespace names for a specific language.
233 * @param $code The language code.
235 * @return Namespace names.
237 public function getNamespaceNames( $code ) {
238 $this->loadFile( $code );
239 return $this->mNamespaceNames[$code];
243 * Get namespace aliases for a specific language.
245 * @param $code The language code.
247 * @return Namespace aliases.
249 public function getNamespaceAliases( $code ) {
250 $this->loadFile( $code );
251 return $this->mNamespaceAliases[$code];
255 * Get magic words for a specific language.
257 * @param $code The language code.
259 * @return Magic words.
261 public function getMagicWords( $code ) {
262 $this->loadFile( $code );
263 return $this->mMagicWords[$code];
267 * Get special page aliases for a specific language.
269 * @param $code The language code.
271 * @return Special page aliases.
273 public function getSpecialPageAliases( $code ) {
274 $this->loadFile( $code );
275 return $this->mSpecialPageAliases[$code];
279 * Get the untranslated messages for a specific language.
281 * @param $code The language code.
283 * @return The untranslated messages for this language.
285 public function getUntranslatedMessages( $code ) {
286 $this->loadGeneralMessages();
287 $this->loadMessages( $code );
288 return array_diff_key( $this->mGeneralMessages['required'], $this->mMessages[$code]['required'] );
292 * Get the duplicate messages for a specific language.
294 * @param $code The language code.
296 * @return The duplicate messages for this language.
298 public function getDuplicateMessages( $code ) {
299 $this->loadGeneralMessages();
300 $this->loadMessages( $code );
301 $duplicateMessages = array();
302 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
303 if ( $this->mGeneralMessages['translatable'][$key] == $value ) {
304 $duplicateMessages[$key] = $value;
307 return $duplicateMessages;
311 * Get the obsolete messages for a specific language.
313 * @param $code The language code.
315 * @return The obsolete messages for this language.
317 public function getObsoleteMessages( $code ) {
318 $this->loadGeneralMessages();
319 $this->loadMessages( $code );
320 return $this->mMessages[$code]['obsolete'];
324 * Get the messages whose variables do not match the original ones.
326 * @param $code The language code.
328 * @return The messages whose variables do not match the original ones.
330 public function getMessagesWithMismatchVariables( $code ) {
331 $this->loadGeneralMessages();
332 $this->loadMessages( $code );
333 $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' );
334 $mismatchMessages = array();
335 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
337 foreach ( $variables as $var ) {
338 if ( preg_match( "/$var/sU", $this->mGeneralMessages['translatable'][$key] ) &&
339 !preg_match( "/$var/sU", $value ) ) {
342 if ( !preg_match( "/$var/sU", $this->mGeneralMessages['translatable'][$key] ) &&
343 preg_match( "/$var/sU", $value ) ) {
348 $mismatchMessages[$key] = $value;
351 return $mismatchMessages;
355 * Get the messages which do not use plural.
357 * @param $code The language code.
359 * @return The messages which do not use plural in this language.
361 public function getMessagesWithoutPlural( $code ) {
362 $this->loadGeneralMessages();
363 $this->loadMessages( $code );
364 $messagesWithoutPlural = array();
365 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
366 if ( stripos( $this->mGeneralMessages['translatable'][$key], '{{plural:' ) !== false && stripos( $value, '{{plural:' ) === false ) {
367 $messagesWithoutPlural[$key] = $value;
370 return $messagesWithoutPlural;
374 * Get the empty messages.
376 * @param $code The language code.
378 * @return The empty messages for this language.
380 public function getEmptyMessages( $code ) {
381 $this->loadGeneralMessages();
382 $this->loadMessages( $code );
383 $emptyMessages = array();
384 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
385 if ( $value === '' || $value === '-' ) {
386 $emptyMessages[$key] = $value;
389 return $emptyMessages;
393 * Get the messages with trailing whitespace.
395 * @param $code The language code.
397 * @return The messages with trailing whitespace in this language.
399 public function getMessagesWithWhitespace( $code ) {
400 $this->loadGeneralMessages();
401 $this->loadMessages( $code );
402 $messagesWithWhitespace = array();
403 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
404 if ( $this->mGeneralMessages['translatable'][$key] !== '' && $value !== rtrim( $value ) ) {
405 $messagesWithWhitespace[$key] = $value;
408 return $messagesWithWhitespace;
412 * Get the non-XHTML messages.
414 * @param $code The language code.
416 * @return The non-XHTML messages for this language.
418 public function getNonXHTMLMessages( $code ) {
419 $this->loadGeneralMessages();
420 $this->loadMessages( $code );
421 $wrongPhrases = array(
429 $wrongPhrases = '~(' . implode( '|', $wrongPhrases ) . ')~sDu';
430 $nonXHTMLMessages = array();
431 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
432 if ( preg_match( $wrongPhrases, $value ) ) {
433 $nonXHTMLMessages[$key] = $value;
436 return $nonXHTMLMessages;
440 * Get the messages which include wrong characters.
442 * @param $code The language code.
444 * @return The messages which include wrong characters in this language.
446 public function getMessagesWithWrongChars( $code ) {
447 $this->loadGeneralMessages();
448 $this->loadMessages( $code );
450 '[LRM]' => "\xE2\x80\x8E",
451 '[RLM]' => "\xE2\x80\x8F",
452 '[LRE]' => "\xE2\x80\xAA",
453 '[RLE]' => "\xE2\x80\xAB",
454 '[POP]' => "\xE2\x80\xAC",
455 '[LRO]' => "\xE2\x80\xAD",
456 '[RLO]' => "\xE2\x80\xAB",
457 '[ZWSP]'=> "\xE2\x80\x8B",
458 '[NBSP]'=> "\xC2\xA0",
459 '[WJ]' => "\xE2\x81\xA0",
460 '[BOM]' => "\xEF\xBB\xBF",
461 '[FFFD]'=> "\xEF\xBF\xBD",
463 $wrongRegExp = '/(' . implode( '|', array_values( $wrongChars ) ) . ')/sDu';
464 $wrongCharsMessages = array();
465 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
466 if ( preg_match( $wrongRegExp, $value ) ) {
467 foreach ( $wrongChars as $viewableChar => $hiddenChar ) {
468 $value = str_replace( $hiddenChar, $viewableChar, $value );
470 $wrongCharsMessages[$key] = $value;
473 return $wrongCharsMessages;
477 * Get the messages which include dubious links.
479 * @param $code The language code.
481 * @return The messages which include dubious links in this language.
483 public function getMessagesWithDubiousLinks( $code ) {
484 $this->loadGeneralMessages();
485 $this->loadMessages( $code );
486 $tc = Title::legalChars() . '#%{}';
488 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
490 preg_match_all( "/\[\[([{$tc}]+)(?:\\|(.+?))?]]/sDu", $value, $matches );
491 for ($i = 0; $i < count($matches[0]); $i++ ) {
492 if ( preg_match( "/.*project.*/isDu", $matches[1][$i] ) ) {
493 $messages[$key][] = $matches[0][$i];
498 if ( isset( $messages[$key] ) ) {
499 $messages[$key] = implode( $messages[$key],", " );
506 * Get the messages which include unbalanced brackets.
508 * @param $code The language code.
510 * @return The messages which include unbalanced brackets in this language.
512 public function getMessagesWithUnbalanced( $code ) {
513 $this->loadGeneralMessages();
514 $this->loadMessages( $code );
516 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
517 $a = $b = $c = $d = 0;
518 foreach ( preg_split( '//', $value ) as $char ) {
535 if ( $a !== $b || $c !== $d ) {
536 $messages[$key] = "$a, $b, $c, $d";
544 * Get the untranslated namespace names.
546 * @param $code The language code.
548 * @return The untranslated namespace names in this language.
550 public function getUntranslatedNamespaces( $code ) {
551 $this->loadFile( 'en' );
552 $this->loadFile( $code );
553 return array_flip( array_diff_key( $this->mNamespaceNames['en'], $this->mNamespaceNames[$code] ) );
557 * Get the project talk namespace names with no $1.
559 * @param $code The language code.
561 * @return The problematic project talk namespaces in this language.
563 public function getProblematicProjectTalks( $code ) {
564 $this->loadFile( $code );
565 $namespaces = array();
567 # Check default namespace name
568 if( isset( $this->mNamespaceNames[$code][NS_PROJECT_TALK] ) ) {
569 $default = $this->mNamespaceNames[$code][NS_PROJECT_TALK];
570 if ( strpos( $default, '$1' ) === FALSE ) {
571 $namespaces[$default] = 'default';
575 # Check namespace aliases
576 foreach( $this->mNamespaceAliases[$code] as $key => $value ) {
577 if ( $value == NS_PROJECT_TALK && strpos( $key, '$1' ) === FALSE ) {
578 $namespaces[$key] = '';
586 * Get the untranslated magic words.
588 * @param $code The language code.
590 * @return The untranslated magic words in this language.
592 public function getUntranslatedMagicWords( $code ) {
593 $this->loadFile( 'en' );
594 $this->loadFile( $code );
595 $magicWords = array();
596 foreach ( $this->mMagicWords['en'] as $key => $value ) {
597 if ( !isset( $this->mMagicWords[$code][$key] ) ) {
598 $magicWords[$key] = $value[1];
605 * Get the obsolete magic words.
607 * @param $code The language code.
609 * @return The obsolete magic words in this language.
611 public function getObsoleteMagicWords( $code ) {
612 $this->loadFile( 'en' );
613 $this->loadFile( $code );
614 $magicWords = array();
615 foreach ( $this->mMagicWords[$code] as $key => $value ) {
616 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
617 $magicWords[$key] = $value[1];
624 * Get the magic words that override the original English magic word.
626 * @param $code The language code.
628 * @return The overriding magic words in this language.
630 public function getOverridingMagicWords( $code ) {
631 $this->loadFile( 'en' );
632 $this->loadFile( $code );
633 $magicWords = array();
634 foreach ( $this->mMagicWords[$code] as $key => $local ) {
635 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
636 # Unrecognized magic word
639 $en = $this->mMagicWords['en'][$key];
640 array_shift( $local );
642 foreach ( $en as $word ) {
643 if ( !in_array( $word, $local ) ) {
644 $magicWords[$key] = $word;
653 * Get the magic words which do not match the case-sensitivity of the original words.
655 * @param $code The language code.
657 * @return The magic words whose case does not match in this language.
659 public function getCaseMismatchMagicWords( $code ) {
660 $this->loadFile( 'en' );
661 $this->loadFile( $code );
662 $magicWords = array();
663 foreach ( $this->mMagicWords[$code] as $key => $local ) {
664 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
665 # Unrecognized magic word
668 if ( $local[0] != $this->mMagicWords['en'][$key][0] ) {
669 $magicWords[$key] = $local[0];
676 * Get the untranslated special page names.
678 * @param $code The language code.
680 * @return The untranslated special page names in this language.
682 public function getUntraslatedSpecialPages( $code ) {
683 $this->loadFile( 'en' );
684 $this->loadFile( $code );
685 $specialPageAliases = array();
686 foreach ( $this->mSpecialPageAliases['en'] as $key => $value ) {
687 if ( !isset( $this->mSpecialPageAliases[$code][$key] ) ) {
688 $specialPageAliases[$key] = $value[0];
691 return $specialPageAliases;
695 * Get the obsolete special page names.
697 * @param $code The language code.
699 * @return The obsolete special page names in this language.
701 public function getObsoleteSpecialPages( $code ) {
702 $this->loadFile( 'en' );
703 $this->loadFile( $code );
704 $specialPageAliases = array();
705 foreach ( $this->mSpecialPageAliases[$code] as $key => $value ) {
706 if ( !isset( $this->mSpecialPageAliases['en'][$key] ) ) {
707 $specialPageAliases[$key] = $value[0];
710 return $specialPageAliases;
714 class extensionLanguages extends languages {
715 private $mMessageGroup; # The message group
718 * Load the messages group.
719 * @param $group The messages group.
721 function __construct( MessageGroup $group ) {
722 $this->mMessageGroup = $group;
724 $bools = $this->mMessageGroup->getBools();
725 $this->mIgnoredMessages = $bools['ignored'];
726 $this->mOptionalMessages = $bools['optional'];
730 * Get the extension name.
732 * @return The extension name.
734 public function name() {
735 return $this->mMessageGroup->getLabel();
739 * Load the language file.
741 * @param $code The language code.
743 protected function loadFile( $code ) {
744 if( !isset( $this->mRawMessages[$code] ) ) {
745 $this->mRawMessages[$code] = $this->mMessageGroup->load( $code );
746 if( empty( $this->mRawMessages[$code] ) ) {
747 $this->mRawMessages[$code] = array();