r106496 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106495‎ | r106496 | r106497 >
Date:02:02, 17 December 2011
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
finished adding support for country lists, making backwards compatible with 1.18 since we need this for fundraising servers
Modified paths:
  • /trunk/extensions/cldr/CountryNames.body.php (modified) (history)
  • /trunk/extensions/cldr/LanguageNames.body.php (modified) (history)
  • /trunk/extensions/cldr/cldr.php (modified) (history)

Diff [purge]

Index: trunk/extensions/cldr/CountryNames.body.php
@@ -28,8 +28,8 @@
2929 $names = self::loadLanguage( $code );
3030
3131 // Load missing country names from fallback languages
32 - $fallbacks = Language::getFallbacksFor( $code );
33 - foreach ( $fallbacks as $fallback ) {
 32+ $fallback = $code;
 33+ while ( $fallback = Language::getFallbackFor( $fallback ) ) {
3434 // Overwrite the things in fallback with what we have already
3535 $names = array_merge( self::loadLanguage( $fallback ), $names );
3636 }
@@ -40,6 +40,8 @@
4141 unset( $names['IR'] ); // Iran
4242 unset( $names['SY'] ); // Syria
4343 }
 44+
 45+ return $names;
4446 }
4547
4648 /**
@@ -49,7 +51,39 @@
5052 * @return an associative array of country codes and localized country names
5153 */
5254 private static function loadLanguage( $code ) {
53 - // TODO: Build this.
 55+ if ( !isset( self::$cache[$code] ) ) {
 56+ wfProfileIn( __METHOD__.'-recache' );
 57+
 58+ /* Load override for wrong or missing entries in cldr */
 59+ $override = dirname(__FILE__) . '/LocalNames/' . self::getOverrideFileName( $code );
 60+ if ( Language::isValidBuiltInCode( $code ) && file_exists( $override ) ) {
 61+ $countryNames = false;
 62+ require( $override );
 63+ if ( is_array( $countryNames ) ) {
 64+ self::$cache[$code] = $countryNames;
 65+ }
 66+ }
 67+
 68+ $filename = dirname(__FILE__) . '/CldrNames/' . self::getFileName( $code );
 69+ if ( Language::isValidBuiltInCode( $code ) && file_exists( $filename ) ) {
 70+ $countryNames = false;
 71+ require( $filename );
 72+ if ( is_array( $countryNames ) ) {
 73+ if ( isset( self::$cache[$code] ) ) {
 74+ // Add to existing list of localized country names
 75+ self::$cache[$code] = self::$cache[$code] + $countryNames;
 76+ } else {
 77+ // No list exists, so create it
 78+ self::$cache[$code] = $countryNames;
 79+ }
 80+ }
 81+ } else {
 82+ wfDebug( __METHOD__ . ": Unable to load country names for $filename\n" );
 83+ }
 84+ wfProfileOut( __METHOD__.'-recache' );
 85+ }
 86+
 87+ return isset( self::$cache[$code] ) ? self::$cache[$code] : array();
5488 }
5589
5690 /**
Index: trunk/extensions/cldr/LanguageNames.body.php
@@ -33,9 +33,9 @@
3434 if ( $fbMethod === self::FALLBACK_NATIVE ) {
3535 $names = array_merge( $native, $xx );
3636 } elseif ( $fbMethod === self::FALLBACK_NORMAL ) {
37 - $fallbacks = Language::getFallbacksFor( $code );
 37+ $fallback = $code;
3838 $fb = $xx;
39 - foreach ( $fallbacks as $fallback ) {
 39+ while ( $fallback = Language::getFallbackFor( $fallback ) ) {
4040 /* Overwrite the things in fallback with what we have already */
4141 $fb = array_merge( self::loadLanguage( $fallback ), $fb );
4242 }
Index: trunk/extensions/cldr/cldr.php
@@ -13,8 +13,8 @@
1414 $wgExtensionCredits['other'][] = array(
1515 'path' => __FILE__,
1616 'name' => 'Language Names',
17 - 'version' => '1.9.0 (CLDR 1.9M2)',
18 - 'author' => array( 'Niklas Laxström', 'Siebrand Mazeland' ),
 17+ 'version' => '2.0.0 (CLDR 2.0)',
 18+ 'author' => array( 'Niklas Laxström', 'Siebrand Mazeland', 'Ryan Kaldari' ),
1919 'url' => 'https://www.mediawiki.org/wiki/Extension:CLDR',
2020 'descriptionmsg' => 'cldr-desc',
2121 );
@@ -22,4 +22,5 @@
2323 $dir = dirname(__FILE__) . '/';
2424 $wgExtensionMessagesFiles['cldr'] = $dir . 'cldr.i18n.php';
2525 $wgAutoloadClasses['LanguageNames'] = $dir . 'LanguageNames.body.php';
 26+$wgAutoloadClasses['CountryNames'] = $dir . 'CountryNames.body.php';
2627 $wgHooks['LanguageGetTranslatedLanguageNames'][] = 'LanguageNames::coreHook';

Follow-up revisions

RevisionCommit summaryAuthorDate
r106551follow-up to r106491 and r106496 - fixing for 1.19, moving embargo filter out...kaldari02:59, 18 December 2011

Comments

#Comment by Nikerabbit (talk | contribs)   20:59, 17 December 2011
+		while ( $fallback = Language::getFallbackFor( $fallback ) ) {

But this will cause infinite loop in 1.19.

#Comment by Kaldari (talk | contribs)   21:26, 17 December 2011
( Is there any way to get this to work in both 1.18 and 1.19? Do I need to check for when the fallback == 'en'?
#Comment by Nikerabbit (talk | contribs)   21:35, 17 December 2011

You can use this snippet from Translate extension:

                // BC <1.19
                if ( method_exists( 'Language', 'getFallbacksFor' ) ) {
                        $list = Language::getFallbacksFor( $code );
                        array_pop( $list ); // Get 'en' away from the end
                        $fallbacks = array_merge( $list , $fallbacks );
                } else {
                        $realFallback = $code ? Language::getFallbackFor( $code ) : false;
                        if ( $realFallback && $realFallback !== 'en' ) {
                                $fallbacks = array_merge( array( $realFallback ), $fallbacks );
                        }
                }
#Comment by Nikerabbit (talk | contribs)   21:35, 17 December 2011

Actually you probably want 'en' too, so you need to modify it a bit.

#Comment by Kaldari (talk | contribs)   03:00, 18 December 2011

Fixed in r106551.

Status & tagging log