Skip to content

Commit

Permalink
Swtich from ::translations() to ::variables()
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora committed Dec 9, 2024
1 parent ea2e7ad commit 14195e3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 50 deletions.
6 changes: 3 additions & 3 deletions config/areas/languages/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
$language = Find::language($code);
$link = '/languages/' . $language->code();
$strings = [];
$foundation = $kirby->defaultLanguage()->translations()->toArray();
$translations = $language->translations()->toArray();
$foundation = $kirby->defaultLanguage()->variables()->toArray();
$variables = $language->variables()->toArray();

// TODO: update following line and adapt for update and
// delete options when `languageVariables.*` permissions available
Expand All @@ -29,7 +29,7 @@
foreach ($foundation as $key => $value) {
$strings[] = [
'key' => $key,
'value' => $translations[$key] ?? null,
'value' => $variables[$key] ?? null,
'options' => [
[
'click' => 'update',
Expand Down
12 changes: 6 additions & 6 deletions src/Cms/AppTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function i18n(): void
) {
$data = [
...$data,
...$language->translations()->toArray()
...$language->variables()->toArray()
];
}

Expand Down Expand Up @@ -140,7 +140,7 @@ public function translation(string|null $locale = null): Translation
if ($language = $this->language($locale)) {
$inject = [
...$inject,
...$language->translations()->toArray()
...$language->variables()->toArray()
];
}

Expand All @@ -166,14 +166,14 @@ public function translations(): Translations
// injects languages translations
if ($languages = $this->languages()) {
foreach ($languages as $language) {
$languageCode = $language->code();
$languageTranslations = $language->translations()->toArray();
$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
35 changes: 24 additions & 11 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Language implements Stringable
protected bool $single;
protected array $slugs;
protected array $smartypants;
protected LanguageTranslations $translations;
protected string|null $url;
protected LanguageVariables $variables;

/**
* Creates a new language object
Expand Down Expand Up @@ -77,7 +77,7 @@ public function __construct(array $props)
$this->locale = [LC_ALL => $this->code];
}

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

/**
Expand Down Expand Up @@ -132,7 +132,7 @@ public function clone(array $props = []): static
'name' => $this->name,
'slugs' => $this->slugs,
'smartypants' => $this->smartypants,
'translations' => $this->translations->toArray(),
'translations' => $this->variables->toArray(),
'url' => $this->url,
], $props));
}
Expand Down Expand Up @@ -224,6 +224,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 @@ -238,7 +239,7 @@ public function delete(): bool
}

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

// if needed, convert content storage to single lang
foreach ($kirby->models() as $model) {
Expand Down Expand Up @@ -483,16 +484,16 @@ public function save(): static
'direction' => $this->direction(),
'locale' => Locale::export($this->locale()),
'name' => $this->name(),
'translations' => $this->translations()->toArray(),
'translations' => $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->translations()->root() !== null) {
$this->translations()->save($data['translations'] ?? []);
if ($this->variables()->root() !== null) {
$this->variables()->save($data['translations'] ?? []);
$data['translations'] = [];
}

Expand Down Expand Up @@ -556,11 +557,14 @@ public function toArray(): array
}

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

/**
Expand Down Expand Up @@ -589,6 +593,7 @@ public function update(array|null $props = null): static


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

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

// validate the language rules after before hook was applied
Expand Down Expand Up @@ -652,4 +657,12 @@ public function variable(string $key, bool $decode = false): LanguageVariable

return new LanguageVariable($this, $key);
}

/**
* Returns the language variables object for this language
*/
public function variables(): LanguageVariables
{
return $this->variables;
}
}
30 changes: 15 additions & 15 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()->toArray();
$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->update(['translations' => $translations]);
$language->update(['variables' => $variables]);

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

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

/**
Expand All @@ -110,19 +110,19 @@ public function key(): string
*/
public function update(string|null $value = null): static
{
$translations = $this->language->translations();
$translations->set($this->key, $value);
$variables = $this->language->variables();
$variables->set($this->key, $value);

$language = $this->language->update(['translations' => $translations->toArray()]);
$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|null
{
return $this->language->translations()->get($this->key);
return $this->language->variables()->get($this->key);
}
}
30 changes: 15 additions & 15 deletions src/Cms/LanguageTranslations.php → src/Cms/LanguageVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Throwable;

/**
* Manages the translations string for a language,
* either from the language file or `translations` root
* Manages the variables string for a language,
* either from the language file or `language:variables` root
* @since 5.0.0
*
* @package Kirby Cms
Expand All @@ -18,7 +18,7 @@
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class LanguageTranslations
class LanguageVariables
{
public function __construct(
protected Language $language,
Expand All @@ -28,28 +28,28 @@ public function __construct(
}

/**
* Deletes the current language translations file
* Deletes the current language variables 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');
throw new Exception('The language variables could not be deleted');
}
}
}

/**
* Returns a single translation string by key
* Returns a single variable string by key
*/
public function get(string $key, string|null $default = null): string|null
{
return $this->data[$key] ?? $default;
}

/**
* Loads the language translations based on custom roots
* Loads the language variables based on custom roots
*/
public function load(): array
{
Expand All @@ -65,13 +65,13 @@ public function load(): array
}

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

if ($root = $this->root()) {
Data::write($root, $this->data);
Expand All @@ -81,7 +81,7 @@ public function save(array $translations = []): static
}

/**
* Returns custom translations root path if defined
* Returns custom variables root path if defined
*/
public function root(): string|null
{
Expand All @@ -93,7 +93,7 @@ public function root(): string|null
}

/**
* Removes a translation key
* Removes a variable key
*
* @return $this
*/
Expand All @@ -104,7 +104,7 @@ public function remove(string $key): static
}

/**
* Sets the translation key
* Sets the variable key
*
* @return $this
*/
Expand All @@ -115,15 +115,15 @@ public function set(string $key, string|null $value = null): static
}

/**
* Returns translations
* Returns variables
*/
public function toArray(): array
{
return $this->data;
}

/**
* Updates the translations data
* Updates the variables data
*/
public function update(array $data = []): static
{
Expand Down

0 comments on commit 14195e3

Please sign in to comment.