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 $mGeneralMessages; # General messages in English, divided to groups
18 protected $mIgnoredMessages; # All the messages which should be exist only in the English file
19 protected $mOptionalMessages; # All the messages which may be translated or not, depending on the language
21 protected $mNamespaceNames; # Namespace names
22 protected $mNamespaceAliases; # Namespace aliases
23 protected $mSkinNames; # Skin names
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->mNamespaceNames[$code] ) &&
81 isset( $this->mNamespaceAliases[$code] ) &&
82 isset( $this->mSkinNames[$code] ) &&
83 isset( $this->mMagicWords[$code] ) &&
84 isset( $this->mSpecialPageAliases[$code] ) ) {
87 $this->mRawMessages[$code] = array();
88 $this->mNamespaceNames[$code] = array();
89 $this->mNamespaceAliases[$code] = array();
90 $this->mSkinNames[$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( $namespaceNames ) ) {
100 $this->mNamespaceNames[$code] = $namespaceNames;
102 if ( isset( $namespaceAliases ) ) {
103 $this->mNamespaceAliases[$code] = $namespaceAliases;
105 if ( isset( $skinNames ) ) {
106 $this->mSkinNames[$code] = $skinNames;
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 namespace names for a specific language.
218 * @param $code The language code.
220 * @return Namespace names.
222 public function getNamespaceNames( $code ) {
223 $this->loadFile( $code );
224 return $this->mNamespaceNames[$code];
228 * Get namespace aliases for a specific language.
230 * @param $code The language code.
232 * @return Namespace aliases.
234 public function getNamespaceAliases( $code ) {
235 $this->loadFile( $code );
236 return $this->mNamespaceAliases[$code];
240 * Get skin names for a specific language.
242 * @param $code The language code.
244 * @return Skin names.
246 public function getSkinNames( $code ) {
247 $this->loadFile( $code );
248 return $this->mSkinNames[$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 which do not use some variables.
323 * @param $code The language code.
325 * @return The messages which do not use some variables in this language.
327 public function getMessagesWithoutVariables( $code ) {
328 $this->loadGeneralMessages();
329 $this->loadMessages( $code );
330 $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' );
331 $messagesWithoutVariables = 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 ) ) {
341 $messagesWithoutVariables[$key] = $value;
344 return $messagesWithoutVariables;
348 * Get the messages which do not use plural.
350 * @param $code The language code.
352 * @return The messages which do not use plural in this language.
354 public function getMessagesWithoutPlural( $code ) {
355 $this->loadGeneralMessages();
356 $this->loadMessages( $code );
357 $messagesWithoutPlural = array();
358 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
359 if ( stripos( $this->mGeneralMessages['translatable'][$key], '{{plural:' ) !== false && stripos( $value, '{{plural:' ) === false ) {
360 $messagesWithoutPlural[$key] = $value;
363 return $messagesWithoutPlural;
367 * Get the empty messages.
369 * @param $code The language code.
371 * @return The empty messages for this language.
373 public function getEmptyMessages( $code ) {
374 $this->loadGeneralMessages();
375 $this->loadMessages( $code );
376 $emptyMessages = array();
377 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
378 if ( $value === '' || $value === '-' ) {
379 $emptyMessages[$key] = $value;
382 return $emptyMessages;
386 * Get the messages with trailing whitespace.
388 * @param $code The language code.
390 * @return The messages with trailing whitespace in this language.
392 public function getMessagesWithWhitespace( $code ) {
393 $this->loadGeneralMessages();
394 $this->loadMessages( $code );
395 $messagesWithWhitespace = array();
396 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
397 if ( $this->mGeneralMessages['translatable'][$key] !== '' && $value !== rtrim( $value ) ) {
398 $messagesWithWhitespace[$key] = $value;
401 return $messagesWithWhitespace;
405 * Get the non-XHTML messages.
407 * @param $code The language code.
409 * @return The non-XHTML messages for this language.
411 public function getNonXHTMLMessages( $code ) {
412 $this->loadGeneralMessages();
413 $this->loadMessages( $code );
414 $wrongPhrases = array(
422 $wrongPhrases = '~(' . implode( '|', $wrongPhrases ) . ')~sDu';
423 $nonXHTMLMessages = array();
424 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
425 if ( preg_match( $wrongPhrases, $value ) ) {
426 $nonXHTMLMessages[$key] = $value;
429 return $nonXHTMLMessages;
433 * Get the messages which include wrong characters.
435 * @param $code The language code.
437 * @return The messages which include wrong characters in this language.
439 public function getMessagesWithWrongChars( $code ) {
440 $this->loadGeneralMessages();
441 $this->loadMessages( $code );
443 '[LRM]' => "\xE2\x80\x8E",
444 '[RLM]' => "\xE2\x80\x8F",
445 '[LRE]' => "\xE2\x80\xAA",
446 '[RLE]' => "\xE2\x80\xAB",
447 '[POP]' => "\xE2\x80\xAC",
448 '[LRO]' => "\xE2\x80\xAD",
449 '[RLO]' => "\xE2\x80\xAB",
450 '[ZWSP]'=> "\xE2\x80\x8B",
451 '[NBSP]'=> "\xC2\xA0",
452 '[WJ]' => "\xE2\x81\xA0",
453 '[BOM]' => "\xEF\xBB\xBF",
454 '[FFFD]'=> "\xEF\xBF\xBD",
456 $wrongRegExp = '/(' . implode( '|', array_values( $wrongChars ) ) . ')/sDu';
457 $wrongCharsMessages = array();
458 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
459 if ( preg_match( $wrongRegExp, $value ) ) {
460 foreach ( $wrongChars as $viewableChar => $hiddenChar ) {
461 $value = str_replace( $hiddenChar, $viewableChar, $value );
463 $wrongCharsMessages[$key] = $value;
466 return $wrongCharsMessages;
470 * Get the messages which include dubious links.
472 * @param $code The language code.
474 * @return The messages which include dubious links in this language.
476 public function getMessagesWithDubiousLinks( $code ) {
477 $this->loadGeneralMessages();
478 $this->loadMessages( $code );
479 $tc = Title::legalChars() . '#%{}';
481 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
483 preg_match_all( "/\[\[([{$tc}]+)(?:\\|(.+?))?]]/sDu", $value, $matches );
484 for ($i = 0; $i < count($matches[0]); $i++ ) {
485 if ( preg_match( "/.*project.*/isDu", $matches[1][$i] ) ) {
486 $messages[$key][] = $matches[0][$i];
491 if ( isset( $messages[$key] ) ) {
492 $messages[$key] = implode( $messages[$key],", " );
499 * Get the messages which include unbalanced brackets.
501 * @param $code The language code.
503 * @return The messages which include unbalanced brackets in this language.
505 public function getMessagesWithUnbalanced( $code ) {
506 $this->loadGeneralMessages();
507 $this->loadMessages( $code );
509 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
510 $a = $b = $c = $d = 0;
511 foreach ( preg_split( '//', $value ) as $char ) {
528 if ( $a !== $b || $c !== $d ) {
529 $messages[$key] = "$a, $b, $c, $d";
537 * Get the untranslated namespace names.
539 * @param $code The language code.
541 * @return The untranslated namespace names in this language.
543 public function getUntranslatedNamespaces( $code ) {
544 $this->loadFile( 'en' );
545 $this->loadFile( $code );
546 return array_flip( array_diff_key( $this->mNamespaceNames['en'], $this->mNamespaceNames[$code] ) );
550 * Get the project talk namespace names with no $1.
552 * @param $code The language code.
554 * @return The problematic project talk namespaces in this language.
556 public function getProblematicProjectTalks( $code ) {
557 $this->loadFile( $code );
558 $namespaces = array();
560 # Check default namespace name
561 if( isset( $this->mNamespaceNames[$code][NS_PROJECT_TALK] ) ) {
562 $default = $this->mNamespaceNames[$code][NS_PROJECT_TALK];
563 if ( strpos( $default, '$1' ) === FALSE ) {
564 $namespaces[$default] = 'default';
568 # Check namespace aliases
569 foreach( $this->mNamespaceAliases[$code] as $key => $value ) {
570 if ( $value == NS_PROJECT_TALK && strpos( $key, '$1' ) === FALSE ) {
571 $namespaces[$key] = '';
579 * Get the untranslated skin names.
581 * @param $code The language code.
583 * @return The untranslated skin names in this language.
585 public function getUntranslatedSkins( $code ) {
586 $this->loadFile( 'en' );
587 $this->loadFile( $code );
588 return array_diff_key( $this->mSkinNames['en'], $this->mSkinNames[$code] );
592 * Get the untranslated magic words.
594 * @param $code The language code.
596 * @return The untranslated magic words in this language.
598 public function getUntranslatedMagicWords( $code ) {
599 $this->loadFile( 'en' );
600 $this->loadFile( $code );
601 $magicWords = array();
602 foreach ( $this->mMagicWords['en'] as $key => $value ) {
603 if ( !isset( $this->mMagicWords[$code][$key] ) ) {
604 $magicWords[$key] = $value[1];
611 * Get the obsolete magic words.
613 * @param $code The language code.
615 * @return The obsolete magic words in this language.
617 public function getObsoleteMagicWords( $code ) {
618 $this->loadFile( 'en' );
619 $this->loadFile( $code );
620 $magicWords = array();
621 foreach ( $this->mMagicWords[$code] as $key => $value ) {
622 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
623 $magicWords[$key] = $value[1];
630 * Get the magic words that override the original English magic word.
632 * @param $code The language code.
634 * @return The overriding magic words in this language.
636 public function getOverridingMagicWords( $code ) {
637 $this->loadFile( 'en' );
638 $this->loadFile( $code );
639 $magicWords = array();
640 foreach ( $this->mMagicWords[$code] as $key => $local ) {
641 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
642 # Unrecognized magic word
645 $en = $this->mMagicWords['en'][$key];
646 array_shift( $local );
648 foreach ( $en as $word ) {
649 if ( !in_array( $word, $local ) ) {
650 $magicWords[$key] = $word;
659 * Get the magic words which do not match the case-sensitivity of the original words.
661 * @param $code The language code.
663 * @return The magic words whose case does not match in this language.
665 public function getCaseMismatchMagicWords( $code ) {
666 $this->loadFile( 'en' );
667 $this->loadFile( $code );
668 $magicWords = array();
669 foreach ( $this->mMagicWords[$code] as $key => $local ) {
670 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
671 # Unrecognized magic word
674 if ( $local[0] != $this->mMagicWords['en'][$key][0] ) {
675 $magicWords[$key] = $local[0];
682 * Get the untranslated special page names.
684 * @param $code The language code.
686 * @return The untranslated special page names in this language.
688 public function getUntraslatedSpecialPages( $code ) {
689 $this->loadFile( 'en' );
690 $this->loadFile( $code );
691 $specialPageAliases = array();
692 foreach ( $this->mSpecialPageAliases['en'] as $key => $value ) {
693 if ( !isset( $this->mSpecialPageAliases[$code][$key] ) ) {
694 $specialPageAliases[$key] = $value[0];
697 return $specialPageAliases;
701 * Get the obsolete special page names.
703 * @param $code The language code.
705 * @return The obsolete special page names in this language.
707 public function getObsoleteSpecialPages( $code ) {
708 $this->loadFile( 'en' );
709 $this->loadFile( $code );
710 $specialPageAliases = array();
711 foreach ( $this->mSpecialPageAliases[$code] as $key => $value ) {
712 if ( !isset( $this->mSpecialPageAliases['en'][$key] ) ) {
713 $specialPageAliases[$key] = $value[0];
716 return $specialPageAliases;
720 class extensionLanguages extends languages {
721 private $mMessageGroup; # The message group
724 * Load the messages group.
725 * @param $group The messages group.
727 function __construct( MessageGroup $group ) {
728 $this->mMessageGroup = $group;
730 $bools = $this->mMessageGroup->getBools();
731 $this->mIgnoredMessages = $bools['ignored'];
732 $this->mOptionalMessages = $bools['optional'];
736 * Get the extension name.
738 * @return The extension name.
740 public function name() {
741 return $this->mMessageGroup->getLabel();
745 * Load the language file.
747 * @param $code The language code.
749 protected function loadFile( $code ) {
750 if( !isset( $this->mRawMessages[$code] ) ) {
751 $this->mRawMessages[$code] = $this->mMessageGroup->load( $code );
752 if( empty( $this->mRawMessages[$code] ) ) {
753 $this->mRawMessages[$code] = array();