Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Aug 11, 2023
1 parent 4f1619b commit a690513
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 68 deletions.
48 changes: 1 addition & 47 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
39 changes: 18 additions & 21 deletions src/web/assets/BaseCkeditorPackageAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit a690513

Please sign in to comment.