-
-
Notifications
You must be signed in to change notification settings - Fork 186
[v4] New translations root
#6216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,7 @@ public function __construct(array $props) | |
| $this->name = trim($props['name'] ?? $this->code); | ||
| $this->slugs = $props['slugs'] ?? []; | ||
| $this->smartypants = $props['smartypants'] ?? []; | ||
| $this->translations = $props['translations'] ?? []; | ||
| $this->translations = (new LanguageTranslations($this))->load($props['translations'] ?? []); | ||
| $this->url = $props['url'] ?? null; | ||
|
|
||
| if ($locale = $props['locale'] ?? null) { | ||
|
|
@@ -409,6 +409,13 @@ public function save(): static | |
| { | ||
| try { | ||
| $existingData = Data::read($this->root()); | ||
|
|
||
| // inject translations from custom root | ||
| // returns existing translations | ||
| // if custom root is not defined as fallback | ||
| $existingData['translations'] = $this | ||
| ->translationsObject() | ||
| ->load($existingData['translations'] ?? []); | ||
| } catch (Throwable) { | ||
| $existingData = []; | ||
| } | ||
|
|
@@ -427,11 +434,30 @@ public function save(): static | |
|
|
||
| ksort($data); | ||
|
|
||
| // save translations to the custom root and remove translations | ||
| // to prevent duplication write into the language file | ||
| if ($this->translationsObject()->root() !== null) { | ||
| $this->translationsObject()->save($data['translations'] ?? []); | ||
| $data['translations'] = []; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it make sense to even unset it here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems setting empty array is correct. With set empty array: <?php
return [
'code' => 'en',
'default' => true,
'direction' => 'ltr',
'locale' => [
'LC_ALL' => 'en_US'
],
'name' => 'EN',
'translations' => [
],
'url' => NULL
];With unseting: <?php
return [
'code' => 'en',
'default' => true,
'direction' => 'ltr',
'locale' => [
'LC_ALL' => 'en_US'
],
'name' => 'EN',
'url' => NULL
]; |
||
| } | ||
|
|
||
| Data::write($this->root(), $data); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the translations data | ||
| * | ||
| * @return $this | ||
| */ | ||
| protected function setTranslations(array $translations = []): static | ||
afbora marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| $this->translations = (new LanguageTranslations($this))->load($translations); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Private siblings collector | ||
| */ | ||
|
|
@@ -475,12 +501,25 @@ public function toArray(): array | |
|
|
||
| /** | ||
| * Returns the translation strings for this language | ||
| * @todo In v5, remove this method and | ||
| * rename `->translationsObject()` method with the `translations`. | ||
| * This will be a breaking change. | ||
| */ | ||
| public function translations(): array | ||
| { | ||
| return $this->translations; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the language translations object for this language | ||
| * @todo In v5, rename this method name as `translations` | ||
| * @internal | ||
| */ | ||
| public function translationsObject(): LanguageTranslations | ||
| { | ||
| return new LanguageTranslations($this, $this->translations); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the absolute Url for the language | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| <?php | ||
|
|
||
| namespace Kirby\Cms; | ||
|
|
||
| use Exception; | ||
| use Kirby\Data\Data; | ||
| use Kirby\Filesystem\F; | ||
|
|
||
| /** | ||
| * With helper methods provides get language translations or | ||
| * loads from custom `translations` root | ||
| * @since 4.2.0 | ||
| * | ||
| * @package Kirby Cms | ||
| * @author Ahmet Bora <[email protected]> | ||
| * @link https://getkirby.com | ||
| * @copyright Bastian Allgeier | ||
| * @license https://getkirby.com/license | ||
| */ | ||
| class LanguageTranslations | ||
| { | ||
| protected array $data; | ||
|
|
||
| public function __construct( | ||
| protected Language $language, | ||
| self|array $translations = [] | ||
| ) { | ||
| $this->setTranslations($translations); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a single translation string by key | ||
| */ | ||
| public function get(string $key, string $default = null): string|null | ||
| { | ||
| return $this->data[$key] ?? $default; | ||
| } | ||
|
|
||
| /** | ||
| * Loads the language translations based on custom roots for provided language code | ||
| */ | ||
| public function load(array $default = []): array | ||
| { | ||
| if ($file = $this->root()) { | ||
| try { | ||
| return Data::read($file); | ||
| } catch (Exception) { | ||
| return $default; | ||
| } | ||
| } | ||
|
|
||
| return $default; | ||
| } | ||
|
|
||
| /** | ||
| * Saves the language translations in the custom root | ||
| * @internal | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function save(array $translations = []): static | ||
| { | ||
| $this->setTranslations($translations); | ||
|
|
||
| if ($root = $this->root()) { | ||
| Data::write($root, $translations); | ||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns custom translations root path if defined | ||
| */ | ||
| 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; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Removes a translation key | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function remove(string $key): static | ||
| { | ||
| unset($this->data[$key]); | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the translation key | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function set(string $key, string|null $value = null): static | ||
| { | ||
| $this->data[$key] = $value; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set translations | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function setTranslations(self|array $translations = []): static | ||
| { | ||
| $this->data = match (true) { | ||
| empty($translations) === true => $this->load(), | ||
| $translations instanceof self => $translations->toArray(), | ||
| default => $translations | ||
| }; | ||
|
|
||
afbora marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns translations | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return $this->data; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.