Improve the readability of the code for CKEditor localizations
authorAlexander Ebert <ebert@woltlab.com>
Wed, 12 Apr 2023 13:22:35 +0000 (15:22 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 12 Apr 2023 15:15:51 +0000 (17:15 +0200)
wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php

index 42d5f872ee5cf13cb8b17097f1d746fb4116c823..fb1bc15739be49a66a6cc916ee6352a9f166fd8d 100644 (file)
@@ -274,7 +274,7 @@ class BBCodeHandler extends SingletonFactory
             'zh',
         ];
 
-        $locale = WCF::getLanguage()->getBcp47();
+        $locale = \strtolower(WCF::getLanguage()->getBcp47());
         if (\in_array($locale, $availableTranslations, true)) {
             return \sprintf(
                 '"ckeditor5-translation/%s",',
@@ -282,19 +282,18 @@ class BBCodeHandler extends SingletonFactory
             );
         }
 
-        $index = \strpos($locale, '-');
-        if ($index !== false) {
-            $locale = \substr($locale, 0, $index);
-
-            if (\in_array($locale, $availableTranslations, true)) {
-                return \sprintf(
-                    '"ckeditor5-translation/%s",',
-                    $locale
-                );
-            }
+        // Some languages offer both specialized variants for certain locales
+        // but also provide a "generic" variant. For example, "en-gb" and "en".
+        [$languageCode] = \explode('-', $locale, 2);
+        if (\in_array($languageCode, $availableTranslations, true)) {
+            return \sprintf(
+                '"ckeditor5-translation/%s",',
+                $languageCode
+            );
         }
 
-
+        // The default locale "en" is part of the generated bundle, we must not
+        // yield any module if this locale is (implicitly) requested.
         return "";
     }
 }