Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora committed Sep 13, 2024
1 parent 437745f commit a18e0ef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 55 deletions.
15 changes: 4 additions & 11 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -472,7 +475,6 @@ public function save(): static
{
try {
$existingData = Data::read($this->root());
$data['translations'] = $this->translations()->load($existingData['translations'] ?? []);
} catch (Throwable) {
$existingData = [];
}
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
78 changes: 36 additions & 42 deletions src/Cms/LanguageTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,13 +20,24 @@
*/
class LanguageTranslations
{
protected array $data;

public function __construct(
protected Language $language,
self|array $translations = []
protected array $data = []
) {
$this->setTranslations($translations);
$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');
}
}
}

/**
Expand All @@ -37,33 +49,33 @@ public function get(string $key, string $default = null): string|null
}

/**
* Loads the language translations based on custom roots for provided language code
* Loads the language translations based on custom roots
*/
public function load(array $default = []): array
public function load(): array
{
if ($file = static::root()) {
try {
return Data::read($file);
} catch (Exception) {
return $default;
} catch (Throwable) {
// skip when an exception thrown
}
}

return $default;
return [];
}

/**
* Saves the language translations in the custom root
* @return $this
* @internal
*
* @return $this
*/
public function save(array $translations = []): static
{
$this->setTranslations($translations);
$this->data = $translations;

if ($root = $this->root()) {
Data::write($root, $translations);
Data::write($root, $this->data);
}

return $this;
Expand All @@ -74,15 +86,8 @@ public function save(array $translations = []): static
*/
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;
if ($root = App::instance()->root('translations')) {
return $root . '/' . $this->language->code() . '.php';
}

return null;
Expand Down Expand Up @@ -111,30 +116,19 @@ public function set(string $key, string|null $value = null): static
}

/**
* Set translations
*
* @return $this
* Returns translations
*/
public function setTranslations(self|array $translations = []): static
public function toArray(): array
{
if (empty($translations) === false) {
if ($translations instanceof self) {
$this->data = $translations->toArray();
} else {
$this->data = $translations;
}
} else {
$this->data = static::load();
}

return $this;
return $this->data;
}

/**
* Returns translations
* Updates the translations data
*/
public function toArray(): array
public function update(array $data = []): static
{
return $this->data;
$this->data = $data;
return $this;
}
}
4 changes: 2 additions & 2 deletions src/Cms/LanguageVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit a18e0ef

Please sign in to comment.