Skip to content

Commit 1cea8dd

Browse files
afboradistantnative
authored andcommitted
New language:variables root
1 parent dbd4d5a commit 1cea8dd

File tree

6 files changed

+202
-35
lines changed

6 files changed

+202
-35
lines changed

config/areas/languages/views.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
$language = Find::language($code);
1818
$link = '/languages/' . $language->code();
1919
$strings = [];
20-
$foundation = $kirby->defaultLanguage()->translations();
21-
$translations = $language->translations();
20+
$foundation = $kirby->defaultLanguage()->variables()->toArray();
21+
$variables = $language->variables()->toArray();
2222

2323
// TODO: update following line and adapt for update and
2424
// delete options when `languageVariables.*` permissions available
@@ -29,7 +29,7 @@
2929
foreach ($foundation as $key => $value) {
3030
$strings[] = [
3131
'key' => $key,
32-
'value' => $translations[$key] ?? null,
32+
'value' => $variables[$key] ?? null,
3333
'options' => [
3434
[
3535
'click' => 'update',

src/Cms/AppTranslations.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ protected function i18n(): void
3131
$this->multilang() === true &&
3232
$language = $this->languages()->find($locale)
3333
) {
34-
$data = [...$data, ...$language->translations()];
34+
$data = [
35+
...$data,
36+
...$language->variables()->toArray()
37+
];
3538
}
3639

3740

@@ -139,7 +142,10 @@ public function translation(string|null $locale = null): Translation
139142

140143
// inject current language translations
141144
if ($language = $this->language($locale)) {
142-
$inject = [...$inject, ...$language->translations()];
145+
$inject = [
146+
...$inject,
147+
...$language->variables()->toArray()
148+
];
143149
}
144150

145151
// load from disk instead
@@ -164,14 +170,14 @@ public function translations(): Translations
164170
// injects languages translations
165171
if ($languages = $this->languages()) {
166172
foreach ($languages as $language) {
167-
$languageCode = $language->code();
168-
$languageTranslations = $language->translations();
173+
$languageCode = $language->code();
174+
$languageVariables = $language->variables()->toArray();
169175

170176
// merges language translations with extensions translations
171-
if (empty($languageTranslations) === false) {
177+
if (empty($languageVariables) === false) {
172178
$translations[$languageCode] = [
173179
...$translations[$languageCode] ?? [],
174-
...$languageTranslations
180+
...$languageVariables
175181
];
176182
}
177183
}

src/Cms/Core.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ public function roots(): array
357357
'commands' => fn (array $roots) => $roots['site'] . '/commands',
358358
'config' => fn (array $roots) => $roots['site'] . '/config',
359359
'controllers' => fn (array $roots) => $roots['site'] . '/controllers',
360+
'language:variables' => null,
360361
'languages' => fn (array $roots) => $roots['site'] . '/languages',
361362
'licenses' => fn (array $roots) => $roots['site'] . '/licenses',
362363
'license' => fn (array $roots) => $roots['config'] . '/.license',

src/Cms/Language.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Kirby\Toolkit\Locale;
1111
use Kirby\Toolkit\Str;
1212
use Stringable;
13+
use Throwable;
1314

1415
/**
1516
* The `$language` object represents
@@ -51,8 +52,8 @@ class Language implements Stringable
5152
protected bool $single;
5253
protected array $slugs;
5354
protected array $smartypants;
54-
protected array $translations;
5555
protected string|null $url;
56+
protected LanguageVariables $variables;
5657

5758
/**
5859
* Creates a new language object
@@ -73,14 +74,15 @@ public function __construct(array $props)
7374
$this->single = $props['single'] ?? false;
7475
$this->slugs = $props['slugs'] ?? [];
7576
$this->smartypants = $props['smartypants'] ?? [];
76-
$this->translations = $props['translations'] ?? [];
7777
$this->url = $props['url'] ?? null;
7878

7979
if ($locale = $props['locale'] ?? null) {
8080
$this->locale = Locale::normalize($locale);
8181
} else {
8282
$this->locale = [LC_ALL => $this->code];
8383
}
84+
85+
$this->variables = new LanguageVariables($this, $props['variables'] ?? $props['translations'] ?? []);
8486
}
8587

8688
/**
@@ -135,7 +137,7 @@ public function clone(array $props = []): static
135137
'name' => $this->name,
136138
'slugs' => $this->slugs,
137139
'smartypants' => $this->smartypants,
138-
'translations' => $this->translations,
140+
'variables' => $this->variables->toArray(),
139141
'url' => $this->url,
140142
], $props));
141143
}
@@ -225,6 +227,7 @@ public function delete(): bool
225227
LanguageRules::delete($this);
226228

227229
// apply before hook
230+
/** @var \Kirby\Cms\Language $language */
228231
$language = $kirby->apply(
229232
'language.delete:before',
230233
['language' => $this]
@@ -237,6 +240,9 @@ public function delete(): bool
237240
throw new Exception(message: 'The language could not be deleted');
238241
}
239242

243+
// delete custom translations file if defined
244+
$language->variables()->delete();
245+
240246
// if needed, convert content storage to single lang
241247
foreach ($kirby->models() as $model) {
242248
if ($language->isLast() === true) {
@@ -483,7 +489,11 @@ public function rules(): array
483489
*/
484490
public function save(): static
485491
{
486-
$existingData = Data::read($this->root(), fail: false);
492+
try {
493+
$existingData = Data::read($this->root(), fail: false);
494+
} catch (Throwable) {
495+
$existingData = [];
496+
}
487497

488498
$data = [
489499
...$existingData,
@@ -492,12 +502,19 @@ public function save(): static
492502
'direction' => $this->direction(),
493503
'locale' => Locale::export($this->locale()),
494504
'name' => $this->name(),
495-
'translations' => $this->translations(),
505+
'variables' => $this->variables()->toArray(),
496506
'url' => $this->url,
497507
];
498508

499509
ksort($data);
500510

511+
// save translations to the custom root and remove translations
512+
// to prevent duplication write into the language file
513+
if ($this->variables()->root() !== null) {
514+
$this->variables()->save($data['translations'] ?? []);
515+
$data['translations'] = [];
516+
}
517+
501518
Data::write($this->root(), $data);
502519

503520
return $this;
@@ -558,11 +575,14 @@ public function toArray(): array
558575
}
559576

560577
/**
561-
* Returns the translation strings for this language
578+
* Alias for Language::variables()
579+
*
580+
* @deprecated 5.0.0 Use `::variables()` instead
581+
* @todo 7.0.0 Remove the method
562582
*/
563-
public function translations(): array
583+
public function translations(): LanguageVariables
564584
{
565-
return $this->translations;
585+
return $this->variables();
566586
}
567587

568588
/**
@@ -589,6 +609,7 @@ public function update(array|null $props = null): static
589609
$props['slug'] = Str::slug($props['slug'] ?? null);
590610

591611
// trigger before hook
612+
/** @var \Kirby\Cms\Language $language */
592613
$language = $kirby->apply(
593614
'language.update:before',
594615
[
@@ -601,7 +622,7 @@ public function update(array|null $props = null): static
601622
$language = $language->clone($props);
602623

603624
if (isset($props['translations']) === true) {
604-
$language->translations = $props['translations'];
625+
$language->variables = $language->variables->update($props['translations'] ?? null);
605626
}
606627

607628
// validate the language rules after before hook was applied
@@ -655,4 +676,12 @@ public function variable(
655676
key: $key
656677
);
657678
}
679+
680+
/**
681+
* Returns the language variables object for this language
682+
*/
683+
public function variables(): LanguageVariables
684+
{
685+
return $this->variables;
686+
}
658687
}

src/Cms/LanguageVariable.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public static function create(
4848
);
4949
}
5050

51-
$kirby = App::instance();
52-
$language = $kirby->defaultLanguage();
53-
$translations = $language->translations();
51+
$kirby = App::instance();
52+
$language = $kirby->defaultLanguage();
53+
$variables = $language->variables()->toArray();
5454

5555
if ($kirby->translation()->get($key) !== null) {
56-
if (isset($translations[$key]) === true) {
56+
if (isset($variables[$key]) === true) {
5757
throw new DuplicateException(
5858
message: 'The variable already exists'
5959
);
@@ -64,9 +64,9 @@ public static function create(
6464
);
6565
}
6666

67-
$translations[$key] = $value ?? '';
67+
$variables[$key] = $value ?? '';
6868

69-
$language = $language->update(['translations' => $translations]);
69+
$language = $language->update(['variables' => $variables]);
7070

7171
return $language->variable($key);
7272
}
@@ -80,11 +80,9 @@ public function delete(): bool
8080
{
8181
// go through all languages and remove the variable
8282
foreach ($this->kirby->languages() as $language) {
83-
$variables = $language->translations();
84-
85-
unset($variables[$this->key]);
86-
87-
$language->update(['translations' => $variables]);
83+
$variables = $language->variables();
84+
$variables->remove($this->key);
85+
$language->update(['variables' => $variables->toArray()]);
8886
}
8987

9088
return true;
@@ -96,7 +94,7 @@ public function delete(): bool
9694
public function exists(): bool
9795
{
9896
$language = $this->kirby->defaultLanguage();
99-
return isset($language->translations()[$this->key]) === true;
97+
return $language->variables()->get($this->key) !== null;
10098
}
10199

102100
/**
@@ -130,19 +128,19 @@ public function language(): Language
130128
*/
131129
public function update(string|array|null $value = null): static
132130
{
133-
$translations = $this->language->translations();
134-
$translations[$this->key] = $value ?? '';
131+
$variables = $this->language->variables();
132+
$variables->set($this->key, $value);
135133

136-
$language = $this->language->update(['translations' => $translations]);
134+
$language = $this->language->update(['variables' => $variables->toArray()]);
137135

138136
return $language->variable($this->key);
139137
}
140138

141139
/**
142-
* Returns the value if the variable has been translated.
140+
* Returns the value if the variable has been translated
143141
*/
144142
public function value(): string|array|null
145143
{
146-
return $this->language->translations()[$this->key] ?? null;
144+
return $this->language->variables()->get($this->key);
147145
}
148146
}

0 commit comments

Comments
 (0)