Define which languages explicitly fallback to 'en'
authorEd Sanders <esanders@wikimedia.org>
Tue, 3 Apr 2018 21:08:52 +0000 (22:08 +0100)
committerJames D. Forrester <jforrester@wikimedia.org>
Wed, 19 Sep 2018 22:55:38 +0000 (15:55 -0700)
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

includes/cache/localisation/LocalisationCache.php
includes/resourceloader/ResourceLoaderImage.php
languages/Language.php
languages/messages/MessagesBi.php
languages/messages/MessagesEn_ca.php [new file with mode: 0644]
languages/messages/MessagesEn_gb.php
languages/messages/MessagesJam.php [new file with mode: 0644]
languages/messages/MessagesPih.php [new file with mode: 0644]
languages/messages/MessagesSco.php

index 9cf7acf..d0381cf 100644 (file)
@@ -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';
                        }
                }
index 0adbd0c..d9c369d 100644 (file)
@@ -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];
                                }
                        }
index 34edb75..5897241 100644 (file)
@@ -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\"" );
+               }
        }
 
        /**
index 2c61b70..eebe4fd 100644 (file)
@@ -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 (file)
index 0000000..698541f
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+/** Canadian English (Canadian English)
+ *
+ * To improve a translation please visit https://translatewiki.net
+ *
+ * @ingroup Language
+ * @file
+ *
+ */
+
+$fallback = 'en';
index 61addfa..df906d5 100644 (file)
@@ -8,6 +8,8 @@
  *
  */
 
+$fallback = 'en';
+
 $specialPageAliases = [
        'Uncategorizedcategories'   => [ 'UncategorisedCategories' ],
        'Uncategorizedimages'       => [ 'UncategorisedFiles', 'UncategorisedImages' ],
diff --git a/languages/messages/MessagesJam.php b/languages/messages/MessagesJam.php
new file mode 100644 (file)
index 0000000..d199048
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+/** Jamaican Creole English (Patois)
+ *
+ * To improve a translation please visit https://translatewiki.net
+ *
+ * @ingroup Language
+ * @file
+ *
+ */
+
+$fallback = 'en';
diff --git a/languages/messages/MessagesPih.php b/languages/messages/MessagesPih.php
new file mode 100644 (file)
index 0000000..25f62b2
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+/** Pitkern (Pitkern)
+ *
+ * To improve a translation please visit https://translatewiki.net
+ *
+ * @ingroup Language
+ * @file
+ *
+ */
+
+$fallback = 'en';
index 558f6ab..1069bbb 100644 (file)
@@ -8,6 +8,8 @@
  *
  */
 
+$fallback = 'en';
+
 $namespaceNames = [
        NS_MEDIA            => 'Media',
        NS_SPECIAL          => 'Special',