Use fallback languages in ResourceLoaderImage
authorEd Sanders <esanders@wikimedia.org>
Fri, 30 Mar 2018 19:56:59 +0000 (20:56 +0100)
committerEd Sanders <esanders@wikimedia.org>
Sun, 1 Apr 2018 19:41:01 +0000 (20:41 +0100)
Bug: T191120
Change-Id: Ic72aead466c12d344bac359654e6dcc7f0e5c8cb

includes/resourceloader/ResourceLoaderImage.php
tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php

index 072ae79..d38a175 100644 (file)
@@ -130,13 +130,27 @@ class ResourceLoaderImage {
                $desc = $this->descriptor;
                if ( is_string( $desc ) ) {
                        return $this->basePath . '/' . $desc;
-               } elseif ( isset( $desc['lang'][$context->getLanguage()] ) ) {
-                       return $this->basePath . '/' . $desc['lang'][$context->getLanguage()];
-               } elseif ( isset( $desc[$context->getDirection()] ) ) {
+               }
+               if ( isset( $desc['lang'] ) ) {
+                       $contextLang = $context->getLanguage();
+                       if ( isset( $desc['lang'][$contextLang] ) ) {
+                               return $this->basePath . '/' . $desc['lang'][$contextLang];
+                       }
+                       $fallbacks = Language::getFallbacksFor( $contextLang );
+                       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] )
+                               ) {
+                                       return $this->basePath . '/' . $desc['lang'][$lang];
+                               }
+                       }
+               }
+               if ( isset( $desc[$context->getDirection()] ) ) {
                        return $this->basePath . '/' . $desc[$context->getDirection()];
-               } else {
-                       return $this->basePath . '/' . $desc['default'];
                }
+               return $this->basePath . '/' . $desc['default'];
        }
 
        /**
index 838d2c0..35c3ef6 100644 (file)
@@ -39,7 +39,9 @@ class ResourceLoaderImageTest extends ResourceLoaderTestCase {
                        [ 'mno', 'ar', 'mno-rtl.svg' ],
                        [ 'mno', 'he', 'mno-ltr.svg' ],
                        [ 'pqr', 'en', 'pqr-b.svg' ],
+                       [ 'pqr', 'en-gb', 'pqr-b.svg' ],
                        [ 'pqr', 'de', 'pqr-f.svg' ],
+                       [ 'pqr', 'de-formal', 'pqr-f.svg' ],
                        [ 'pqr', 'ar', 'pqr-f.svg' ],
                        [ 'pqr', 'fr', 'pqr-a.svg' ],
                        [ 'pqr', 'he', 'pqr-a.svg' ],
@@ -53,7 +55,9 @@ class ResourceLoaderImageTest extends ResourceLoaderTestCase {
        public function testGetPath( $imageName, $languageCode, $path ) {
                static $dirMap = [
                        'en' => 'ltr',
+                       'en-gb' => 'ltr',
                        'de' => 'ltr',
+                       'de-formal' => 'ltr',
                        'fr' => 'ltr',
                        'he' => 'rtl',
                        'ar' => 'rtl',