7 if( !defined( 'MEDIAWIKI' ) ) {
8 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
13 # In general you should not make customizations in these language files
14 # directly, but should use the MediaWiki: special namespace to customize
15 # user interface messages through the wiki.
16 # See http://meta.wikipedia.org/wiki/MediaWiki_namespace
18 # NOTE TO TRANSLATORS: Do not copy this whole file when making translations!
19 # A lot of common constants and a base class with inheritable methods are
20 # defined here, which should not be redefined. See the other LanguageXx.php
25 global $wgLanguageNames;
26 require_once( 'Names.php' );
28 global $wgInputEncoding, $wgOutputEncoding;
29 global $wgDBname, $wgMemc;
32 * These are always UTF-8, they exist only for backwards compatibility
34 $wgInputEncoding = "UTF-8";
35 $wgOutputEncoding = "UTF-8";
37 if( function_exists( 'mb_strtoupper' ) ) {
38 mb_internal_encoding('UTF-8');
41 /* a fake language converter */
44 function FakeConverter($langobj) {$this->mLang
= $langobj;}
45 function convert($t, $i) {return $t;}
46 function parserConvert($t, $p) {return $t;}
47 function getVariants() { return array( $this->mLang
->getCode() ); }
48 function getPreferredVariant() {return $this->mLang
->getCode(); }
49 function findVariantLink(&$l, &$n) {}
50 function getExtraHashOptions() {return '';}
51 function getParsedTitle() {return '';}
52 function markNoConversion($text) {return $text;}
53 function convertCategoryKey( $key ) {return $key; }
57 #--------------------------------------------------------------------------
58 # Internationalisation code
59 #--------------------------------------------------------------------------
62 var $mConverter, $mVariants, $mCode, $mLoaded = false;
64 static public $mLocalisationKeys = array( 'fallback', 'namespaceNames',
65 'quickbarSettings', 'skinNames', 'mathNames',
66 'bookstoreList', 'magicWords', 'messages', 'rtl', 'digitTransformTable',
67 'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
68 'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases',
69 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
70 'defaultDateFormat', 'extraUserToggles' );
72 static public $mMergeableMapKeys = array( 'messages', 'namespaceNames', 'mathNames',
73 'dateFormats', 'defaultUserOptionOverrides', 'magicWords' );
75 static public $mMergeableListKeys = array( 'extraUserToggles' );
77 static public $mLocalisationCache = array();
79 static public $mWeekdayMsgs = array(
80 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
84 static public $mWeekdayAbbrevMsgs = array(
85 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
88 static public $mMonthMsgs = array(
89 'january', 'february', 'march', 'april', 'may_long', 'june',
90 'july', 'august', 'september', 'october', 'november',
93 static public $mMonthGenMsgs = array(
94 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
95 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
98 static public $mMonthAbbrevMsgs = array(
99 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
100 'sep', 'oct', 'nov', 'dec'
104 * Create a language object for a given language code
106 static function factory( $code ) {
108 static $recursionLevel = 0;
110 if ( $code == 'en' ) {
113 $class = 'Language' . str_replace( '-', '_', ucfirst( $code ) );
114 // Preload base classes to work around APC/PHP5 bug
115 if ( file_exists( "$IP/languages/$class.deps.php" ) ) {
116 include_once("$IP/languages/$class.deps.php");
118 if ( file_exists( "$IP/languages/$class.php" ) ) {
119 include_once("$IP/languages/$class.php");
123 if ( $recursionLevel > 5 ) {
124 throw new MWException( "Language fallback loop detected when creating class $class\n" );
127 if( ! class_exists( $class ) ) {
128 $fallback = Language
::getFallbackFor( $code );
130 $lang = Language
::factory( $fallback );
132 $lang->setCode( $code );
140 function __construct() {
141 $this->mConverter
= new FakeConverter($this);
142 // Set the code to the name of the descendant
143 if ( get_class( $this ) == 'Language' ) {
146 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
151 * Hook which will be called if this is the content language.
152 * Descendants can use this to register hook functions or modify globals
154 function initContLang() {}
160 function getDefaultUserOptions() {
161 return User
::getDefaultOptions();
165 * Exports $wgBookstoreListEn
168 function getBookstoreList() {
170 return $this->bookstoreList
;
176 function getNamespaces() {
178 return $this->namespaceNames
;
182 * A convenience function that returns the same thing as
183 * getNamespaces() except with the array values changed to ' '
184 * where it found '_', useful for producing output to be displayed
185 * e.g. in <select> forms.
189 function getFormattedNamespaces() {
190 $ns = $this->getNamespaces();
191 foreach($ns as $k => $v) {
192 $ns[$k] = strtr($v, '_', ' ');
198 * Get a namespace value by key
200 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
201 * echo $mw_ns; // prints 'MediaWiki'
204 * @param int $index the array key of the namespace to return
205 * @return mixed, string if the namespace value exists, otherwise false
207 function getNsText( $index ) {
208 $ns = $this->getNamespaces();
209 return isset( $ns[$index] ) ?
$ns[$index] : false;
213 * A convenience function that returns the same thing as
214 * getNsText() except with '_' changed to ' ', useful for
219 function getFormattedNsText( $index ) {
220 $ns = $this->getNsText( $index );
221 return strtr($ns, '_', ' ');
225 * Get a namespace key by value, case insensetive.
227 * @param string $text
228 * @return mixed An integer if $text is a valid value otherwise false
230 function getNsIndex( $text ) {
232 $index = @$this->mNamespaceIds
[$this->lc($text)];
233 if ( is_null( $index ) ) {
241 * short names for language variants used for language conversion links.
243 * @param string $code
246 function getVariantname( $code ) {
247 return $this->getMessageFromDB( "variantname-$code" );
250 function specialPage( $name ) {
251 return $this->getNsText(NS_SPECIAL
) . ':' . $name;
254 function getQuickbarSettings() {
256 return $this->quickbarSettings
;
259 function getSkinNames() {
261 return $this->skinNames
;
264 function getMathNames() {
266 return $this->mathNames
;
269 function getDatePreferences() {
271 return $this->datePreferences
;
274 function getDateFormats() {
276 return $this->dateFormats
;
279 function getDatePreferenceMigrationMap() {
281 return $this->datePreferenceMigrationMap
;
284 function getDefaultUserOptionOverrides() {
286 return $this->defaultUserOptionOverrides
;
289 function getExtraUserToggles() {
291 return $this->extraUserToggles
;
294 function getUserToggle( $tog ) {
295 return $this->getMessageFromDB( "tog-$tog" );
299 * Get language names, indexed by code.
300 * If $customisedOnly is true, only returns codes with a messages file
302 function getLanguageNames( $customisedOnly = false ) {
303 global $wgLanguageNames;
304 if ( !$customisedOnly ) {
305 return $wgLanguageNames;
309 $messageFiles = glob( "$IP/languages/Messages*.php" );
311 foreach ( $messageFiles as $file ) {
312 if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
313 $code = str_replace( '_', '-', strtolower( $m[1] ) );
314 if ( isset( $wgLanguageNames[$code] ) ) {
315 $names[$code] = $wgLanguageNames[$code];
323 * Ugly hack to get a message maybe from the MediaWiki namespace, if this
324 * language object is the content or user language.
326 function getMessageFromDB( $msg ) {
327 global $wgContLang, $wgLang;
328 if ( $wgContLang->getCode() == $this->getCode() ) {
330 return wfMsgForContent( $msg );
331 } elseif ( $wgLang->getCode() == $this->getCode() ) {
333 return wfMsg( $msg );
335 # Neither, get from localisation
336 return $this->getMessage( $msg );
340 function getLanguageName( $code ) {
341 global $wgLanguageNames;
342 if ( ! array_key_exists( $code, $wgLanguageNames ) ) {
345 return $wgLanguageNames[$code];
348 function getMonthName( $key ) {
349 return $this->getMessageFromDB( self
::$mMonthMsgs[$key-1] );
352 function getMonthNameGen( $key ) {
353 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key-1] );
356 function getMonthAbbreviation( $key ) {
357 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key-1] );
360 function getWeekdayName( $key ) {
361 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key-1] );
364 function getWeekdayAbbreviation( $key ) {
365 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key-1] );
369 * Used by date() and time() to adjust the time output.
371 * @param int $ts the time in date('YmdHis') format
372 * @param mixed $tz adjust the time by this amount (default false,
373 * mean we get user timecorrection setting)
376 function userAdjust( $ts, $tz = false ) {
377 global $wgUser, $wgLocalTZoffset;
380 $tz = $wgUser->getOption( 'timecorrection' );
383 # minutes and hours differences:
388 # Global offset in minutes.
389 if( isset($wgLocalTZoffset) ) {
390 $hrDiff = $wgLocalTZoffset %
60;
391 $minDiff = $wgLocalTZoffset - ($hrDiff * 60);
393 } elseif ( strpos( $tz, ':' ) !== false ) {
394 $tzArray = explode( ':', $tz );
395 $hrDiff = intval($tzArray[0]);
396 $minDiff = intval($hrDiff < 0 ?
-$tzArray[1] : $tzArray[1]);
398 $hrDiff = intval( $tz );
401 # No difference ? Return time unchanged
402 if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
404 # Generate an adjusted date
406 (int)substr( $ts, 8, 2) ) +
$hrDiff, # Hours
407 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
408 (int)substr( $ts, 12, 2 ), # Seconds
409 (int)substr( $ts, 4, 2 ), # Month
410 (int)substr( $ts, 6, 2 ), # Day
411 (int)substr( $ts, 0, 4 ) ); #Year
412 return date( 'YmdHis', $t );
416 * This is a workalike of PHP's date() function, but with better
417 * internationalisation, a reduced set of format characters, and a better
420 * Supported format characters are dDjlFmMnYyHis. See the PHP manual for
421 * definitions. There are a number of extensions, which start with "x":
423 * xn Do not translate digits of the next numeric format character
424 * xr Use roman numerals for the next numeric format character
426 * xg Genitive month name
428 * Characters enclosed in double quotes will be considered literal (with
429 * the quotes themselves removed). Unmatched quotes will be considered
430 * literal quotes. Example:
432 * "The month is" F => The month is January
435 * Backslash escaping is also supported.
437 * @param string $format
438 * @param string $ts 14-character timestamp
442 function sprintfDate( $format, $ts ) {
446 for ( $p = 0; $p < strlen( $format ); $p++
) {
449 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
450 $code .= $format[++
$p];
464 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
467 $num = substr( $ts, 6, 2 );
470 $s .= $this->getWeekdayAbbreviation( self
::calculateWeekday( $ts ) );
473 $num = intval( substr( $ts, 6, 2 ) );
476 $s .= $this->getWeekdayName( self
::calculateWeekday( $ts ) );
479 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
482 $num = substr( $ts, 4, 2 );
485 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
488 $num = intval( substr( $ts, 4, 2 ) );
491 $num = substr( $ts, 0, 4 );
494 $num = substr( $ts, 2, 2 );
497 $num = substr( $ts, 8, 2 );
500 $num = intval( substr( $ts, 8, 2 ) );
503 $num = substr( $ts, 10, 2 );
506 $num = substr( $ts, 12, 2 );
510 if ( $p < strlen( $format ) - 1 ) {
518 if ( $p < strlen( $format ) - 1 ) {
519 $endQuote = strpos( $format, '"', $p +
1 );
520 if ( $endQuote === false ) {
521 # No terminating quote, assume literal "
524 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
528 # Quote at end of string, assume literal "
535 if ( $num !== false ) {
539 } elseif ( $roman ) {
540 $s .= Language
::romanNumeral( $num );
543 $s .= $this->formatNum( $num, true );
552 * Roman number formatting up to 100
554 static function romanNumeral( $num ) {
555 static $units = array( 0, 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' );
556 static $decades = array( 0, 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' );
557 $num = intval( $num );
558 if ( $num > 100 ||
$num <= 0 ) {
563 $s .= $decades[floor( $num / 10 )];
573 * Calculate the day of the week for a 14-character timestamp
574 * 1 for Sunday through to 7 for Saturday
575 * This takes about 100us on a slow computer
577 static function calculateWeekday( $ts ) {
578 return date( 'w', wfTimestamp( TS_UNIX
, $ts ) ) +
1;
582 * This is meant to be used by time(), date(), and timeanddate() to get
583 * the date preference they're supposed to use, it should be used in
587 * function timeanddate([...], $format = true) {
588 * $datePreference = $this->dateFormat($format);
593 * @param mixed $usePrefs: if true, the user's preference is used
594 * if false, the site/language default is used
595 * if int/string, assumed to be a format.
598 function dateFormat( $usePrefs = true ) {
601 if( is_bool( $usePrefs ) ) {
603 $datePreference = $wgUser->getDatePreference();
605 $options = User
::getDefaultOptions();
606 $datePreference = (string)$options['date'];
609 $datePreference = (string)$usePrefs;
613 if( $datePreference == '' ) {
617 return $datePreference;
622 * @param mixed $ts the time format which needs to be turned into a
623 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
624 * @param bool $adj whether to adjust the time output according to the
625 * user configured offset ($timecorrection)
626 * @param mixed $format true to use user's date format preference
627 * @param string $timecorrection the time offset as returned by
628 * validateTimeZone() in Special:Preferences
631 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
634 $ts = $this->userAdjust( $ts, $timecorrection );
637 $pref = $this->dateFormat( $format );
638 if( $pref == 'default' ||
!isset( $this->dateFormats
["$pref date"] ) ) {
639 $pref = $this->defaultDateFormat
;
641 return $this->sprintfDate( $this->dateFormats
["$pref date"], $ts );
646 * @param mixed $ts the time format which needs to be turned into a
647 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
648 * @param bool $adj whether to adjust the time output according to the
649 * user configured offset ($timecorrection)
650 * @param mixed $format true to use user's date format preference
651 * @param string $timecorrection the time offset as returned by
652 * validateTimeZone() in Special:Preferences
655 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
658 $ts = $this->userAdjust( $ts, $timecorrection );
661 $pref = $this->dateFormat( $format );
662 if( $pref == 'default' ||
!isset( $this->dateFormats
["$pref time"] ) ) {
663 $pref = $this->defaultDateFormat
;
665 return $this->sprintfDate( $this->dateFormats
["$pref time"], $ts );
670 * @param mixed $ts the time format which needs to be turned into a
671 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
672 * @param bool $adj whether to adjust the time output according to the
673 * user configured offset ($timecorrection)
675 * @param mixed $format what format to return, if it's false output the
676 * default one (default true)
677 * @param string $timecorrection the time offset as returned by
678 * validateTimeZone() in Special:Preferences
681 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
684 $ts = $this->userAdjust( $ts, $timecorrection );
687 $pref = $this->dateFormat( $format );
688 if( $pref == 'default' ||
!isset( $this->dateFormats
["$pref both"] ) ) {
689 $pref = $this->defaultDateFormat
;
692 return $this->sprintfDate( $this->dateFormats
["$pref both"], $ts );
695 function getMessage( $key ) {
697 return @$this->messages
[$key];
700 function getAllMessages() {
702 return $this->messages
;
705 function iconv( $in, $out, $string ) {
706 # For most languages, this is a wrapper for iconv
707 return iconv( $in, $out, $string );
710 function ucfirst( $str ) {
711 return self
::uc( $str, true );
714 function uc( $str, $first = false ) {
715 if ( function_exists( 'mb_strtoupper' ) )
717 if ( self
::isMultibyte( $str ) )
718 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
720 return ucfirst( $str );
722 return self
::isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
724 if ( self
::isMultibyte( $str ) ) {
725 list( $wikiUpperChars ) = $this->getCaseMaps();
726 $x = $first ?
'^' : '';
728 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
729 "strtr( \"\$1\" , \$wikiUpperChars )",
733 return $first ?
ucfirst( $str ) : strtoupper( $str );
736 function lcfirst( $str ) {
737 return self
::lc( $str, true );
740 function lc( $str, $first = false ) {
741 if ( function_exists( 'mb_strtolower' ) )
743 if ( self
::isMultibyte( $str ) )
744 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
746 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
748 return self
::isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
750 if ( self
::isMultibyte( $str ) ) {
751 list( , $wikiLowerChars ) = self
::getCaseMaps();
752 $x = $first ?
'^' : '';
754 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
755 "strtr( \"\$1\" , \$wikiLowerChars )",
759 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
762 function isMultibyte( $str ) {
763 return (bool)preg_match( '/^[\x80-\xff]/', $str );
766 function checkTitleEncoding( $s ) {
767 if( is_array( $s ) ) {
768 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
770 # Check for non-UTF-8 URLs
771 $ishigh = preg_match( '/[\x80-\xff]/', $s);
772 if(!$ishigh) return $s;
774 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
775 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
776 if( $isutf8 ) return $s;
778 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
781 function fallback8bitEncoding() {
783 return $this->fallback8bitEncoding
;
787 * Some languages have special punctuation to strip out
788 * or characters which need to be converted for MySQL's
789 * indexing to grok it correctly. Make such changes here.
794 function stripForSearch( $string ) {
795 # MySQL fulltext index doesn't grok utf-8, so we
796 # need to fold cases and convert to hex
798 wfProfileIn( __METHOD__
);
799 if( function_exists( 'mb_strtolower' ) ) {
801 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
802 "'U8' . bin2hex( \"$1\" )",
803 mb_strtolower( $string ) );
805 list( , $wikiLowerChars ) = self
::getCaseMaps();
807 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
808 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
811 wfProfileOut( __METHOD__
);
815 function convertForSearchResult( $termsArray ) {
816 # some languages, e.g. Chinese, need to do a conversion
817 # in order for search results to be displayed correctly
822 * Get the first character of a string.
827 function firstChar( $s ) {
828 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
829 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
831 return isset( $matches[1] ) ?
$matches[1] : "";
834 function initEncoding() {
835 # Some languages may have an alternate char encoding option
836 # (Esperanto X-coding, Japanese furigana conversion, etc)
837 # If this language is used as the primary content language,
838 # an override to the defaults can be set here on startup.
841 function recodeForEdit( $s ) {
842 # For some languages we'll want to explicitly specify
843 # which characters make it into the edit box raw
844 # or are converted in some way or another.
845 # Note that if wgOutputEncoding is different from
846 # wgInputEncoding, this text will be further converted
847 # to wgOutputEncoding.
848 global $wgEditEncoding;
849 if( $wgEditEncoding == '' or
850 $wgEditEncoding == 'UTF-8' ) {
853 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
857 function recodeInput( $s ) {
858 # Take the previous into account.
859 global $wgEditEncoding;
860 if($wgEditEncoding != "") {
861 $enc = $wgEditEncoding;
865 if( $enc == 'UTF-8' ) {
868 return $this->iconv( $enc, 'UTF-8', $s );
873 * For right-to-left language support
883 * A hidden direction mark (LRM or RLM), depending on the language direction
887 function getDirMark() {
888 return $this->isRTL() ?
"\xE2\x80\x8F" : "\xE2\x80\x8E";
892 * An arrow, depending on the language direction
896 function getArrow() {
897 return $this->isRTL() ?
'←' : '→';
901 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
905 function linkPrefixExtension() {
907 return $this->linkPrefixExtension
;
910 function &getMagicWords() {
912 return $this->magicWords
;
915 # Fill a MagicWord object with data from here
916 function getMagic( &$mw ) {
917 if ( !isset( $this->mMagicExtensions
) ) {
918 $this->mMagicExtensions
= array();
919 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
921 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
922 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
924 $magicWords =& $this->getMagicWords();
925 if ( isset( $magicWords[$mw->mId
] ) ) {
926 $rawEntry = $magicWords[$mw->mId
];
928 # Fall back to English if local list is incomplete
929 $magicWords =& Language
::getMagicWords();
930 $rawEntry = $magicWords[$mw->mId
];
934 $mw->mCaseSensitive
= $rawEntry[0];
935 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
939 * Italic is unsuitable for some languages
943 * @param string $text The text to be emphasized.
946 function emphasize( $text ) {
947 return "<em>$text</em>";
951 * Normally we output all numbers in plain en_US style, that is
952 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
953 * point twohundredthirtyfive. However this is not sutable for all
954 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
955 * Icelandic just want to use commas instead of dots, and dots instead
956 * of commas like "293.291,235".
958 * An example of this function being called:
960 * wfMsg( 'message', $wgLang->formatNum( $num ) )
963 * See LanguageGu.php for the Gujarati implementation and
964 * LanguageIs.php for the , => . and . => , implementation.
966 * @todo check if it's viable to use localeconv() for the decimal
969 * @param mixed $number the string to be formatted, should be an integer or
970 * a floating point number.
971 * @param bool $nocommafy Set to true for special numbers like dates
974 function formatNum( $number, $nocommafy = false ) {
975 global $wgTranslateNumerals;
977 $number = $this->commafy($number);
978 $s = $this->separatorTransformTable();
979 if (!is_null($s)) { $number = strtr($number, $s); }
982 if ($wgTranslateNumerals) {
983 $s = $this->digitTransformTable();
984 if (!is_null($s)) { $number = strtr($number, $s); }
991 * Adds commas to a given number
996 function commafy($_) {
997 return strrev((string)preg_replace('/(\d{3})(?=\d)(?!\d*\.)/','$1,',strrev($_)));
1000 function digitTransformTable() {
1002 return $this->digitTransformTable
;
1005 function separatorTransformTable() {
1007 return $this->separatorTransformTable
;
1012 * For the credit list in includes/Credits.php (action=credits)
1017 function listToText( $l ) {
1020 for ($i = $m; $i >= 0; $i--) {
1023 } else if ($i == $m - 1) {
1024 $s = $l[$i] . ' ' . $this->getMessageFromDB( 'and' ) . ' ' . $s;
1026 $s = $l[$i] . ', ' . $s;
1032 # Crop a string from the beginning or end to a certain number of bytes.
1033 # (Bytes are used because our storage has limited byte lengths for some
1034 # columns in the database.) Multibyte charsets will need to make sure that
1035 # only whole characters are included!
1037 # $length does not include the optional ellipsis.
1038 # If $length is negative, snip from the beginning
1039 function truncate( $string, $length, $ellipsis = "" ) {
1040 if( $length == 0 ) {
1043 if ( strlen( $string ) <= abs( $length ) ) {
1047 $string = substr( $string, 0, $length );
1048 $char = ord( $string[strlen( $string ) - 1] );
1049 if ($char >= 0xc0) {
1050 # We got the first byte only of a multibyte char; remove it.
1051 $string = substr( $string, 0, -1 );
1052 } elseif( $char >= 0x80 &&
1053 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
1054 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
1055 # We chopped in the middle of a character; remove it
1058 return $string . $ellipsis;
1060 $string = substr( $string, $length );
1061 $char = ord( $string[0] );
1062 if( $char >= 0x80 && $char < 0xc0 ) {
1063 # We chopped in the middle of a character; remove the whole thing
1064 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
1066 return $ellipsis . $string;
1071 * Grammatical transformations, needed for inflected languages
1072 * Invoked by putting {{grammar:case|word}} in a message
1074 * @param string $word
1075 * @param string $case
1078 function convertGrammar( $word, $case ) {
1079 global $wgGrammarForms;
1080 if ( isset($wgGrammarForms['en'][$case][$word]) ) {
1081 return $wgGrammarForms['en'][$case][$word];
1087 * Plural form transformations, needed for some languages.
1088 * For example, where are 3 form of plural in Russian and Polish,
1089 * depending on "count mod 10". See [[w:Plural]]
1090 * For English it is pretty simple.
1092 * Invoked by putting {{plural:count|wordform1|wordform2}}
1093 * or {{plural:count|wordform1|wordform2|wordform3}}
1095 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
1097 * @param integer $count
1098 * @param string $wordform1
1099 * @param string $wordform2
1100 * @param string $wordform3 (optional)
1103 function convertPlural( $count, $w1, $w2, $w3) {
1104 return $count == '1' ?
$w1 : $w2;
1108 * For translaing of expiry times
1109 * @param string The validated block time in English
1110 * @return Somehow translated block time
1111 * @see LanguageFi.php for example implementation
1113 function translateBlockExpiry( $str ) {
1115 $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
1117 if ( $scBlockExpiryOptions == '-') {
1121 foreach (explode(',', $scBlockExpiryOptions) as $option) {
1122 if ( strpos($option, ":") === false )
1124 list($show, $value) = explode(":", $option);
1125 if ( strcmp ( $str, $value) == 0 )
1126 return '<span title="' . htmlspecialchars($str). '">' .
1127 htmlspecialchars( trim( $show ) ) . '</span>';
1134 * languages like Chinese need to be segmented in order for the diff
1137 * @param string $text
1140 function segmentForDiff( $text ) {
1145 * and unsegment to show the result
1147 * @param string $text
1150 function unsegmentForDiff( $text ) {
1154 # convert text to different variants of a language.
1155 function convert( $text, $isTitle = false) {
1156 return $this->mConverter
->convert($text, $isTitle);
1159 # Convert text from within Parser
1160 function parserConvert( $text, &$parser ) {
1161 return $this->mConverter
->parserConvert( $text, $parser );
1165 * Perform output conversion on a string, and encode for safe HTML output.
1166 * @param string $text
1167 * @param bool $isTitle -- wtf?
1169 * @todo this should get integrated somewhere sane
1171 function convertHtml( $text, $isTitle = false ) {
1172 return htmlspecialchars( $this->convert( $text, $isTitle ) );
1175 function convertCategoryKey( $key ) {
1176 return $this->mConverter
->convertCategoryKey( $key );
1180 * get the list of variants supported by this langauge
1181 * see sample implementation in LanguageZh.php
1183 * @return array an array of language codes
1185 function getVariants() {
1186 return $this->mConverter
->getVariants();
1190 function getPreferredVariant( $fromUser = true ) {
1191 return $this->mConverter
->getPreferredVariant( $fromUser );
1195 * if a language supports multiple variants, it is
1196 * possible that non-existing link in one variant
1197 * actually exists in another variant. this function
1198 * tries to find it. See e.g. LanguageZh.php
1200 * @param string $link the name of the link
1201 * @param mixed $nt the title object of the link
1202 * @return null the input parameters may be modified upon return
1204 function findVariantLink( &$link, &$nt ) {
1205 $this->mConverter
->findVariantLink($link, $nt);
1209 * returns language specific options used by User::getPageRenderHash()
1210 * for example, the preferred language variant
1215 function getExtraHashOptions() {
1216 return $this->mConverter
->getExtraHashOptions();
1220 * for languages that support multiple variants, the title of an
1221 * article may be displayed differently in different variants. this
1222 * function returns the apporiate title defined in the body of the article.
1226 function getParsedTitle() {
1227 return $this->mConverter
->getParsedTitle();
1231 * Enclose a string with the "no conversion" tag. This is used by
1232 * various functions in the Parser
1234 * @param string $text text to be tagged for no conversion
1235 * @return string the tagged text
1237 function markNoConversion( $text ) {
1238 return $this->mConverter
->markNoConversion( $text );
1242 * A regular expression to match legal word-trailing characters
1243 * which should be merged onto a link of the form [[foo]]bar.
1248 function linkTrail() {
1250 return $this->linkTrail
;
1253 function getLangObj() {
1258 * Get the RFC 3066 code for this language object
1260 function getCode() {
1261 return $this->mCode
;
1264 function setCode( $code ) {
1265 $this->mCode
= $code;
1268 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
1269 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
1272 static function getLocalisationArray( $code, $disableCache = false ) {
1273 self
::loadLocalisation( $code, $disableCache );
1274 return self
::$mLocalisationCache[$code];
1278 * Load localisation data for a given code into the static cache
1280 * @return array Dependencies, map of filenames to mtimes
1282 static function loadLocalisation( $code, $disableCache = false ) {
1283 static $recursionGuard = array();
1284 global $wgMemc, $wgDBname, $IP;
1287 throw new MWException( "Invalid language code requested" );
1290 if ( !$disableCache ) {
1291 # Try the per-process cache
1292 if ( isset( self
::$mLocalisationCache[$code] ) ) {
1293 return self
::$mLocalisationCache[$code]['deps'];
1296 wfProfileIn( __METHOD__
);
1298 # Try the serialized directory
1299 $cache = wfGetPrecompiledData( self
::getFileName( "Messages", $code, '.ser' ) );
1301 self
::$mLocalisationCache[$code] = $cache;
1302 wfDebug( "Got localisation for $code from precompiled data file\n" );
1303 wfProfileOut( __METHOD__
);
1304 return self
::$mLocalisationCache[$code]['deps'];
1307 # Try the global cache
1308 $memcKey = "$wgDBname:localisation:$code";
1309 $cache = $wgMemc->get( $memcKey );
1312 # Check file modification times
1313 foreach ( $cache['deps'] as $file => $mtime ) {
1314 if ( filemtime( $file ) > $mtime ) {
1319 if ( self
::isLocalisationOutOfDate( $cache ) ) {
1320 $wgMemc->delete( $memcKey );
1322 wfDebug( "Localisation cache for $code had expired due to update of $file\n" );
1324 self
::$mLocalisationCache[$code] = $cache;
1325 wfDebug( "Got localisation for $code from cache\n" );
1326 wfProfileOut( __METHOD__
);
1327 return $cache['deps'];
1331 wfProfileIn( __METHOD__
);
1334 if ( $code != 'en' ) {
1340 # Load the primary localisation from the source file
1342 $filename = self
::getFileName( "$IP/languages/Messages", $code, '.php' );
1343 if ( !file_exists( $filename ) ) {
1344 wfDebug( "No localisation file for $code, using implicit fallback to en\n" );
1348 $deps = array( $filename => filemtime( $filename ) );
1349 require( $filename );
1350 $cache = compact( self
::$mLocalisationKeys );
1351 wfDebug( "Got localisation for $code from source\n" );
1354 if ( !empty( $fallback ) ) {
1355 # Load the fallback localisation, with a circular reference guard
1356 if ( isset( $recursionGuard[$code] ) ) {
1357 throw new MWException( "Error: Circular fallback reference in language code $code" );
1359 $recursionGuard[$code] = true;
1360 $newDeps = self
::loadLocalisation( $fallback );
1361 unset( $recursionGuard[$code] );
1363 $secondary = self
::$mLocalisationCache[$fallback];
1364 $deps = array_merge( $deps, $newDeps );
1366 # Merge the fallback localisation with the current localisation
1367 foreach ( self
::$mLocalisationKeys as $key ) {
1368 if ( isset( $cache[$key] ) ) {
1369 if ( isset( $secondary[$key] ) ) {
1370 if ( in_array( $key, self
::$mMergeableMapKeys ) ) {
1371 $cache[$key] = $cache[$key] +
$secondary[$key];
1372 } elseif ( in_array( $key, self
::$mMergeableListKeys ) ) {
1373 $cache[$key] = array_merge( $secondary[$key], $cache[$key] );
1377 $cache[$key] = $secondary[$key];
1381 # Merge bookstore lists if requested
1382 if ( !empty( $cache['bookstoreList']['inherit'] ) ) {
1383 $cache['bookstoreList'] = array_merge( $cache['bookstoreList'], $secondary['bookstoreList'] );
1385 if ( isset( $cache['bookstoreList']['inherit'] ) ) {
1386 unset( $cache['bookstoreList']['inherit'] );
1390 # Add dependencies to the cache entry
1391 $cache['deps'] = $deps;
1393 # Save to both caches
1394 self
::$mLocalisationCache[$code] = $cache;
1395 if ( !$disableCache ) {
1396 $wgMemc->set( $memcKey, $cache );
1399 wfProfileOut( __METHOD__
);
1404 * Test if a given localisation cache is out of date with respect to the
1405 * source Messages files. This is done automatically for the global cache
1406 * in $wgMemc, but is only done on certain occasions for the serialized
1409 * @param $cache mixed Either a language code or a cache array
1411 static function isLocalisationOutOfDate( $cache ) {
1412 if ( !is_array( $cache ) ) {
1413 self
::loadLocalisation( $cache );
1414 $cache = self
::$mLocalisationCache[$cache];
1417 foreach ( $cache['deps'] as $file => $mtime ) {
1418 if ( filemtime( $file ) > $mtime ) {
1427 * Get the fallback for a given language
1429 static function getFallbackFor( $code ) {
1430 self
::loadLocalisation( $code );
1431 return self
::$mLocalisationCache[$code]['fallback'];
1435 * Get all messages for a given language
1437 static function getMessagesFor( $code ) {
1438 self
::loadLocalisation( $code );
1439 return self
::$mLocalisationCache[$code]['messages'];
1443 * Get a message for a given language
1445 static function getMessageFor( $key, $code ) {
1446 self
::loadLocalisation( $code );
1447 return @self
::$mLocalisationCache[$code]['messages'][$key];
1451 * Load localisation data for this object
1454 if ( !$this->mLoaded
) {
1455 self
::loadLocalisation( $this->getCode() );
1456 $cache =& self
::$mLocalisationCache[$this->getCode()];
1457 foreach ( self
::$mLocalisationKeys as $key ) {
1458 $this->$key = $cache[$key];
1460 $this->mLoaded
= true;
1462 $this->fixUpSettings();
1467 * Do any necessary post-cache-load settings adjustment
1469 function fixUpSettings() {
1470 global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk, $wgMessageCache,
1471 $wgNamespaceAliases, $wgAmericanDates;
1472 wfProfileIn( __METHOD__
);
1473 if ( $wgExtraNamespaces ) {
1474 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames
;
1477 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
1478 if ( $wgMetaNamespaceTalk ) {
1479 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
1481 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
1482 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
1484 # Allow grammar transformations
1485 # Allowing full message-style parsing would make simple requests
1486 # such as action=raw much more expensive than they need to be.
1487 # This will hopefully cover most cases.
1488 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
1489 array( &$this, 'replaceGrammarInNamespace' ), $talk );
1490 $talk = str_replace( ' ', '_', $talk );
1491 $this->namespaceNames
[NS_PROJECT_TALK
] = $talk;
1494 # The above mixing may leave namespaces out of canonical order.
1495 # Re-order by namespace ID number...
1496 ksort( $this->namespaceNames
);
1498 # Put namespace names and aliases into a hashtable.
1499 # If this is too slow, then we should arrange it so that it is done
1500 # before caching. The catch is that at pre-cache time, the above
1501 # class-specific fixup hasn't been done.
1502 $this->mNamespaceIds
= array();
1503 foreach ( $this->namespaceNames
as $index => $name ) {
1504 $this->mNamespaceIds
[$this->lc($name)] = $index;
1506 if ( $this->namespaceAliases
) {
1507 foreach ( $this->namespaceAliases
as $name => $index ) {
1508 $this->mNamespaceIds
[$this->lc($name)] = $index;
1511 if ( $wgNamespaceAliases ) {
1512 foreach ( $wgNamespaceAliases as $name => $index ) {
1513 $this->mNamespaceIds
[$this->lc($name)] = $index;
1517 if ( $this->defaultDateFormat
== 'dmy or mdy' ) {
1518 $this->defaultDateFormat
= $wgAmericanDates ?
'mdy' : 'dmy';
1520 wfProfileOut( __METHOD__
);
1523 function replaceGrammarInNamespace( $m ) {
1524 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
1527 static function getCaseMaps() {
1528 static $wikiUpperChars, $wikiLowerChars;
1530 if ( isset( $wikiUpperChars ) ) {
1531 return array( $wikiUpperChars, $wikiLowerChars );
1534 wfProfileIn( __METHOD__
);
1535 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
1536 if ( $arr === false ) {
1537 throw new MWException(
1538 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
1541 wfProfileOut( __METHOD__
);
1542 return array( $wikiUpperChars, $wikiLowerChars );