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;
31 * These are always UTF-8, they exist only for backwards compatibility
33 $wgInputEncoding = "UTF-8";
34 $wgOutputEncoding = "UTF-8";
36 if( function_exists( 'mb_strtoupper' ) ) {
37 mb_internal_encoding('UTF-8');
40 /* a fake language converter */
43 function FakeConverter($langobj) {$this->mLang
= $langobj;}
44 function convert($t, $i) {return $t;}
45 function parserConvert($t, $p) {return $t;}
46 function getVariants() { return array( $this->mLang
->getCode() ); }
47 function getPreferredVariant() {return $this->mLang
->getCode(); }
48 function findVariantLink(&$l, &$n) {}
49 function getExtraHashOptions() {return '';}
50 function getParsedTitle() {return '';}
51 function markNoConversion($text, $noParse=false) {return $text;}
52 function convertCategoryKey( $key ) {return $key; }
53 function convertLinkToAllVariants($text){ return array( $this->mLang
->getCode() => $text); }
54 function armourMath($text){ return $text; }
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', 'specialPageAliases' );
72 static public $mMergeableMapKeys = array( 'messages', 'namespaceNames', 'mathNames',
73 'dateFormats', 'defaultUserOptionOverrides', 'magicWords' );
75 static public $mMergeableListKeys = array( 'extraUserToggles' );
77 static public $mMergeableAliasListKeys = array( 'specialPageAliases' );
79 static public $mLocalisationCache = array();
81 static public $mWeekdayMsgs = array(
82 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
86 static public $mWeekdayAbbrevMsgs = array(
87 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
90 static public $mMonthMsgs = array(
91 'january', 'february', 'march', 'april', 'may_long', 'june',
92 'july', 'august', 'september', 'october', 'november',
95 static public $mMonthGenMsgs = array(
96 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
97 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
100 static public $mMonthAbbrevMsgs = array(
101 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
102 'sep', 'oct', 'nov', 'dec'
106 * Create a language object for a given language code
108 static function factory( $code ) {
110 static $recursionLevel = 0;
112 if ( $code == 'en' ) {
115 $class = 'Language' . str_replace( '-', '_', ucfirst( $code ) );
116 // Preload base classes to work around APC/PHP5 bug
117 if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
118 include_once("$IP/languages/classes/$class.deps.php");
120 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
121 include_once("$IP/languages/classes/$class.php");
125 if ( $recursionLevel > 5 ) {
126 throw new MWException( "Language fallback loop detected when creating class $class\n" );
129 if( ! class_exists( $class ) ) {
130 $fallback = Language
::getFallbackFor( $code );
132 $lang = Language
::factory( $fallback );
134 $lang->setCode( $code );
142 function __construct() {
143 $this->mConverter
= new FakeConverter($this);
144 // Set the code to the name of the descendant
145 if ( get_class( $this ) == 'Language' ) {
148 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
153 * Hook which will be called if this is the content language.
154 * Descendants can use this to register hook functions or modify globals
156 function initContLang() {}
162 function getDefaultUserOptions() {
163 return User
::getDefaultOptions();
167 * Exports $wgBookstoreListEn
170 function getBookstoreList() {
172 return $this->bookstoreList
;
178 function getNamespaces() {
180 return $this->namespaceNames
;
184 * A convenience function that returns the same thing as
185 * getNamespaces() except with the array values changed to ' '
186 * where it found '_', useful for producing output to be displayed
187 * e.g. in <select> forms.
191 function getFormattedNamespaces() {
192 $ns = $this->getNamespaces();
193 foreach($ns as $k => $v) {
194 $ns[$k] = strtr($v, '_', ' ');
200 * Get a namespace value by key
202 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
203 * echo $mw_ns; // prints 'MediaWiki'
206 * @param int $index the array key of the namespace to return
207 * @return mixed, string if the namespace value exists, otherwise false
209 function getNsText( $index ) {
210 $ns = $this->getNamespaces();
211 return isset( $ns[$index] ) ?
$ns[$index] : false;
215 * A convenience function that returns the same thing as
216 * getNsText() except with '_' changed to ' ', useful for
221 function getFormattedNsText( $index ) {
222 $ns = $this->getNsText( $index );
223 return strtr($ns, '_', ' ');
227 * Get a namespace key by value, case insensetive.
229 * @param string $text
230 * @return mixed An integer if $text is a valid value otherwise false
232 function getNsIndex( $text ) {
234 $lctext = $this->lc($text);
235 return isset( $this->mNamespaceIds
[$lctext] ) ?
$this->mNamespaceIds
[$lctext] : false;
239 * short names for language variants used for language conversion links.
241 * @param string $code
244 function getVariantname( $code ) {
245 return $this->getMessageFromDB( "variantname-$code" );
248 function specialPage( $name ) {
249 $aliases = $this->getSpecialPageAliases();
250 if ( isset( $aliases[$name][0] ) ) {
251 $name = $aliases[$name][0];
253 return $this->getNsText(NS_SPECIAL
) . ':' . $name;
256 function getQuickbarSettings() {
258 return $this->quickbarSettings
;
261 function getSkinNames() {
263 return $this->skinNames
;
266 function getMathNames() {
268 return $this->mathNames
;
271 function getDatePreferences() {
273 return $this->datePreferences
;
276 function getDateFormats() {
278 return $this->dateFormats
;
281 function getDefaultDateFormat() {
283 return $this->defaultDateFormat
;
286 function getDatePreferenceMigrationMap() {
288 return $this->datePreferenceMigrationMap
;
291 function getDefaultUserOptionOverrides() {
293 return $this->defaultUserOptionOverrides
;
296 function getExtraUserToggles() {
298 return $this->extraUserToggles
;
301 function getUserToggle( $tog ) {
302 return $this->getMessageFromDB( "tog-$tog" );
306 * Get language names, indexed by code.
307 * If $customisedOnly is true, only returns codes with a messages file
309 public static function getLanguageNames( $customisedOnly = false ) {
310 global $wgLanguageNames;
311 if ( !$customisedOnly ) {
312 return $wgLanguageNames;
316 $messageFiles = glob( "$IP/languages/messages/Messages*.php" );
318 foreach ( $messageFiles as $file ) {
319 if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
320 $code = str_replace( '_', '-', strtolower( $m[1] ) );
321 if ( isset( $wgLanguageNames[$code] ) ) {
322 $names[$code] = $wgLanguageNames[$code];
330 * Ugly hack to get a message maybe from the MediaWiki namespace, if this
331 * language object is the content or user language.
333 function getMessageFromDB( $msg ) {
334 global $wgContLang, $wgLang;
335 if ( $wgContLang->getCode() == $this->getCode() ) {
337 return wfMsgForContent( $msg );
338 } elseif ( $wgLang->getCode() == $this->getCode() ) {
340 return wfMsg( $msg );
342 # Neither, get from localisation
343 return $this->getMessage( $msg );
347 function getLanguageName( $code ) {
348 global $wgLanguageNames;
349 if ( ! array_key_exists( $code, $wgLanguageNames ) ) {
352 return $wgLanguageNames[$code];
355 function getMonthName( $key ) {
356 return $this->getMessageFromDB( self
::$mMonthMsgs[$key-1] );
359 function getMonthNameGen( $key ) {
360 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key-1] );
363 function getMonthAbbreviation( $key ) {
364 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key-1] );
367 function getWeekdayName( $key ) {
368 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key-1] );
371 function getWeekdayAbbreviation( $key ) {
372 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key-1] );
376 * Used by date() and time() to adjust the time output.
378 * @param int $ts the time in date('YmdHis') format
379 * @param mixed $tz adjust the time by this amount (default false,
380 * mean we get user timecorrection setting)
383 function userAdjust( $ts, $tz = false ) {
384 global $wgUser, $wgLocalTZoffset;
387 $tz = $wgUser->getOption( 'timecorrection' );
390 # minutes and hours differences:
395 # Global offset in minutes.
396 if( isset($wgLocalTZoffset) ) {
397 $hrDiff = $wgLocalTZoffset %
60;
398 $minDiff = $wgLocalTZoffset - ($hrDiff * 60);
400 } elseif ( strpos( $tz, ':' ) !== false ) {
401 $tzArray = explode( ':', $tz );
402 $hrDiff = intval($tzArray[0]);
403 $minDiff = intval($hrDiff < 0 ?
-$tzArray[1] : $tzArray[1]);
405 $hrDiff = intval( $tz );
408 # No difference ? Return time unchanged
409 if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
411 # Generate an adjusted date
413 (int)substr( $ts, 8, 2) ) +
$hrDiff, # Hours
414 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
415 (int)substr( $ts, 12, 2 ), # Seconds
416 (int)substr( $ts, 4, 2 ), # Month
417 (int)substr( $ts, 6, 2 ), # Day
418 (int)substr( $ts, 0, 4 ) ); #Year
419 return date( 'YmdHis', $t );
423 * This is a workalike of PHP's date() function, but with better
424 * internationalisation, a reduced set of format characters, and a better
427 * Supported format characters are dDjlNwzWFmMntLYyaAgGhHiscrU. See the
428 * PHP manual for definitions. There are a number of extensions, which
431 * xn Do not translate digits of the next numeric format character
432 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
433 * xr Use roman numerals for the next numeric format character
435 * xg Genitive month name
437 * Characters enclosed in double quotes will be considered literal (with
438 * the quotes themselves removed). Unmatched quotes will be considered
439 * literal quotes. Example:
441 * "The month is" F => The month is January
444 * Backslash escaping is also supported.
446 * @param string $format
447 * @param string $ts 14-character timestamp
451 function sprintfDate( $format, $ts ) {
457 for ( $p = 0; $p < strlen( $format ); $p++
) {
460 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
461 $code .= $format[++
$p];
472 $rawToggle = !$rawToggle;
478 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
481 $num = substr( $ts, 6, 2 );
484 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
485 $s .= $this->getWeekdayAbbreviation( date( 'w', $unix ) +
1 );
488 $num = intval( substr( $ts, 6, 2 ) );
491 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
492 $s .= $this->getWeekdayName( date( 'w', $unix ) +
1 );
495 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
496 $w = date( 'w', $unix );
500 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
501 $num = date( 'w', $unix );
504 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
505 $num = date( 'z', $unix );
508 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
509 $num = date( 'W', $unix );
512 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
515 $num = substr( $ts, 4, 2 );
518 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
521 $num = intval( substr( $ts, 4, 2 ) );
524 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
525 $num = date( 't', $unix );
528 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
529 $num = date( 'L', $unix );
532 $num = substr( $ts, 0, 4 );
535 $num = substr( $ts, 2, 2 );
538 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
541 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
544 $h = substr( $ts, 8, 2 );
545 $num = $h %
12 ?
$h %
12 : 12;
548 $num = intval( substr( $ts, 8, 2 ) );
551 $h = substr( $ts, 8, 2 );
552 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
555 $num = substr( $ts, 8, 2 );
558 $num = substr( $ts, 10, 2 );
561 $num = substr( $ts, 12, 2 );
564 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
565 $s .= date( 'c', $unix );
568 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
569 $s .= date( 'r', $unix );
572 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
577 if ( $p < strlen( $format ) - 1 ) {
585 if ( $p < strlen( $format ) - 1 ) {
586 $endQuote = strpos( $format, '"', $p +
1 );
587 if ( $endQuote === false ) {
588 # No terminating quote, assume literal "
591 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
595 # Quote at end of string, assume literal "
602 if ( $num !== false ) {
603 if ( $rawToggle ||
$raw ) {
606 } elseif ( $roman ) {
607 $s .= self
::romanNumeral( $num );
610 $s .= $this->formatNum( $num, true );
619 * Roman number formatting up to 3000
621 static function romanNumeral( $num ) {
622 static $table = array(
623 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
624 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
625 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
626 array( '', 'M', 'MM', 'MMM' )
629 $num = intval( $num );
630 if ( $num > 3000 ||
$num <= 0 ) {
635 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
636 if ( $num >= $pow10 ) {
637 $s .= $table[$i][floor($num / $pow10)];
639 $num = $num %
$pow10;
645 * This is meant to be used by time(), date(), and timeanddate() to get
646 * the date preference they're supposed to use, it should be used in
650 * function timeanddate([...], $format = true) {
651 * $datePreference = $this->dateFormat($format);
656 * @param mixed $usePrefs: if true, the user's preference is used
657 * if false, the site/language default is used
658 * if int/string, assumed to be a format.
661 function dateFormat( $usePrefs = true ) {
664 if( is_bool( $usePrefs ) ) {
666 $datePreference = $wgUser->getDatePreference();
668 $options = User
::getDefaultOptions();
669 $datePreference = (string)$options['date'];
672 $datePreference = (string)$usePrefs;
676 if( $datePreference == '' ) {
680 return $datePreference;
685 * @param mixed $ts the time format which needs to be turned into a
686 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
687 * @param bool $adj whether to adjust the time output according to the
688 * user configured offset ($timecorrection)
689 * @param mixed $format true to use user's date format preference
690 * @param string $timecorrection the time offset as returned by
691 * validateTimeZone() in Special:Preferences
694 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
697 $ts = $this->userAdjust( $ts, $timecorrection );
700 $pref = $this->dateFormat( $format );
701 if( $pref == 'default' ||
!isset( $this->dateFormats
["$pref date"] ) ) {
702 $pref = $this->defaultDateFormat
;
704 return $this->sprintfDate( $this->dateFormats
["$pref date"], $ts );
709 * @param mixed $ts the time format which needs to be turned into a
710 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
711 * @param bool $adj whether to adjust the time output according to the
712 * user configured offset ($timecorrection)
713 * @param mixed $format true to use user's date format preference
714 * @param string $timecorrection the time offset as returned by
715 * validateTimeZone() in Special:Preferences
718 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
721 $ts = $this->userAdjust( $ts, $timecorrection );
724 $pref = $this->dateFormat( $format );
725 if( $pref == 'default' ||
!isset( $this->dateFormats
["$pref time"] ) ) {
726 $pref = $this->defaultDateFormat
;
728 return $this->sprintfDate( $this->dateFormats
["$pref time"], $ts );
733 * @param mixed $ts the time format which needs to be turned into a
734 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
735 * @param bool $adj whether to adjust the time output according to the
736 * user configured offset ($timecorrection)
738 * @param mixed $format what format to return, if it's false output the
739 * default one (default true)
740 * @param string $timecorrection the time offset as returned by
741 * validateTimeZone() in Special:Preferences
744 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
747 $ts = $this->userAdjust( $ts, $timecorrection );
750 $pref = $this->dateFormat( $format );
751 if( $pref == 'default' ||
!isset( $this->dateFormats
["$pref both"] ) ) {
752 $pref = $this->defaultDateFormat
;
755 return $this->sprintfDate( $this->dateFormats
["$pref both"], $ts );
758 function getMessage( $key ) {
760 return isset( $this->messages
[$key] ) ?
$this->messages
[$key] : null;
763 function getAllMessages() {
765 return $this->messages
;
768 function iconv( $in, $out, $string ) {
769 # For most languages, this is a wrapper for iconv
770 return iconv( $in, $out, $string );
773 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
774 function ucwordbreaksCallbackAscii($matches){
775 return $this->ucfirst($matches[1]);
778 function ucwordbreaksCallbackMB($matches){
779 return mb_strtoupper($matches[0]);
782 function ucCallback($matches){
783 list( $wikiUpperChars ) = self
::getCaseMaps();
784 return strtr( $matches[1], $wikiUpperChars );
787 function lcCallback($matches){
788 list( , $wikiLowerChars ) = self
::getCaseMaps();
789 return strtr( $matches[1], $wikiLowerChars );
792 function ucwordsCallbackMB($matches){
793 return mb_strtoupper($matches[0]);
796 function ucwordsCallbackWiki($matches){
797 list( $wikiUpperChars ) = self
::getCaseMaps();
798 return strtr( $matches[0], $wikiUpperChars );
801 function ucfirst( $str ) {
802 return self
::uc( $str, true );
805 function uc( $str, $first = false ) {
806 if ( function_exists( 'mb_strtoupper' ) )
808 if ( self
::isMultibyte( $str ) )
809 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
811 return ucfirst( $str );
813 return self
::isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
815 if ( self
::isMultibyte( $str ) ) {
816 list( $wikiUpperChars ) = $this->getCaseMaps();
817 $x = $first ?
'^' : '';
818 return preg_replace_callback(
819 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
820 array($this,"ucCallback"),
824 return $first ?
ucfirst( $str ) : strtoupper( $str );
827 function lcfirst( $str ) {
828 return self
::lc( $str, true );
831 function lc( $str, $first = false ) {
832 if ( function_exists( 'mb_strtolower' ) )
834 if ( self
::isMultibyte( $str ) )
835 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
837 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
839 return self
::isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
841 if ( self
::isMultibyte( $str ) ) {
842 list( , $wikiLowerChars ) = self
::getCaseMaps();
843 $x = $first ?
'^' : '';
844 return preg_replace_callback(
845 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
846 array($this,"lcCallback"),
850 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
853 function isMultibyte( $str ) {
854 return (bool)preg_match( '/[\x80-\xff]/', $str );
857 function ucwords($str) {
858 if ( self
::isMultibyte( $str ) ) {
859 $str = self
::lc($str);
861 // regexp to find first letter in each word (i.e. after each space)
862 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
864 // function to use to capitalize a single char
865 if ( function_exists( 'mb_strtoupper' ) )
866 return preg_replace_callback(
868 array($this,"ucwordsCallbackMB"),
872 return preg_replace_callback(
874 array($this,"ucwordsCallbackWiki"),
879 return ucwords( strtolower( $str ) );
882 # capitalize words at word breaks
883 function ucwordbreaks($str){
884 if (self
::isMultibyte( $str ) ) {
885 $str = self
::lc($str);
887 // since \b doesn't work for UTF-8, we explicitely define word break chars
888 $breaks= "[ \-\(\)\}\{\.,\?!]";
890 // find first letter after word break
891 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
893 if ( function_exists( 'mb_strtoupper' ) )
894 return preg_replace_callback(
896 array($this,"ucwordbreaksCallbackMB"),
900 return preg_replace_callback(
902 array($this,"ucwordsCallbackWiki"),
907 return preg_replace_callback(
908 '/\b([\w\x80-\xff]+)\b/',
909 array($this,"ucwordbreaksCallbackAscii"),
914 * Return a case-folded representation of $s
916 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
917 * and $s2 are the same except for the case of their characters. It is not
918 * necessary for the value returned to make sense when displayed.
920 * Do *not* perform any other normalisation in this function. If a caller
921 * uses this function when it should be using a more general normalisation
922 * function, then fix the caller.
924 function caseFold( $s ) {
925 return $this->uc( $s );
928 function checkTitleEncoding( $s ) {
929 if( is_array( $s ) ) {
930 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
932 # Check for non-UTF-8 URLs
933 $ishigh = preg_match( '/[\x80-\xff]/', $s);
934 if(!$ishigh) return $s;
936 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
937 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
938 if( $isutf8 ) return $s;
940 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
943 function fallback8bitEncoding() {
945 return $this->fallback8bitEncoding
;
949 * Some languages have special punctuation to strip out
950 * or characters which need to be converted for MySQL's
951 * indexing to grok it correctly. Make such changes here.
956 function stripForSearch( $string ) {
957 # MySQL fulltext index doesn't grok utf-8, so we
958 # need to fold cases and convert to hex
960 wfProfileIn( __METHOD__
);
961 if( function_exists( 'mb_strtolower' ) ) {
963 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
964 "'U8' . bin2hex( \"$1\" )",
965 mb_strtolower( $string ) );
967 list( , $wikiLowerChars ) = self
::getCaseMaps();
969 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
970 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
973 wfProfileOut( __METHOD__
);
977 function convertForSearchResult( $termsArray ) {
978 # some languages, e.g. Chinese, need to do a conversion
979 # in order for search results to be displayed correctly
984 * Get the first character of a string.
989 function firstChar( $s ) {
990 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
991 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
993 return isset( $matches[1] ) ?
$matches[1] : "";
996 function initEncoding() {
997 # Some languages may have an alternate char encoding option
998 # (Esperanto X-coding, Japanese furigana conversion, etc)
999 # If this language is used as the primary content language,
1000 # an override to the defaults can be set here on startup.
1003 function recodeForEdit( $s ) {
1004 # For some languages we'll want to explicitly specify
1005 # which characters make it into the edit box raw
1006 # or are converted in some way or another.
1007 # Note that if wgOutputEncoding is different from
1008 # wgInputEncoding, this text will be further converted
1009 # to wgOutputEncoding.
1010 global $wgEditEncoding;
1011 if( $wgEditEncoding == '' or
1012 $wgEditEncoding == 'UTF-8' ) {
1015 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
1019 function recodeInput( $s ) {
1020 # Take the previous into account.
1021 global $wgEditEncoding;
1022 if($wgEditEncoding != "") {
1023 $enc = $wgEditEncoding;
1027 if( $enc == 'UTF-8' ) {
1030 return $this->iconv( $enc, 'UTF-8', $s );
1035 * For right-to-left language support
1045 * A hidden direction mark (LRM or RLM), depending on the language direction
1049 function getDirMark() {
1050 return $this->isRTL() ?
"\xE2\x80\x8F" : "\xE2\x80\x8E";
1054 * An arrow, depending on the language direction
1058 function getArrow() {
1059 return $this->isRTL() ?
'←' : '→';
1063 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
1067 function linkPrefixExtension() {
1069 return $this->linkPrefixExtension
;
1072 function &getMagicWords() {
1074 return $this->magicWords
;
1077 # Fill a MagicWord object with data from here
1078 function getMagic( &$mw ) {
1079 if ( !isset( $this->mMagicExtensions
) ) {
1080 $this->mMagicExtensions
= array();
1081 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
1083 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
1084 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
1086 $magicWords =& $this->getMagicWords();
1087 if ( isset( $magicWords[$mw->mId
] ) ) {
1088 $rawEntry = $magicWords[$mw->mId
];
1090 # Fall back to English if local list is incomplete
1091 $magicWords =& Language
::getMagicWords();
1092 $rawEntry = $magicWords[$mw->mId
];
1096 if( !is_array( $rawEntry ) ) {
1097 error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
1099 $mw->mCaseSensitive
= $rawEntry[0];
1100 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
1104 * Get special page names, as an associative array
1105 * case folded alias => real name
1107 function getSpecialPageAliases() {
1109 if ( !isset( $this->mExtendedSpecialPageAliases
) ) {
1110 $this->mExtendedSpecialPageAliases
= $this->specialPageAliases
;
1111 wfRunHooks( 'LangugeGetSpecialPageAliases',
1112 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
1114 return $this->mExtendedSpecialPageAliases
;
1118 * Italic is unsuitable for some languages
1122 * @param string $text The text to be emphasized.
1125 function emphasize( $text ) {
1126 return "<em>$text</em>";
1130 * Normally we output all numbers in plain en_US style, that is
1131 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
1132 * point twohundredthirtyfive. However this is not sutable for all
1133 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
1134 * Icelandic just want to use commas instead of dots, and dots instead
1135 * of commas like "293.291,235".
1137 * An example of this function being called:
1139 * wfMsg( 'message', $wgLang->formatNum( $num ) )
1142 * See LanguageGu.php for the Gujarati implementation and
1143 * LanguageIs.php for the , => . and . => , implementation.
1145 * @todo check if it's viable to use localeconv() for the decimal
1148 * @param mixed $number the string to be formatted, should be an integer or
1149 * a floating point number.
1150 * @param bool $nocommafy Set to true for special numbers like dates
1153 function formatNum( $number, $nocommafy = false ) {
1154 global $wgTranslateNumerals;
1156 $number = $this->commafy($number);
1157 $s = $this->separatorTransformTable();
1158 if (!is_null($s)) { $number = strtr($number, $s); }
1161 if ($wgTranslateNumerals) {
1162 $s = $this->digitTransformTable();
1163 if (!is_null($s)) { $number = strtr($number, $s); }
1170 * Adds commas to a given number
1175 function commafy($_) {
1176 return strrev((string)preg_replace('/(\d{3})(?=\d)(?!\d*\.)/','$1,',strrev($_)));
1179 function digitTransformTable() {
1181 return $this->digitTransformTable
;
1184 function separatorTransformTable() {
1186 return $this->separatorTransformTable
;
1191 * For the credit list in includes/Credits.php (action=credits)
1196 function listToText( $l ) {
1199 for ($i = $m; $i >= 0; $i--) {
1202 } else if ($i == $m - 1) {
1203 $s = $l[$i] . ' ' . $this->getMessageFromDB( 'and' ) . ' ' . $s;
1205 $s = $l[$i] . ', ' . $s;
1211 # Crop a string from the beginning or end to a certain number of bytes.
1212 # (Bytes are used because our storage has limited byte lengths for some
1213 # columns in the database.) Multibyte charsets will need to make sure that
1214 # only whole characters are included!
1216 # $length does not include the optional ellipsis.
1217 # If $length is negative, snip from the beginning
1218 function truncate( $string, $length, $ellipsis = "" ) {
1219 if( $length == 0 ) {
1222 if ( strlen( $string ) <= abs( $length ) ) {
1226 $string = substr( $string, 0, $length );
1227 $char = ord( $string[strlen( $string ) - 1] );
1228 if ($char >= 0xc0) {
1229 # We got the first byte only of a multibyte char; remove it.
1230 $string = substr( $string, 0, -1 );
1231 } elseif( $char >= 0x80 &&
1232 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
1233 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
1234 # We chopped in the middle of a character; remove it
1237 return $string . $ellipsis;
1239 $string = substr( $string, $length );
1240 $char = ord( $string[0] );
1241 if( $char >= 0x80 && $char < 0xc0 ) {
1242 # We chopped in the middle of a character; remove the whole thing
1243 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
1245 return $ellipsis . $string;
1250 * Grammatical transformations, needed for inflected languages
1251 * Invoked by putting {{grammar:case|word}} in a message
1253 * @param string $word
1254 * @param string $case
1257 function convertGrammar( $word, $case ) {
1258 global $wgGrammarForms;
1259 if ( isset($wgGrammarForms['en'][$case][$word]) ) {
1260 return $wgGrammarForms['en'][$case][$word];
1266 * Plural form transformations, needed for some languages.
1267 * For example, where are 3 form of plural in Russian and Polish,
1268 * depending on "count mod 10". See [[w:Plural]]
1269 * For English it is pretty simple.
1271 * Invoked by putting {{plural:count|wordform1|wordform2}}
1272 * or {{plural:count|wordform1|wordform2|wordform3}}
1274 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
1276 * @param integer $count
1277 * @param string $wordform1
1278 * @param string $wordform2
1279 * @param string $wordform3 (optional)
1280 * @param string $wordform4 (optional)
1281 * @param string $wordform5 (optional)
1284 function convertPlural( $count, $w1, $w2, $w3, $w4, $w5) {
1285 return $count == '1' ?
$w1 : $w2;
1289 * For translaing of expiry times
1290 * @param string The validated block time in English
1291 * @return Somehow translated block time
1292 * @see LanguageFi.php for example implementation
1294 function translateBlockExpiry( $str ) {
1296 $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
1298 if ( $scBlockExpiryOptions == '-') {
1302 foreach (explode(',', $scBlockExpiryOptions) as $option) {
1303 if ( strpos($option, ":") === false )
1305 list($show, $value) = explode(":", $option);
1306 if ( strcmp ( $str, $value) == 0 )
1307 return '<span title="' . htmlspecialchars($str). '">' .
1308 htmlspecialchars( trim( $show ) ) . '</span>';
1315 * languages like Chinese need to be segmented in order for the diff
1318 * @param string $text
1321 function segmentForDiff( $text ) {
1326 * and unsegment to show the result
1328 * @param string $text
1331 function unsegmentForDiff( $text ) {
1335 # convert text to different variants of a language.
1336 function convert( $text, $isTitle = false) {
1337 return $this->mConverter
->convert($text, $isTitle);
1340 # Convert text from within Parser
1341 function parserConvert( $text, &$parser ) {
1342 return $this->mConverter
->parserConvert( $text, $parser );
1345 # Check if this is a language with variants
1346 function hasVariants(){
1347 return sizeof($this->getVariants())>1;
1350 # Put custom tags (e.g. -{ }-) around math to prevent conversion
1351 function armourMath($text){
1352 return $this->mConverter
->armourMath($text);
1357 * Perform output conversion on a string, and encode for safe HTML output.
1358 * @param string $text
1359 * @param bool $isTitle -- wtf?
1361 * @todo this should get integrated somewhere sane
1363 function convertHtml( $text, $isTitle = false ) {
1364 return htmlspecialchars( $this->convert( $text, $isTitle ) );
1367 function convertCategoryKey( $key ) {
1368 return $this->mConverter
->convertCategoryKey( $key );
1372 * get the list of variants supported by this langauge
1373 * see sample implementation in LanguageZh.php
1375 * @return array an array of language codes
1377 function getVariants() {
1378 return $this->mConverter
->getVariants();
1382 function getPreferredVariant( $fromUser = true ) {
1383 return $this->mConverter
->getPreferredVariant( $fromUser );
1387 * if a language supports multiple variants, it is
1388 * possible that non-existing link in one variant
1389 * actually exists in another variant. this function
1390 * tries to find it. See e.g. LanguageZh.php
1392 * @param string $link the name of the link
1393 * @param mixed $nt the title object of the link
1394 * @return null the input parameters may be modified upon return
1396 function findVariantLink( &$link, &$nt ) {
1397 $this->mConverter
->findVariantLink($link, $nt);
1401 * If a language supports multiple variants, converts text
1402 * into an array of all possible variants of the text:
1403 * 'variant' => text in that variant
1406 function convertLinkToAllVariants($text){
1407 return $this->mConverter
->convertLinkToAllVariants($text);
1412 * returns language specific options used by User::getPageRenderHash()
1413 * for example, the preferred language variant
1418 function getExtraHashOptions() {
1419 return $this->mConverter
->getExtraHashOptions();
1423 * for languages that support multiple variants, the title of an
1424 * article may be displayed differently in different variants. this
1425 * function returns the apporiate title defined in the body of the article.
1429 function getParsedTitle() {
1430 return $this->mConverter
->getParsedTitle();
1434 * Enclose a string with the "no conversion" tag. This is used by
1435 * various functions in the Parser
1437 * @param string $text text to be tagged for no conversion
1438 * @return string the tagged text
1440 function markNoConversion( $text, $noParse=false ) {
1441 return $this->mConverter
->markNoConversion( $text, $noParse );
1445 * A regular expression to match legal word-trailing characters
1446 * which should be merged onto a link of the form [[foo]]bar.
1451 function linkTrail() {
1453 return $this->linkTrail
;
1456 function getLangObj() {
1461 * Get the RFC 3066 code for this language object
1463 function getCode() {
1464 return $this->mCode
;
1467 function setCode( $code ) {
1468 $this->mCode
= $code;
1471 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
1472 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
1475 static function getMessagesFileName( $code ) {
1477 return self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
1480 static function getClassFileName( $code ) {
1482 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
1485 static function getLocalisationArray( $code, $disableCache = false ) {
1486 self
::loadLocalisation( $code, $disableCache );
1487 return self
::$mLocalisationCache[$code];
1491 * Load localisation data for a given code into the static cache
1493 * @return array Dependencies, map of filenames to mtimes
1495 static function loadLocalisation( $code, $disableCache = false ) {
1496 static $recursionGuard = array();
1500 throw new MWException( "Invalid language code requested" );
1503 if ( !$disableCache ) {
1504 # Try the per-process cache
1505 if ( isset( self
::$mLocalisationCache[$code] ) ) {
1506 return self
::$mLocalisationCache[$code]['deps'];
1509 wfProfileIn( __METHOD__
);
1511 # Try the serialized directory
1512 $cache = wfGetPrecompiledData( self
::getFileName( "Messages", $code, '.ser' ) );
1514 self
::$mLocalisationCache[$code] = $cache;
1515 wfDebug( "Got localisation for $code from precompiled data file\n" );
1516 wfProfileOut( __METHOD__
);
1517 return self
::$mLocalisationCache[$code]['deps'];
1520 # Try the global cache
1521 $memcKey = wfMemcKey('localisation', $code );
1522 $cache = $wgMemc->get( $memcKey );
1525 # Check file modification times
1526 foreach ( $cache['deps'] as $file => $mtime ) {
1527 if ( !file_exists( $file ) ||
filemtime( $file ) > $mtime ) {
1532 if ( self
::isLocalisationOutOfDate( $cache ) ) {
1533 $wgMemc->delete( $memcKey );
1535 wfDebug( "Localisation cache for $code had expired due to update of $file\n" );
1537 self
::$mLocalisationCache[$code] = $cache;
1538 wfDebug( "Got localisation for $code from cache\n" );
1539 wfProfileOut( __METHOD__
);
1540 return $cache['deps'];
1544 wfProfileIn( __METHOD__
);
1547 # Default fallback, may be overridden when the messages file is included
1548 if ( $code != 'en' ) {
1554 # Load the primary localisation from the source file
1555 $filename = self
::getMessagesFileName( $code );
1556 if ( !file_exists( $filename ) ) {
1557 wfDebug( "No localisation file for $code, using implicit fallback to en\n" );
1561 $deps = array( $filename => filemtime( $filename ) );
1562 require( $filename );
1563 $cache = compact( self
::$mLocalisationKeys );
1564 wfDebug( "Got localisation for $code from source\n" );
1567 if ( !empty( $fallback ) ) {
1568 # Load the fallback localisation, with a circular reference guard
1569 if ( isset( $recursionGuard[$code] ) ) {
1570 throw new MWException( "Error: Circular fallback reference in language code $code" );
1572 $recursionGuard[$code] = true;
1573 $newDeps = self
::loadLocalisation( $fallback, $disableCache );
1574 unset( $recursionGuard[$code] );
1576 $secondary = self
::$mLocalisationCache[$fallback];
1577 $deps = array_merge( $deps, $newDeps );
1579 # Merge the fallback localisation with the current localisation
1580 foreach ( self
::$mLocalisationKeys as $key ) {
1581 if ( isset( $cache[$key] ) ) {
1582 if ( isset( $secondary[$key] ) ) {
1583 if ( in_array( $key, self
::$mMergeableMapKeys ) ) {
1584 $cache[$key] = $cache[$key] +
$secondary[$key];
1585 } elseif ( in_array( $key, self
::$mMergeableListKeys ) ) {
1586 $cache[$key] = array_merge( $secondary[$key], $cache[$key] );
1587 } elseif ( in_array( $key, self
::$mMergeableAliasListKeys ) ) {
1588 $cache[$key] = array_merge_recursive( $cache[$key], $secondary[$key] );
1592 $cache[$key] = $secondary[$key];
1596 # Merge bookstore lists if requested
1597 if ( !empty( $cache['bookstoreList']['inherit'] ) ) {
1598 $cache['bookstoreList'] = array_merge( $cache['bookstoreList'], $secondary['bookstoreList'] );
1600 if ( isset( $cache['bookstoreList']['inherit'] ) ) {
1601 unset( $cache['bookstoreList']['inherit'] );
1605 # Add dependencies to the cache entry
1606 $cache['deps'] = $deps;
1608 # Replace spaces with underscores in namespace names
1609 $cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
1611 # Save to both caches
1612 self
::$mLocalisationCache[$code] = $cache;
1613 if ( !$disableCache ) {
1614 $wgMemc->set( $memcKey, $cache );
1617 wfProfileOut( __METHOD__
);
1622 * Test if a given localisation cache is out of date with respect to the
1623 * source Messages files. This is done automatically for the global cache
1624 * in $wgMemc, but is only done on certain occasions for the serialized
1627 * @param $cache mixed Either a language code or a cache array
1629 static function isLocalisationOutOfDate( $cache ) {
1630 if ( !is_array( $cache ) ) {
1631 self
::loadLocalisation( $cache );
1632 $cache = self
::$mLocalisationCache[$cache];
1635 foreach ( $cache['deps'] as $file => $mtime ) {
1636 if ( !file_exists( $file ) ||
filemtime( $file ) > $mtime ) {
1645 * Get the fallback for a given language
1647 static function getFallbackFor( $code ) {
1648 self
::loadLocalisation( $code );
1649 return self
::$mLocalisationCache[$code]['fallback'];
1653 * Get all messages for a given language
1655 static function getMessagesFor( $code ) {
1656 self
::loadLocalisation( $code );
1657 return self
::$mLocalisationCache[$code]['messages'];
1661 * Get a message for a given language
1663 static function getMessageFor( $key, $code ) {
1664 self
::loadLocalisation( $code );
1665 return isset( self
::$mLocalisationCache[$code]['messages'][$key] ) ? self
::$mLocalisationCache[$code]['messages'][$key] : null;
1669 * Load localisation data for this object
1672 if ( !$this->mLoaded
) {
1673 self
::loadLocalisation( $this->getCode() );
1674 $cache =& self
::$mLocalisationCache[$this->getCode()];
1675 foreach ( self
::$mLocalisationKeys as $key ) {
1676 $this->$key = $cache[$key];
1678 $this->mLoaded
= true;
1680 $this->fixUpSettings();
1685 * Do any necessary post-cache-load settings adjustment
1687 function fixUpSettings() {
1688 global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk, $wgMessageCache,
1689 $wgNamespaceAliases, $wgAmericanDates;
1690 wfProfileIn( __METHOD__
);
1691 if ( $wgExtraNamespaces ) {
1692 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames
;
1695 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
1696 if ( $wgMetaNamespaceTalk ) {
1697 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
1699 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
1700 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
1702 # Allow grammar transformations
1703 # Allowing full message-style parsing would make simple requests
1704 # such as action=raw much more expensive than they need to be.
1705 # This will hopefully cover most cases.
1706 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
1707 array( &$this, 'replaceGrammarInNamespace' ), $talk );
1708 $talk = str_replace( ' ', '_', $talk );
1709 $this->namespaceNames
[NS_PROJECT_TALK
] = $talk;
1712 # The above mixing may leave namespaces out of canonical order.
1713 # Re-order by namespace ID number...
1714 ksort( $this->namespaceNames
);
1716 # Put namespace names and aliases into a hashtable.
1717 # If this is too slow, then we should arrange it so that it is done
1718 # before caching. The catch is that at pre-cache time, the above
1719 # class-specific fixup hasn't been done.
1720 $this->mNamespaceIds
= array();
1721 foreach ( $this->namespaceNames
as $index => $name ) {
1722 $this->mNamespaceIds
[$this->lc($name)] = $index;
1724 if ( $this->namespaceAliases
) {
1725 foreach ( $this->namespaceAliases
as $name => $index ) {
1726 $this->mNamespaceIds
[$this->lc($name)] = $index;
1729 if ( $wgNamespaceAliases ) {
1730 foreach ( $wgNamespaceAliases as $name => $index ) {
1731 $this->mNamespaceIds
[$this->lc($name)] = $index;
1735 if ( $this->defaultDateFormat
== 'dmy or mdy' ) {
1736 $this->defaultDateFormat
= $wgAmericanDates ?
'mdy' : 'dmy';
1738 wfProfileOut( __METHOD__
);
1741 function replaceGrammarInNamespace( $m ) {
1742 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
1745 static function getCaseMaps() {
1746 static $wikiUpperChars, $wikiLowerChars;
1747 if ( isset( $wikiUpperChars ) ) {
1748 return array( $wikiUpperChars, $wikiLowerChars );
1751 wfProfileIn( __METHOD__
);
1752 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
1753 if ( $arr === false ) {
1754 throw new MWException(
1755 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
1758 wfProfileOut( __METHOD__
);
1759 return array( $wikiUpperChars, $wikiLowerChars );