3 * Handle messages in the language files.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup MaintenanceLanguage
25 * @ingroup MaintenanceLanguage
28 /** @var array List of languages */
29 protected $mLanguages; #
31 /** @var array Raw list of the messages in each language */
32 protected $mRawMessages;
34 /** @var array Messages in each language (except for English), divided to groups */
37 /** @var array Fallback language in each language */
40 /** @var array General messages in English, divided to groups */
41 protected $mGeneralMessages;
43 /** @var array All the messages which should be exist only in the English file */
44 protected $mIgnoredMessages;
46 /** @var array All the messages which may be translated or not, depending on the language */
47 protected $mOptionalMessages;
49 /** @var array Namespace names */
50 protected $mNamespaceNames;
52 /** @var array Namespace aliases */
53 protected $mNamespaceAliases;
55 /** @var array Magic words */
56 protected $mMagicWords;
58 /** @var array Special page aliases */
59 protected $mSpecialPageAliases;
62 * Load the list of languages: all the Messages*.php
63 * files in the languages directory.
65 * @param $exif bool Treat the Exif messages?
67 function __construct( $exif = true ) {
68 require __DIR__ . '/messageTypes.inc';
69 $this->mIgnoredMessages = $wgIgnoredMessages;
71 $this->mOptionalMessages = array_merge( $wgOptionalMessages );
73 $this->mOptionalMessages = array_merge( $wgOptionalMessages, $wgEXIFMessages );
76 $this->mLanguages = array_keys( Language::fetchLanguageNames( null, 'mwfile' ) );
77 sort( $this->mLanguages );
81 * Get the language list.
83 * @return array The language list.
85 public function getLanguages() {
86 return $this->mLanguages;
90 * Get the ignored messages list.
92 * @return array The ignored messages list.
94 public function getIgnoredMessages() {
95 return $this->mIgnoredMessages;
99 * Get the optional messages list.
101 * @return array The optional messages list.
103 public function getOptionalMessages() {
104 return $this->mOptionalMessages;
108 * Load the language file.
110 * @param $code string The language code.
112 protected function loadFile( $code ) {
113 if ( isset( $this->mRawMessages[$code] ) &&
114 isset( $this->mFallback[$code] ) &&
115 isset( $this->mNamespaceNames[$code] ) &&
116 isset( $this->mNamespaceAliases[$code] ) &&
117 isset( $this->mMagicWords[$code] ) &&
118 isset( $this->mSpecialPageAliases[$code] )
122 $this->mRawMessages[$code] = array();
123 $this->mFallback[$code] = '';
124 $this->mNamespaceNames[$code] = array();
125 $this->mNamespaceAliases[$code] = array();
126 $this->mMagicWords[$code] = array();
127 $this->mSpecialPageAliases[$code] = array();
128 $filename = Language::getMessagesFileName( $code );
129 if ( file_exists( $filename ) ) {
131 if ( isset( $messages ) ) {
132 $this->mRawMessages[$code] = $messages;
134 if ( isset( $fallback ) ) {
135 $this->mFallback[$code] = $fallback;
137 if ( isset( $namespaceNames ) ) {
138 $this->mNamespaceNames[$code] = $namespaceNames;
140 if ( isset( $namespaceAliases ) ) {
141 $this->mNamespaceAliases[$code] = $namespaceAliases;
143 if ( isset( $magicWords ) ) {
144 $this->mMagicWords[$code] = $magicWords;
146 if ( isset( $specialPageAliases ) ) {
147 $this->mSpecialPageAliases[$code] = $specialPageAliases;
153 * Load the messages for a specific language (which is not English) and divide them to
155 * all - all the messages.
156 * required - messages which should be translated in order to get a complete translation.
157 * optional - messages which can be translated, the fallback translation is used if not
159 * obsolete - messages which should not be translated, either because they do not exist,
160 * or they are ignored messages.
161 * translated - messages which are either required or optional, but translated from
162 * English and needed.
164 * @param $code string The language code.
166 private function loadMessages( $code ) {
167 if ( isset( $this->mMessages[$code] ) ) {
170 $this->loadFile( $code );
171 $this->loadGeneralMessages();
172 $this->mMessages[$code]['all'] = $this->mRawMessages[$code];
173 $this->mMessages[$code]['required'] = array();
174 $this->mMessages[$code]['optional'] = array();
175 $this->mMessages[$code]['obsolete'] = array();
176 $this->mMessages[$code]['translated'] = array();
177 foreach ( $this->mMessages[$code]['all'] as $key => $value ) {
178 if ( isset( $this->mGeneralMessages['required'][$key] ) ) {
179 $this->mMessages[$code]['required'][$key] = $value;
180 $this->mMessages[$code]['translated'][$key] = $value;
181 } elseif ( isset( $this->mGeneralMessages['optional'][$key] ) ) {
182 $this->mMessages[$code]['optional'][$key] = $value;
183 $this->mMessages[$code]['translated'][$key] = $value;
185 $this->mMessages[$code]['obsolete'][$key] = $value;
191 * Load the messages for English and divide them to groups:
192 * all - all the messages.
193 * required - messages which should be translated to other languages in order to get a
194 * complete translation.
195 * optional - messages which can be translated to other languages, but it's not required
196 * for a complete translation.
197 * ignored - messages which should not be translated to other languages.
198 * translatable - messages which are either required or optional, but can be translated
201 private function loadGeneralMessages() {
202 if ( isset( $this->mGeneralMessages ) ) {
205 $this->loadFile( 'en' );
206 $this->mGeneralMessages['all'] = $this->mRawMessages['en'];
207 $this->mGeneralMessages['required'] = array();
208 $this->mGeneralMessages['optional'] = array();
209 $this->mGeneralMessages['ignored'] = array();
210 $this->mGeneralMessages['translatable'] = array();
211 foreach ( $this->mGeneralMessages['all'] as $key => $value ) {
212 if ( in_array( $key, $this->mIgnoredMessages ) ) {
213 $this->mGeneralMessages['ignored'][$key] = $value;
214 } elseif ( in_array( $key, $this->mOptionalMessages ) ) {
215 $this->mGeneralMessages['optional'][$key] = $value;
216 $this->mGeneralMessages['translatable'][$key] = $value;
218 $this->mGeneralMessages['required'][$key] = $value;
219 $this->mGeneralMessages['translatable'][$key] = $value;
225 * Get all the messages for a specific language (not English), without the
226 * fallback language messages, divided to groups:
227 * all - all the messages.
228 * required - messages which should be translated in order to get a complete translation.
229 * optional - messages which can be translated, the fallback translation is used if not
231 * obsolete - messages which should not be translated, either because they do not exist,
232 * or they are ignored messages.
233 * translated - messages which are either required or optional, but translated from
234 * English and needed.
236 * @param $code string The language code.
238 * @return string The messages in this language.
240 public function getMessages( $code ) {
241 $this->loadMessages( $code );
243 return $this->mMessages[$code];
247 * Get all the general English messages, divided to groups:
248 * all - all the messages.
249 * required - messages which should be translated to other languages in
250 * order to get a complete translation.
251 * optional - messages which can be translated to other languages, but it's
252 * not required for a complete translation.
253 * ignored - messages which should not be translated to other languages.
254 * translatable - messages which are either required or optional, but can be
255 * translated from English.
257 * @return array The general English messages.
259 public function getGeneralMessages() {
260 $this->loadGeneralMessages();
262 return $this->mGeneralMessages;
266 * Get fallback language code for a specific language.
268 * @param $code string The language code.
270 * @return string Fallback code.
272 public function getFallback( $code ) {
273 $this->loadFile( $code );
275 return $this->mFallback[$code];
279 * Get namespace names for a specific language.
281 * @param $code string The language code.
283 * @return array Namespace names.
285 public function getNamespaceNames( $code ) {
286 $this->loadFile( $code );
288 return $this->mNamespaceNames[$code];
292 * Get namespace aliases for a specific language.
294 * @param $code string The language code.
296 * @return array Namespace aliases.
298 public function getNamespaceAliases( $code ) {
299 $this->loadFile( $code );
301 return $this->mNamespaceAliases[$code];
305 * Get magic words for a specific language.
307 * @param $code string The language code.
309 * @return array Magic words.
311 public function getMagicWords( $code ) {
312 $this->loadFile( $code );
314 return $this->mMagicWords[$code];
318 * Get special page aliases for a specific language.
320 * @param $code string The language code.
322 * @return array Special page aliases.
324 public function getSpecialPageAliases( $code ) {
325 $this->loadFile( $code );
327 return $this->mSpecialPageAliases[$code];
331 * Get the untranslated messages for a specific language.
333 * @param $code string The language code.
335 * @return array The untranslated messages for this language.
337 public function getUntranslatedMessages( $code ) {
338 $this->loadGeneralMessages();
339 $this->loadMessages( $code );
341 return array_diff_key( $this->mGeneralMessages['required'], $this->mMessages[$code]['required'] );
345 * Get the duplicate messages for a specific language.
347 * @param $code string The language code.
349 * @return array The duplicate messages for this language.
351 public function getDuplicateMessages( $code ) {
352 $this->loadGeneralMessages();
353 $this->loadMessages( $code );
354 $duplicateMessages = array();
355 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
356 if ( $this->mGeneralMessages['translatable'][$key] == $value ) {
357 $duplicateMessages[$key] = $value;
361 return $duplicateMessages;
365 * Get the obsolete messages for a specific language.
367 * @param $code string The language code.
369 * @return array The obsolete messages for this language.
371 public function getObsoleteMessages( $code ) {
372 $this->loadGeneralMessages();
373 $this->loadMessages( $code );
375 return $this->mMessages[$code]['obsolete'];
379 * Get the messages whose variables do not match the original ones.
381 * @param $code string The language code.
383 * @return array The messages whose variables do not match the original ones.
385 public function getMessagesWithMismatchVariables( $code ) {
386 $this->loadGeneralMessages();
387 $this->loadMessages( $code );
388 $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' );
389 $mismatchMessages = array();
390 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
392 foreach ( $variables as $var ) {
393 if ( preg_match( "/$var/sU", $this->mGeneralMessages['translatable'][$key] ) &&
394 !preg_match( "/$var/sU", $value )
398 if ( !preg_match( "/$var/sU", $this->mGeneralMessages['translatable'][$key] ) &&
399 preg_match( "/$var/sU", $value )
405 $mismatchMessages[$key] = $value;
409 return $mismatchMessages;
413 * Get the messages which do not use plural.
415 * @param $code string The language code.
417 * @return array The messages which do not use plural in this language.
419 public function getMessagesWithoutPlural( $code ) {
420 $this->loadGeneralMessages();
421 $this->loadMessages( $code );
422 $messagesWithoutPlural = array();
423 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
424 if ( stripos( $this->mGeneralMessages['translatable'][$key], '{{plural:' ) !== false &&
425 stripos( $value, '{{plural:' ) === false
427 $messagesWithoutPlural[$key] = $value;
431 return $messagesWithoutPlural;
435 * Get the empty messages.
437 * @param $code string The language code.
439 * @return array The empty messages for this language.
441 public function getEmptyMessages( $code ) {
442 $this->loadGeneralMessages();
443 $this->loadMessages( $code );
444 $emptyMessages = array();
445 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
446 if ( $value === '' || $value === '-' ) {
447 $emptyMessages[$key] = $value;
451 return $emptyMessages;
455 * Get the messages with trailing whitespace.
457 * @param $code string The language code.
459 * @return array The messages with trailing whitespace in this language.
461 public function getMessagesWithWhitespace( $code ) {
462 $this->loadGeneralMessages();
463 $this->loadMessages( $code );
464 $messagesWithWhitespace = array();
465 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
466 if ( $this->mGeneralMessages['translatable'][$key] !== '' && $value !== rtrim( $value ) ) {
467 $messagesWithWhitespace[$key] = $value;
471 return $messagesWithWhitespace;
475 * Get the non-XHTML messages.
477 * @param $code string The language code.
479 * @return array The non-XHTML messages for this language.
481 public function getNonXHTMLMessages( $code ) {
482 $this->loadGeneralMessages();
483 $this->loadMessages( $code );
484 $wrongPhrases = array(
492 $wrongPhrases = '~(' . implode( '|', $wrongPhrases ) . ')~sDu';
493 $nonXHTMLMessages = array();
494 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
495 if ( preg_match( $wrongPhrases, $value ) ) {
496 $nonXHTMLMessages[$key] = $value;
500 return $nonXHTMLMessages;
504 * Get the messages which include wrong characters.
506 * @param $code string The language code.
508 * @return array The messages which include wrong characters in this language.
510 public function getMessagesWithWrongChars( $code ) {
511 $this->loadGeneralMessages();
512 $this->loadMessages( $code );
514 '[LRM]' => "\xE2\x80\x8E",
515 '[RLM]' => "\xE2\x80\x8F",
516 '[LRE]' => "\xE2\x80\xAA",
517 '[RLE]' => "\xE2\x80\xAB",
518 '[POP]' => "\xE2\x80\xAC",
519 '[LRO]' => "\xE2\x80\xAD",
520 '[RLO]' => "\xE2\x80\xAB",
521 '[ZWSP]' => "\xE2\x80\x8B",
522 '[NBSP]' => "\xC2\xA0",
523 '[WJ]' => "\xE2\x81\xA0",
524 '[BOM]' => "\xEF\xBB\xBF",
525 '[FFFD]' => "\xEF\xBF\xBD",
527 $wrongRegExp = '/(' . implode( '|', array_values( $wrongChars ) ) . ')/sDu';
528 $wrongCharsMessages = array();
529 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
530 if ( preg_match( $wrongRegExp, $value ) ) {
531 foreach ( $wrongChars as $viewableChar => $hiddenChar ) {
532 $value = str_replace( $hiddenChar, $viewableChar, $value );
534 $wrongCharsMessages[$key] = $value;
538 return $wrongCharsMessages;
542 * Get the messages which include dubious links.
544 * @param $code string The language code.
546 * @return array The messages which include dubious links in this language.
548 public function getMessagesWithDubiousLinks( $code ) {
549 $this->loadGeneralMessages();
550 $this->loadMessages( $code );
551 $tc = Title::legalChars() . '#%{}';
553 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
555 preg_match_all( "/\[\[([{$tc}]+)(?:\\|(.+?))?]]/sDu", $value, $matches );
556 $numMatches = count( $matches[0] );
557 for ( $i = 0; $i < $numMatches; $i++ ) {
558 if ( preg_match( "/.*project.*/isDu", $matches[1][$i] ) ) {
559 $messages[$key][] = $matches[0][$i];
563 if ( isset( $messages[$key] ) ) {
564 $messages[$key] = implode( $messages[$key], ", " );
572 * Get the messages which include unbalanced brackets.
574 * @param $code string The language code.
576 * @return array The messages which include unbalanced brackets in this language.
578 public function getMessagesWithUnbalanced( $code ) {
579 $this->loadGeneralMessages();
580 $this->loadMessages( $code );
582 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
583 $a = $b = $c = $d = 0;
584 foreach ( preg_split( '//', $value ) as $char ) {
601 if ( $a !== $b || $c !== $d ) {
602 $messages[$key] = "$a, $b, $c, $d";
610 * Get the untranslated namespace names.
612 * @param $code string The language code.
614 * @return array The untranslated namespace names in this language.
616 public function getUntranslatedNamespaces( $code ) {
617 $this->loadFile( 'en' );
618 $this->loadFile( $code );
619 $namespacesDiff = array_diff_key( $this->mNamespaceNames['en'], $this->mNamespaceNames[$code] );
620 if ( isset( $namespacesDiff[NS_MAIN] ) ) {
621 unset( $namespacesDiff[NS_MAIN] );
624 return $namespacesDiff;
628 * Get the project talk namespace names with no $1.
630 * @param $code string The language code.
632 * @return array The problematic project talk namespaces in this language.
634 public function getProblematicProjectTalks( $code ) {
635 $this->loadFile( $code );
636 $namespaces = array();
638 # Check default namespace name
639 if ( isset( $this->mNamespaceNames[$code][NS_PROJECT_TALK] ) ) {
640 $default = $this->mNamespaceNames[$code][NS_PROJECT_TALK];
641 if ( strpos( $default, '$1' ) === false ) {
642 $namespaces[$default] = 'default';
646 # Check namespace aliases
647 foreach ( $this->mNamespaceAliases[$code] as $key => $value ) {
648 if ( $value == NS_PROJECT_TALK && strpos( $key, '$1' ) === false ) {
649 $namespaces[$key] = '';
657 * Get the untranslated magic words.
659 * @param $code string The language code.
661 * @return array The untranslated magic words in this language.
663 public function getUntranslatedMagicWords( $code ) {
664 $this->loadFile( 'en' );
665 $this->loadFile( $code );
666 $magicWords = array();
667 foreach ( $this->mMagicWords['en'] as $key => $value ) {
668 if ( !isset( $this->mMagicWords[$code][$key] ) ) {
669 $magicWords[$key] = $value[1];
677 * Get the obsolete magic words.
679 * @param $code string The language code.
681 * @return array The obsolete magic words in this language.
683 public function getObsoleteMagicWords( $code ) {
684 $this->loadFile( 'en' );
685 $this->loadFile( $code );
686 $magicWords = array();
687 foreach ( $this->mMagicWords[$code] as $key => $value ) {
688 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
689 $magicWords[$key] = $value[1];
697 * Get the magic words that override the original English magic word.
699 * @param $code string The language code.
701 * @return array The overriding magic words in this language.
703 public function getOverridingMagicWords( $code ) {
704 $this->loadFile( 'en' );
705 $this->loadFile( $code );
706 $magicWords = array();
707 foreach ( $this->mMagicWords[$code] as $key => $local ) {
708 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
709 # Unrecognized magic word
712 $en = $this->mMagicWords['en'][$key];
713 array_shift( $local );
715 foreach ( $en as $word ) {
716 if ( !in_array( $word, $local ) ) {
717 $magicWords[$key] = $word;
727 * Get the magic words which do not match the case-sensitivity of the original words.
729 * @param $code string The language code.
731 * @return array The magic words whose case does not match in this language.
733 public function getCaseMismatchMagicWords( $code ) {
734 $this->loadFile( 'en' );
735 $this->loadFile( $code );
736 $magicWords = array();
737 foreach ( $this->mMagicWords[$code] as $key => $local ) {
738 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
739 # Unrecognized magic word
742 if ( $local[0] != $this->mMagicWords['en'][$key][0] ) {
743 $magicWords[$key] = $local[0];
751 * Get the untranslated special page names.
753 * @param $code string The language code.
755 * @return array The untranslated special page names in this language.
757 public function getUntraslatedSpecialPages( $code ) {
758 $this->loadFile( 'en' );
759 $this->loadFile( $code );
760 $specialPageAliases = array();
761 foreach ( $this->mSpecialPageAliases['en'] as $key => $value ) {
762 if ( !isset( $this->mSpecialPageAliases[$code][$key] ) ) {
763 $specialPageAliases[$key] = $value[0];
767 return $specialPageAliases;
771 * Get the obsolete special page names.
773 * @param $code string The language code.
775 * @return array The obsolete special page names in this language.
777 public function getObsoleteSpecialPages( $code ) {
778 $this->loadFile( 'en' );
779 $this->loadFile( $code );
780 $specialPageAliases = array();
781 foreach ( $this->mSpecialPageAliases[$code] as $key => $value ) {
782 if ( !isset( $this->mSpecialPageAliases['en'][$key] ) ) {
783 $specialPageAliases[$key] = $value[0];
787 return $specialPageAliases;
791 class ExtensionLanguages extends Languages {
795 private $mMessageGroup;
798 * Load the messages group.
799 * @param $group MessageGroup The messages group.
801 function __construct( MessageGroup $group ) {
802 $this->mMessageGroup = $group;
804 $this->mIgnoredMessages = $this->mMessageGroup->getIgnored();
805 $this->mOptionalMessages = $this->mMessageGroup->getOptional();
809 * Get the extension name.
811 * @return string The extension name.
813 public function name() {
814 return $this->mMessageGroup->getLabel();
818 * Load the language file.
820 * @param $code string The language code.
822 protected function loadFile( $code ) {
823 if ( !isset( $this->mRawMessages[$code] ) ) {
824 $this->mRawMessages[$code] = $this->mMessageGroup->load( $code );
825 if ( empty( $this->mRawMessages[$code] ) ) {
826 $this->mRawMessages[$code] = array();