Index: trunk/phase3/includes/Xml.php |
— | — | @@ -190,29 +190,13 @@ |
191 | 191 | * |
192 | 192 | * @param string $selected The language code of the selected language |
193 | 193 | * @param boolean $customisedOnly If true only languages which have some content are listed |
194 | | - * @param string $language The ISO code of the language to display the select list in (optional) |
| 194 | + * @param string $inLanguage The ISO code of the language to display the select list in (optional) |
195 | 195 | * @return array containing 2 items: label HTML and select list HTML |
196 | 196 | */ |
197 | | - public static function languageSelector( $selected, $customisedOnly = true, $language = null ) { |
| 197 | + public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null ) { |
198 | 198 | global $wgLanguageCode; |
199 | 199 | |
200 | | - // TODO: This should be replaced with a hook or something, from r107002 |
201 | | - // If a specific language was requested and CLDR is installed, use it |
202 | | - if ( $language && is_callable( array( 'LanguageNames', 'getNames' ) ) ) { |
203 | | - if ( $customisedOnly ) { |
204 | | - $listType = LanguageNames::LIST_MW_SUPPORTED; // Only pull names that have localisation in MediaWiki |
205 | | - } else { |
206 | | - $listType = LanguageNames::LIST_MW; // Pull all languages that are in Names.php |
207 | | - } |
208 | | - // Retrieve the list of languages in the requested language (via CLDR) |
209 | | - $languages = LanguageNames::getNames( |
210 | | - $language, // Code of the requested language |
211 | | - LanguageNames::FALLBACK_NORMAL, // Use fallback chain |
212 | | - $listType |
213 | | - ); |
214 | | - } else { |
215 | | - $languages = Language::getLanguageNames( $customisedOnly ); |
216 | | - } |
| 200 | + $languages = Language::fetchLanguageNames( $inLanguage, $customisedOnly ? 'mwfile' : 'mw' ); |
217 | 201 | |
218 | 202 | // Make sure the site language is in the list; a custom language code might not have a |
219 | 203 | // defined name... |
Index: trunk/phase3/includes/parser/CoreParserFunctions.php |
— | — | @@ -622,21 +622,14 @@ |
623 | 623 | /** |
624 | 624 | * Gives language names. |
625 | 625 | * @param $parser Parser |
626 | | - * @param $code String Language code |
627 | | - * @param $language String Language code |
| 626 | + * @param $code String Language code (of which to get name) |
| 627 | + * @param $inLnguage String Language code (in which to get name) |
628 | 628 | * @return String |
629 | 629 | */ |
630 | | - static function language( $parser, $code = '', $language = '' ) { |
631 | | - global $wgContLang; |
| 630 | + static function language( $parser, $code = '', $inLanguage = '' ) { |
632 | 631 | $code = strtolower( $code ); |
633 | | - $language = strtolower( $language ); |
634 | | - |
635 | | - if ( $language !== '' ) { |
636 | | - $names = Language::getTranslatedLanguageNames( $language ); |
637 | | - return isset( $names[$code] ) ? $names[$code] : wfBCP47( $code ); |
638 | | - } |
639 | | - |
640 | | - $lang = $wgContLang->getLanguageName( $code ); |
| 632 | + $inLanguage = strtolower( $inLanguage ); |
| 633 | + $lang = Language::fetchLanguageName( $code, $inLanguage ); |
641 | 634 | return $lang !== '' ? $lang : wfBCP47( $code ); |
642 | 635 | } |
643 | 636 | |
Index: trunk/phase3/includes/Preferences.php |
— | — | @@ -252,7 +252,7 @@ |
253 | 253 | } |
254 | 254 | |
255 | 255 | // Language |
256 | | - $languages = Language::getLanguageNames( false ); |
| 256 | + $languages = Language::fetchLanguageNames( null, 'mw' ); |
257 | 257 | if ( !array_key_exists( $wgLanguageCode, $languages ) ) { |
258 | 258 | $languages[$wgLanguageCode] = $wgLanguageCode; |
259 | 259 | } |
Index: trunk/phase3/languages/Language.php |
— | — | @@ -659,29 +659,10 @@ |
660 | 660 | * @param $customisedOnly bool |
661 | 661 | * |
662 | 662 | * @return array |
| 663 | + * @deprecated in 1.20, use fetchLanguageNames() |
663 | 664 | */ |
664 | 665 | public static function getLanguageNames( $customisedOnly = false ) { |
665 | | - global $wgExtraLanguageNames; |
666 | | - static $coreLanguageNames; |
667 | | - |
668 | | - if ( $coreLanguageNames === null ) { |
669 | | - include( MWInit::compiledPath( 'languages/Names.php' ) ); |
670 | | - } |
671 | | - |
672 | | - $allNames = $wgExtraLanguageNames + $coreLanguageNames; |
673 | | - if ( !$customisedOnly ) { |
674 | | - return $allNames; |
675 | | - } |
676 | | - |
677 | | - $names = array(); |
678 | | - // We do this using a foreach over the codes instead of a directory |
679 | | - // loop so that messages files in extensions will work correctly. |
680 | | - foreach ( $allNames as $code => $value ) { |
681 | | - if ( is_readable( self::getMessagesFileName( $code ) ) ) { |
682 | | - $names[$code] = $allNames[$code]; |
683 | | - } |
684 | | - } |
685 | | - return $names; |
| 666 | + return self::fetchLanguageNames( null, $customisedOnly ? 'mwfile' : 'mw' ); |
686 | 667 | } |
687 | 668 | |
688 | 669 | /** |
— | — | @@ -691,19 +672,86 @@ |
692 | 673 | * @param $code String Language code. |
693 | 674 | * @return Array language code => language name |
694 | 675 | * @since 1.18.0 |
| 676 | + * @deprecated in 1.20, use fetchLanguageNames() |
695 | 677 | */ |
696 | 678 | public static function getTranslatedLanguageNames( $code ) { |
| 679 | + return self::fetchLanguageNames( $code, 'all' ); |
| 680 | + } |
| 681 | + |
| 682 | + |
| 683 | + /* |
| 684 | + * Get an array of language names, indexed by code. |
| 685 | + * @param $inLanguage null|string: Code of language in which to return the names |
| 686 | + * Use null for autonyms (native names) |
| 687 | + * @param $include string: |
| 688 | + * 'all' all available languages |
| 689 | + * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames |
| 690 | + * 'mwfile' only if the language is in 'mw' *and* has a message file |
| 691 | + * @return array|false: language code => language name, false if $include is wrong |
| 692 | + */ |
| 693 | + public static function fetchLanguageNames( $inLanguage = null, $include = 'all' ) { |
| 694 | + global $wgExtraLanguageNames; |
| 695 | + static $coreLanguageNames; |
| 696 | + |
| 697 | + if ( $coreLanguageNames === null ) { |
| 698 | + include( MWInit::compiledPath( 'languages/Names.php' ) ); |
| 699 | + } |
| 700 | + |
697 | 701 | $names = array(); |
698 | | - wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) ); |
699 | 702 | |
700 | | - foreach ( self::getLanguageNames() as $code => $name ) { |
701 | | - if ( !isset( $names[$code] ) ) $names[$code] = $name; |
| 703 | + if( $inLanguage ) { |
| 704 | + # TODO: also include when $inLanguage is null, when this code is more efficient |
| 705 | + wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) ); |
702 | 706 | } |
703 | 707 | |
704 | | - return $names; |
| 708 | + $mwNames = $wgExtraLanguageNames + $coreLanguageNames; |
| 709 | + foreach ( $mwNames as $mwCode => $mwName ) { |
| 710 | + # - Prefer own MediaWiki native name when not using the hook |
| 711 | + # TODO: prefer it always to make it consistent, but casing is different in CLDR |
| 712 | + # - For other names just add if not added through the hook |
| 713 | + if ( ( $mwCode === $inLanguage && !$inLanguage ) || !isset( $names[$mwCode] ) ) { |
| 714 | + $names[$mwCode] = $mwName; |
| 715 | + } |
| 716 | + } |
| 717 | + |
| 718 | + if ( $include === 'all' ) { |
| 719 | + return $names; |
| 720 | + } |
| 721 | + |
| 722 | + $returnMw = array(); |
| 723 | + $coreCodes = array_keys( $mwNames ); |
| 724 | + foreach( $coreCodes as $coreCode ) { |
| 725 | + $returnMw[$coreCode] = $names[$coreCode]; |
| 726 | + } |
| 727 | + |
| 728 | + if( $include === 'mw' ) { |
| 729 | + return $returnMw; |
| 730 | + } elseif( $include === 'mwfile' ) { |
| 731 | + $namesMwFile = array(); |
| 732 | + # We do this using a foreach over the codes instead of a directory |
| 733 | + # loop so that messages files in extensions will work correctly. |
| 734 | + foreach ( $returnMw as $code => $value ) { |
| 735 | + if ( is_readable( self::getMessagesFileName( $code ) ) ) { |
| 736 | + $namesMwFile[$code] = $names[$code]; |
| 737 | + } |
| 738 | + } |
| 739 | + return $namesMwFile; |
| 740 | + } |
| 741 | + return false; |
705 | 742 | } |
706 | 743 | |
707 | 744 | /** |
| 745 | + * @param $code string: The code of the language for which to get the name |
| 746 | + * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms) |
| 747 | + * @return string: Language name or empty |
| 748 | + * @since 1.20 |
| 749 | + */ |
| 750 | + public static function fetchLanguageName( $code, $inLanguage = null ) { |
| 751 | + $array = self::fetchLanguageNames( $inLanguage, 'all' ); |
| 752 | + return !array_key_exists( $code, $array ) ? '' : $array[$code]; |
| 753 | + } |
| 754 | + |
| 755 | + /** |
708 | 756 | * Get a message from the MediaWiki namespace. |
709 | 757 | * |
710 | 758 | * @param $msg String: message name |
— | — | @@ -718,13 +766,10 @@ |
719 | 767 | * Only if defined in MediaWiki, no other data like CLDR. |
720 | 768 | * @param $code string |
721 | 769 | * @return string |
| 770 | + * @deprecated in 1.20, use fetchLanguageName() |
722 | 771 | */ |
723 | 772 | function getLanguageName( $code ) { |
724 | | - $names = self::getLanguageNames(); |
725 | | - if ( !array_key_exists( $code, $names ) ) { |
726 | | - return ''; |
727 | | - } |
728 | | - return $names[$code]; |
| 773 | + return self::fetchLanguageName( $code ); |
729 | 774 | } |
730 | 775 | |
731 | 776 | /** |
Index: trunk/extensions/Translate/TranslateUtils.php |
— | — | @@ -187,8 +187,8 @@ |
188 | 188 | * @return array |
189 | 189 | */ |
190 | 190 | public static function getLanguageNames( /*string */ $code ) { |
191 | | - if ( is_callable( array( 'Language', 'getTranslatedLanguageNames' ) ) ) { |
192 | | - return Language::getTranslatedLanguageNames( $code ); |
| 191 | + if ( is_callable( array( 'Language', 'fetchLanguageNames' ) ) ) { |
| 192 | + return Language::fetchLanguageNames( $code, 'mw' ); // since 1.20 |
193 | 193 | } elseif ( is_callable( array( 'LanguageNames', 'getNames' ) ) ) { |
194 | 194 | return LanguageNames::getNames( $code, |
195 | 195 | LanguageNames::FALLBACK_NORMAL, |