From c3bc647aceadbd5a0c47260a67cc749e9420e0be Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Fri, 13 Sep 2024 20:04:18 +0300 Subject: [PATCH] Improvements --- src/Cms/Language.php | 15 +- src/Cms/LanguageTranslations.php | 236 +++++++++++++++---------------- src/Cms/LanguageVariable.php | 4 +- 3 files changed, 121 insertions(+), 134 deletions(-) diff --git a/src/Cms/Language.php b/src/Cms/Language.php index 26941e1137..9020c89e56 100644 --- a/src/Cms/Language.php +++ b/src/Cms/Language.php @@ -236,6 +236,9 @@ public function delete(): bool throw new Exception('The language could not be deleted'); } + // delete custom translations file if defined + $language->translations()->delete(); + // if needed, convert content storage to single lang foreach ($kirby->models() as $model) { if ($language->isLast() === true) { @@ -472,7 +475,6 @@ public function save(): static { try { $existingData = Data::read($this->root()); - $data['translations'] = $this->translations()->load($existingData['translations'] ?? []); } catch (Throwable) { $existingData = []; } @@ -588,15 +590,6 @@ public function update(array|null $props = null): static // make sure the slug is nice and clean $props['slug'] = Str::slug($props['slug'] ?? null); - $kirby = App::instance(); - $updated = $this->clone($props); - - if (isset($props['translations']) === true) { - $updated->translations = new LanguageTranslations($updated, $props['translations']); - } - - // validate the updated language - LanguageRules::update($updated); // trigger before hook $language = $kirby->apply( @@ -612,7 +605,7 @@ public function update(array|null $props = null): static $language = $language->clone($props); if (isset($props['translations']) === true) { - $language->translations = $props['translations']; + $language->translations = $language->translations->update($props['translations'] ?? null); } // validate the language rules after before hook was applied diff --git a/src/Cms/LanguageTranslations.php b/src/Cms/LanguageTranslations.php index 6ef7b8fb6f..e998506792 100644 --- a/src/Cms/LanguageTranslations.php +++ b/src/Cms/LanguageTranslations.php @@ -2,13 +2,14 @@ namespace Kirby\Cms; -use Exception; use Kirby\Data\Data; +use Kirby\Exception\Exception; use Kirby\Filesystem\F; +use Throwable; /** - * With helper methods provides get language translations or - * loads from custom `translations` root + * Manages the translations string for a language, + * either from the language file or `translations` root * @since 5.0.0 * * @package Kirby Cms @@ -19,122 +20,115 @@ */ class LanguageTranslations { - protected array $data; - - public function __construct( - protected Language $language, - self|array $translations = [] - ) { - $this->setTranslations($translations); - } - - /** - * Returns a single translation string by key - */ - public function get(string $key, string $default = null): string|null - { - return $this->data[$key] ?? $default; - } - - /** - * Loads the language translations based on custom roots for provided language code - */ - public function load(array $default = []): array - { - if ($file = static::root()) { - try { - return Data::read($file); - } catch (Exception) { - return $default; - } - } - - return $default; - } - - /** - * Saves the language translations in the custom root - * @internal - * - * @return $this - */ - public function save(array $translations = []): static - { - $this->setTranslations($translations); - - if ($root = $this->root()) { - Data::write($root, $translations); - } - - return $this; - } - - /** - * Returns custom translations root path if defined - */ - public function root(): string|null - { - $kirby = App::instance(); - $root = $kirby->root('translations'); - $file = ($root ?? '') . '/' . $this->language->code() . '.php'; - - if ( - $root !== null && - F::exists($file) === true - ) { - return $file; - } - - return null; - } - - /** - * Removes a translation key - * - * @return $this - */ - public function remove(string $key): static - { - unset($this->data[$key]); - return $this; - } - - /** - * Sets the translation key - * - * @return $this - */ - public function set(string $key, string|null $value = null): static - { - $this->data[$key] = $value; - return $this; - } - - /** - * Set translations - * - * @return $this - */ - public function setTranslations(self|array $translations = []): static - { - if (empty($translations) === false) { - if ($translations instanceof self) { - $this->data = $translations->toArray(); - } else { - $this->data = $translations; - } - } else { - $this->data = static::load(); - } - - return $this; - } - - /** - * Returns translations - */ - public function toArray(): array - { - return $this->data; - } + public function __construct( + protected Language $language, + protected array $data = [] + ) { + $this->data = [...$this->load(), ...$this->data]; + } + + /** + * Deletes the current language translations file + * if custom root defined + */ + public function delete(): void + { + if ($file = $this->root()) { + if (F::remove($file) !== true) { + throw new Exception('The language translations could not be deleted'); + } + } + } + + /** + * Returns a single translation string by key + */ + public function get(string $key, string $default = null): string|null + { + return $this->data[$key] ?? $default; + } + + /** + * Loads the language translations based on custom roots + */ + public function load(): array + { + if ($file = static::root()) { + try { + return Data::read($file); + } catch (Throwable) { + // skip when an exception thrown + } + } + + return []; + } + + /** + * Saves the language translations in the custom root + * @return $this + * @internal + * + */ + public function save(array $translations = []): static + { + $this->data = $translations; + + if ($root = $this->root()) { + Data::write($root, $this->data); + } + + return $this; + } + + /** + * Returns custom translations root path if defined + */ + public function root(): string|null + { + if ($root = App::instance()->root('translations')) { + return $root . '/' . $this->language->code() . '.php'; + } + + return null; + } + + /** + * Removes a translation key + * + * @return $this + */ + public function remove(string $key): static + { + unset($this->data[$key]); + return $this; + } + + /** + * Sets the translation key + * + * @return $this + */ + public function set(string $key, string|null $value = null): static + { + $this->data[$key] = $value; + return $this; + } + + /** + * Returns translations + */ + public function toArray(): array + { + return $this->data; + } + + /** + * Updates the translations data + */ + public function update(array $data = []): static + { + $this->data = $data; + return $this; + } } diff --git a/src/Cms/LanguageVariable.php b/src/Cms/LanguageVariable.php index 6632353eb8..1e9a175ff4 100644 --- a/src/Cms/LanguageVariable.php +++ b/src/Cms/LanguageVariable.php @@ -74,7 +74,7 @@ public function delete(): bool foreach ($this->kirby->languages() as $language) { $translations = $language->translations(); $translations->remove($this->key); - $language->update(['translations' => $translations]); + $language->update(['translations' => $translations->toArray()]); } return true; @@ -105,7 +105,7 @@ public function update(string|null $value = null): static $translations = $this->language->translations(); $translations->set($this->key, $value); - $language = $this->language->update(['translations' => $translations]); + $language = $this->language->update(['translations' => $translations->toArray()]); return $language->variable($this->key); }