Skip to content

Commit a18e0ef

Browse files
committed
Improvements
1 parent 437745f commit a18e0ef

File tree

3 files changed

+42
-55
lines changed

3 files changed

+42
-55
lines changed

src/Cms/Language.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ public function delete(): bool
236236
throw new Exception('The language could not be deleted');
237237
}
238238

239+
// delete custom translations file if defined
240+
$language->translations()->delete();
241+
239242
// if needed, convert content storage to single lang
240243
foreach ($kirby->models() as $model) {
241244
if ($language->isLast() === true) {
@@ -472,7 +475,6 @@ public function save(): static
472475
{
473476
try {
474477
$existingData = Data::read($this->root());
475-
$data['translations'] = $this->translations()->load($existingData['translations'] ?? []);
476478
} catch (Throwable) {
477479
$existingData = [];
478480
}
@@ -588,15 +590,6 @@ public function update(array|null $props = null): static
588590
// make sure the slug is nice and clean
589591
$props['slug'] = Str::slug($props['slug'] ?? null);
590592

591-
$kirby = App::instance();
592-
$updated = $this->clone($props);
593-
594-
if (isset($props['translations']) === true) {
595-
$updated->translations = new LanguageTranslations($updated, $props['translations']);
596-
}
597-
598-
// validate the updated language
599-
LanguageRules::update($updated);
600593

601594
// trigger before hook
602595
$language = $kirby->apply(
@@ -612,7 +605,7 @@ public function update(array|null $props = null): static
612605
$language = $language->clone($props);
613606

614607
if (isset($props['translations']) === true) {
615-
$language->translations = $props['translations'];
608+
$language->translations = $language->translations->update($props['translations'] ?? null);
616609
}
617610

618611
// validate the language rules after before hook was applied

src/Cms/LanguageTranslations.php

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Kirby\Cms;
44

5-
use Exception;
65
use Kirby\Data\Data;
6+
use Kirby\Exception\Exception;
77
use Kirby\Filesystem\F;
8+
use Throwable;
89

910
/**
10-
* With helper methods provides get language translations or
11-
* loads from custom `translations` root
11+
* Manages the translations string for a language,
12+
* either from the language file or `translations` root
1213
* @since 5.0.0
1314
*
1415
* @package Kirby Cms
@@ -19,13 +20,24 @@
1920
*/
2021
class LanguageTranslations
2122
{
22-
protected array $data;
23-
2423
public function __construct(
2524
protected Language $language,
26-
self|array $translations = []
25+
protected array $data = []
2726
) {
28-
$this->setTranslations($translations);
27+
$this->data = [...$this->load(), ...$this->data];
28+
}
29+
30+
/**
31+
* Deletes the current language translations file
32+
* if custom root defined
33+
*/
34+
public function delete(): void
35+
{
36+
if ($file = $this->root()) {
37+
if (F::remove($file) !== true) {
38+
throw new Exception('The language translations could not be deleted');
39+
}
40+
}
2941
}
3042

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

3951
/**
40-
* Loads the language translations based on custom roots for provided language code
52+
* Loads the language translations based on custom roots
4153
*/
42-
public function load(array $default = []): array
54+
public function load(): array
4355
{
4456
if ($file = static::root()) {
4557
try {
4658
return Data::read($file);
47-
} catch (Exception) {
48-
return $default;
59+
} catch (Throwable) {
60+
// skip when an exception thrown
4961
}
5062
}
5163

52-
return $default;
64+
return [];
5365
}
5466

5567
/**
5668
* Saves the language translations in the custom root
69+
* @return $this
5770
* @internal
5871
*
59-
* @return $this
6072
*/
6173
public function save(array $translations = []): static
6274
{
63-
$this->setTranslations($translations);
75+
$this->data = $translations;
6476

6577
if ($root = $this->root()) {
66-
Data::write($root, $translations);
78+
Data::write($root, $this->data);
6779
}
6880

6981
return $this;
@@ -74,15 +86,8 @@ public function save(array $translations = []): static
7486
*/
7587
public function root(): string|null
7688
{
77-
$kirby = App::instance();
78-
$root = $kirby->root('translations');
79-
$file = ($root ?? '') . '/' . $this->language->code() . '.php';
80-
81-
if (
82-
$root !== null &&
83-
F::exists($file) === true
84-
) {
85-
return $file;
89+
if ($root = App::instance()->root('translations')) {
90+
return $root . '/' . $this->language->code() . '.php';
8691
}
8792

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

113118
/**
114-
* Set translations
115-
*
116-
* @return $this
119+
* Returns translations
117120
*/
118-
public function setTranslations(self|array $translations = []): static
121+
public function toArray(): array
119122
{
120-
if (empty($translations) === false) {
121-
if ($translations instanceof self) {
122-
$this->data = $translations->toArray();
123-
} else {
124-
$this->data = $translations;
125-
}
126-
} else {
127-
$this->data = static::load();
128-
}
129-
130-
return $this;
123+
return $this->data;
131124
}
132125

133126
/**
134-
* Returns translations
127+
* Updates the translations data
135128
*/
136-
public function toArray(): array
129+
public function update(array $data = []): static
137130
{
138-
return $this->data;
131+
$this->data = $data;
132+
return $this;
139133
}
140134
}

src/Cms/LanguageVariable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function delete(): bool
7474
foreach ($this->kirby->languages() as $language) {
7575
$translations = $language->translations();
7676
$translations->remove($this->key);
77-
$language->update(['translations' => $translations]);
77+
$language->update(['translations' => $translations->toArray()]);
7878
}
7979

8080
return true;
@@ -105,7 +105,7 @@ public function update(string|null $value = null): static
105105
$translations = $this->language->translations();
106106
$translations->set($this->key, $value);
107107

108-
$language = $this->language->update(['translations' => $translations]);
108+
$language = $this->language->update(['translations' => $translations->toArray()]);
109109

110110
return $language->variable($this->key);
111111
}

0 commit comments

Comments
 (0)