From 8477d4a6f5949fcb875fb0d21800866bc548fc05 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Tue, 3 Apr 2018 22:08:52 +0100 Subject: [PATCH] Define which languages explicitly fallback to 'en' In the message store, all messages fall through to English, but only a few languages should actually explicitly fallback to English (English variants and dialects). These new explicit fallbacks are used by ResourceLoaderImageModule, and this change doesn't affect the message fall through system. Bug: T203350 Change-Id: I6b68a17f4d69341bccdae748727b5133a600d8bc --- .../cache/localisation/LocalisationCache.php | 22 +++++++----- .../resourceloader/ResourceLoaderImage.php | 8 ++--- languages/Language.php | 36 ++++++++++++++++--- languages/messages/MessagesBi.php | 2 ++ languages/messages/MessagesEn_ca.php | 11 ++++++ languages/messages/MessagesEn_gb.php | 2 ++ languages/messages/MessagesJam.php | 11 ++++++ languages/messages/MessagesPih.php | 11 ++++++ languages/messages/MessagesSco.php | 2 ++ 9 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 languages/messages/MessagesEn_ca.php create mode 100644 languages/messages/MessagesJam.php create mode 100644 languages/messages/MessagesPih.php diff --git a/includes/cache/localisation/LocalisationCache.php b/includes/cache/localisation/LocalisationCache.php index 9cf7acf6f1..d0381cf04d 100644 --- a/includes/cache/localisation/LocalisationCache.php +++ b/includes/cache/localisation/LocalisationCache.php @@ -838,17 +838,23 @@ class LocalisationCache { } # Fill in the fallback if it's not there already - if ( is_null( $coreData['fallback'] ) ) { - $coreData['fallback'] = $code === 'en' ? false : 'en'; - } - if ( $coreData['fallback'] === false ) { - $coreData['fallbackSequence'] = []; + if ( ( is_null( $coreData['fallback'] ) || $coreData['fallback'] === false ) && $code === 'en' ) { + $coreData['fallback'] = false; + $coreData['originalFallbackSequence'] = $coreData['fallbackSequence'] = []; } else { - $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) ); + if ( !is_null( $coreData['fallback'] ) ) { + $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) ); + } else { + $coreData['fallbackSequence'] = []; + } $len = count( $coreData['fallbackSequence'] ); - # Ensure that the sequence ends at en - if ( $coreData['fallbackSequence'][$len - 1] !== 'en' ) { + # Before we add the 'en' fallback for messages, keep a copy of + # the original fallback sequence + $coreData['originalFallbackSequence'] = $coreData['fallbackSequence']; + + # Ensure that the sequence ends at 'en' for messages + if ( !$len || $coreData['fallbackSequence'][$len - 1] !== 'en' ) { $coreData['fallbackSequence'][] = 'en'; } } diff --git a/includes/resourceloader/ResourceLoaderImage.php b/includes/resourceloader/ResourceLoaderImage.php index 0adbd0cb38..d9c369dd02 100644 --- a/includes/resourceloader/ResourceLoaderImage.php +++ b/includes/resourceloader/ResourceLoaderImage.php @@ -140,13 +140,9 @@ class ResourceLoaderImage { if ( isset( $desc['lang'][$contextLang] ) ) { return $this->basePath . '/' . $desc['lang'][$contextLang]; } - $fallbacks = Language::getFallbacksFor( $contextLang ); + $fallbacks = Language::getFallbacksFor( $contextLang, Language::STRICT_FALLBACKS ); foreach ( $fallbacks as $lang ) { - // Images will fallback to 'default' instead of 'en', except for 'en-*' variants - if ( - ( $lang !== 'en' || substr( $contextLang, 0, 3 ) === 'en-' ) && - isset( $desc['lang'][$lang] ) - ) { + if ( isset( $desc['lang'][$lang] ) ) { return $this->basePath . '/' . $desc['lang'][$lang]; } } diff --git a/languages/Language.php b/languages/Language.php index 34edb753b5..5897241039 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -80,6 +80,18 @@ class Language { static public $mLangObjCache = []; + /** + * Return a fallback chain for messages in getFallbacksFor + * @since 1.32 + */ + const MESSAGES_FALLBACKS = 0; + + /** + * Return a strict fallback chain in getFallbacksFor + * @since 1.32 + */ + const STRICT_FALLBACKS = 1; + static public $mWeekdayMsgs = [ 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' @@ -4588,15 +4600,29 @@ class Language { * * @since 1.19 * @param string $code Language code - * @return array Non-empty array, ending in "en" + * @param int $mode Fallback mode, either MESSAGES_FALLBACKS (which always falls back to 'en'), + * or STRICT_FALLBACKS (whic honly falls back to 'en' when explicitly defined) + * @throws MWException + * @return array List of language codes */ - public static function getFallbacksFor( $code ) { + public static function getFallbacksFor( $code, $mode = self::MESSAGES_FALLBACKS ) { if ( $code === 'en' || !self::isValidBuiltInCode( $code ) ) { return []; } - // For unknown languages, fallbackSequence returns an empty array, - // hardcode fallback to 'en' in that case. - return self::getLocalisationCache()->getItem( $code, 'fallbackSequence' ) ?: [ 'en' ]; + switch ( $mode ) { + case self::MESSAGES_FALLBACKS: + // For unknown languages, fallbackSequence returns an empty array, + // hardcode fallback to 'en' in that case as English messages are + // always defined. + return self::getLocalisationCache()->getItem( $code, 'fallbackSequence' ) ?: [ 'en' ]; + case self::STRICT_FALLBACKS: + // Use this mode when you don't want to fallback to English unless + // explicitly defined, for example when you have language-variant icons + // and an international language-independent fallback. + return self::getLocalisationCache()->getItem( $code, 'originalFallbackSequence' ); + default: + throw new MWException( "Invalid fallback mode \"$mode\"" ); + } } /** diff --git a/languages/messages/MessagesBi.php b/languages/messages/MessagesBi.php index 2c61b70445..eebe4fdf90 100644 --- a/languages/messages/MessagesBi.php +++ b/languages/messages/MessagesBi.php @@ -7,3 +7,5 @@ * @file * */ + +$fallback = 'en'; diff --git a/languages/messages/MessagesEn_ca.php b/languages/messages/MessagesEn_ca.php new file mode 100644 index 0000000000..698541fc63 --- /dev/null +++ b/languages/messages/MessagesEn_ca.php @@ -0,0 +1,11 @@ + [ 'UncategorisedCategories' ], 'Uncategorizedimages' => [ 'UncategorisedFiles', 'UncategorisedImages' ], diff --git a/languages/messages/MessagesJam.php b/languages/messages/MessagesJam.php new file mode 100644 index 0000000000..d19904865a --- /dev/null +++ b/languages/messages/MessagesJam.php @@ -0,0 +1,11 @@ + 'Media', NS_SPECIAL => 'Special', -- 2.20.1