-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
196 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,141 @@ | ||
<?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 5.0.0 | ||
* | ||
* @package Kirby Cms | ||
* @author Ahmet Bora <[email protected]> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://getkirby.com/license | ||
*/ | ||
class LanguageTranslations | ||
{ | ||
protected App $kirby; | ||
protected array $data; | ||
|
||
public function __construct( | ||
protected Language $language, | ||
self|array $translations = [] | ||
) { | ||
$this->kirby = App::instance(); | ||
$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 = static::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 | ||
{ | ||
if ($root = $this->root()) { | ||
$this->data = $translations; | ||
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 | ||
{ | ||
if (empty($translations) === false) { | ||
if ($translations instanceof self) { | ||
$this->data = $translations->toArray(); | ||
} else { | ||
$this->data = $translations; | ||
} | ||
} else { | ||
$this->data = static::load(); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns translations | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return $this->data; | ||
} | ||
} |
This file contains 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