Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/Cms/AppTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ protected function i18n(): void
$this->multilang() === true &&
$language = $this->languages()->find($locale)
) {
$data = [...$data, ...$language->translations()];
$data = [
...$data,
...$language->variables()->toArray()
];
}


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

// inject current language translations
if ($language = $this->language($locale)) {
$inject = [...$inject, ...$language->translations()];
$inject = [
...$inject,
...$language->variables()->toArray()
];
}

// load from disk instead
Expand All @@ -164,14 +170,14 @@ public function translations(): Translations
// injects languages translations
if ($languages = $this->languages()) {
foreach ($languages as $language) {
$languageCode = $language->code();
$languageTranslations = $language->translations();
$languageCode = $language->code();
$languageVariables = $language->variables()->toArray();

// merges language translations with extensions translations
if (empty($languageTranslations) === false) {
if (empty($languageVariables) === false) {
$translations[$languageCode] = [
...$translations[$languageCode] ?? [],
...$languageTranslations
...$languageVariables
];
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Cms/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ public function roots(): array
'commands' => fn (array $roots) => $roots['site'] . '/commands',
'config' => fn (array $roots) => $roots['site'] . '/config',
'controllers' => fn (array $roots) => $roots['site'] . '/controllers',
'language:variables' => null,
'languages' => fn (array $roots) => $roots['site'] . '/languages',
'licenses' => fn (array $roots) => $roots['site'] . '/licenses',
'license' => fn (array $roots) => $roots['config'] . '/.license',
Expand Down
47 changes: 38 additions & 9 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Kirby\Toolkit\Locale;
use Kirby\Toolkit\Str;
use Stringable;
use Throwable;

/**
* The `$language` object represents
Expand Down Expand Up @@ -51,8 +52,8 @@ class Language implements Stringable
protected bool $single;
protected array $slugs;
protected array $smartypants;
protected array $translations;
protected string|null $url;
protected LanguageVariables $variables;

/**
* Creates a new language object
Expand All @@ -73,14 +74,15 @@ public function __construct(array $props)
$this->single = $props['single'] ?? false;
$this->slugs = $props['slugs'] ?? [];
$this->smartypants = $props['smartypants'] ?? [];
$this->translations = $props['translations'] ?? [];
$this->url = $props['url'] ?? null;

if ($locale = $props['locale'] ?? null) {
$this->locale = Locale::normalize($locale);
} else {
$this->locale = [LC_ALL => $this->code];
}

$this->variables = new LanguageVariables($this, $props['variables'] ?? $props['translations'] ?? []);
}

/**
Expand Down Expand Up @@ -135,7 +137,7 @@ public function clone(array $props = []): static
'name' => $this->name,
'slugs' => $this->slugs,
'smartypants' => $this->smartypants,
'translations' => $this->translations,
'variables' => $this->variables->toArray(),
'url' => $this->url,
], $props));
}
Expand Down Expand Up @@ -225,6 +227,7 @@ public function delete(): bool
LanguageRules::delete($this);

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

// delete custom translations file if defined
$language->variables()->delete();

// if needed, convert content storage to single lang
foreach ($kirby->models() as $model) {
if ($language->isLast() === true) {
Expand Down Expand Up @@ -483,7 +489,11 @@ public function rules(): array
*/
public function save(): static
{
$existingData = Data::read($this->root(), fail: false);
try {
$existingData = Data::read($this->root(), fail: false);
} catch (Throwable) {
$existingData = [];
}

$data = [
...$existingData,
Expand All @@ -492,12 +502,19 @@ public function save(): static
'direction' => $this->direction(),
'locale' => Locale::export($this->locale()),
'name' => $this->name(),
'translations' => $this->translations(),
'variables' => $this->variables()->toArray(),
'url' => $this->url,
];

ksort($data);

// save translations to the custom root and remove translations
// to prevent duplication write into the language file
if ($this->variables()->root() !== null) {
$this->variables()->save($data['translations'] ?? []);
$data['translations'] = [];
}

Data::write($this->root(), $data);

return $this;
Expand Down Expand Up @@ -558,11 +575,14 @@ public function toArray(): array
}

/**
* Returns the translation strings for this language
* Alias for Language::variables()
*
* @deprecated 5.0.0 Use `::variables()` instead
* @todo 7.0.0 Remove the method
*/
public function translations(): array
public function translations(): LanguageVariables
{
return $this->translations;
return $this->variables();
}

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

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

if (isset($props['translations']) === true) {
$language->translations = $props['translations'];
$language->variables = $language->variables->update($props['translations'] ?? null);
}

// validate the language rules after before hook was applied
Expand Down Expand Up @@ -655,4 +676,12 @@ public function variable(
key: $key
);
}

/**
* Returns the language variables object for this language
*/
public function variables(): LanguageVariables
{
return $this->variables;
}
}
32 changes: 15 additions & 17 deletions src/Cms/LanguageVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public static function create(
);
}

$kirby = App::instance();
$language = $kirby->defaultLanguage();
$translations = $language->translations();
$kirby = App::instance();
$language = $kirby->defaultLanguage();
$variables = $language->variables()->toArray();

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

$translations[$key] = $value ?? '';
$variables[$key] = $value ?? '';

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

return $language->variable($key);
}
Expand All @@ -80,11 +80,9 @@ public function delete(): bool
{
// go through all languages and remove the variable
foreach ($this->kirby->languages() as $language) {
$variables = $language->translations();

unset($variables[$this->key]);

$language->update(['translations' => $variables]);
$variables = $language->variables();
$variables->remove($this->key);
$language->update(['variables' => $variables->toArray()]);
}

return true;
Expand All @@ -96,7 +94,7 @@ public function delete(): bool
public function exists(): bool
{
$language = $this->kirby->defaultLanguage();
return isset($language->translations()[$this->key]) === true;
return $language->variables()->get($this->key) !== null;
}

/**
Expand Down Expand Up @@ -130,19 +128,19 @@ public function language(): Language
*/
public function update(string|array|null $value = null): static
{
$translations = $this->language->translations();
$translations[$this->key] = $value ?? '';
$variables = $this->language->variables();
$variables->set($this->key, $value);

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

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

/**
* Returns the value if the variable has been translated.
* Returns the value if the variable has been translated
*/
public function value(): string|array|null
{
return $this->language->translations()[$this->key] ?? null;
return $this->language->variables()->get($this->key);
}
}
Loading
Loading