From a6905137945f5ac844e22311849c5f56ad889b81 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Fri, 11 Aug 2023 16:19:32 -0700 Subject: [PATCH] Cleanup --- src/Field.php | 48 +-------------------- src/web/assets/BaseCkeditorPackageAsset.php | 39 ++++++++--------- 2 files changed, 19 insertions(+), 68 deletions(-) diff --git a/src/Field.php b/src/Field.php index a4a318c..81ab9a8 100644 --- a/src/Field.php +++ b/src/Field.php @@ -314,10 +314,7 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st ], ], 'language' => [ - 'ui' => match (Craft::$app->language) { - 'nn' => 'no', - default => strtolower(Craft::$app->language), - }, + 'ui' => BaseCkeditorPackageAsset::uiLanguage(), 'content' => $element?->getSite()->language ?? Craft::$app->language, ], 'linkOptions' => $this->_linkOptions($element), @@ -433,8 +430,6 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st ]); } - $this->_registerTranslations($baseConfig, $ckeConfig, $view); - return Html::tag('div', $html, [ 'class' => array_filter([ $this->showWordCount ? 'ck-with-show-word-count' : null, @@ -770,45 +765,4 @@ private function _transforms(): array 'name' => $transform->name, ])->values()->all(); } - - /** - * Register native CKEditor translation files based on config. - * For example, if the app language is "en" you should still be able to use "pl" for the CKEditor UI - * if you specify that in the CKE config; - * It also allows you to have the cke field language dependent on the config, - * and not always on the app and site languages - * - * @param array $baseConfig - * @param CkeConfig $ckeConfig - * @param View $view - * @return void - * @throws \yii\base\InvalidConfigException - */ - private function _registerTranslations(array $baseConfig, CkeConfig $ckeConfig, View $view): void - { - $languages = []; - - // if ckeConfig has language info, it should overwrite the baseConfig - if (isset($ckeConfig->options, $ckeConfig->options['language'])) { - // if it's a string like: "language": "pl", then the language should be used for both ui and content - if (is_string($ckeConfig->options['language'])) { - $languages[] = $ckeConfig->options['language']; - } else { - // if it's an array, then we want to use $ckeConfig if it exists and fall back on $baseConfig if it doesn't - $languages[] = $ckeConfig->options['language']['ui'] ?? $baseConfig['language']['ui']; - $languages[] = $ckeConfig->options['language']['content'] ?? $baseConfig['language']['content']; - } - } else { - $languages[] = $baseConfig['language']['ui']; - $languages[] = $baseConfig['language']['content']; - } - - $languages = array_unique($languages); - - $am = $view->getAssetManager(); - /** @var BaseCkeditorPackageAsset $bundle */ - $bundle = $am->getBundle(CkeditorAsset::class); - - $bundle->includeFieldTranslations($languages); - } } diff --git a/src/web/assets/BaseCkeditorPackageAsset.php b/src/web/assets/BaseCkeditorPackageAsset.php index 1d09a52..ef036f8 100644 --- a/src/web/assets/BaseCkeditorPackageAsset.php +++ b/src/web/assets/BaseCkeditorPackageAsset.php @@ -23,6 +23,20 @@ */ abstract class BaseCkeditorPackageAsset extends AssetBundle { + /** + * Returns the CKEditor UI language that should be used based on the app language. + * + * @return string + * @since 3.5.0 + */ + public static function uiLanguage(): string + { + return match (Craft::$app->language) { + 'nb', 'nn' => 'no', // https://github.com/craftcms/ckeditor/issues/113 + default => strtolower(Craft::$app->language), + }; + } + /** * @var string[] List of CKEditor plugins’ names that should be loaded by default. * @@ -62,32 +76,15 @@ public function registerAssetFiles($view): void } } - /** - * Include native CKEditor translation files based on the CKE config - * - * @param array $languages - * @return void - */ - public function includeFieldTranslations(array $languages): void - { - foreach ($languages as $language) { - $this->includeTranslationForLanguage($language); - } - } - - private function includeTranslation(): void { - $language = match (Craft::$app->language) { - 'en', 'en-US' => false, - 'nn' => 'no', - default => strtolower(Craft::$app->language), - }; - - if ($language === false) { + if (in_array(Craft::$app->language, ['en', 'en-US'])) { + // that's what the source files use return; } + $language = static::uiLanguage(); + if ($this->includeTranslationForLanguage($language)) { return; }