first version
This commit is contained in:
55
kirby/src/Cms/Translations.php
Executable file
55
kirby/src/Cms/Translations.php
Executable file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Cms;
|
||||
|
||||
use Kirby\Toolkit\Dir;
|
||||
use Kirby\Toolkit\F;
|
||||
|
||||
/**
|
||||
* A collection of all available Translations.
|
||||
* Provides a factory method to convert an array
|
||||
* to a collection of Translation objects and load
|
||||
* method to load all translations from disk
|
||||
*/
|
||||
class Translations extends Collection
|
||||
{
|
||||
public function start(string $code)
|
||||
{
|
||||
F::move($this->parent->contentFile('', true), $this->parent->contentFile($code, true));
|
||||
}
|
||||
|
||||
public function stop(string $code)
|
||||
{
|
||||
F::move($this->parent->contentFile($code, true), $this->parent->contentFile('', true));
|
||||
}
|
||||
|
||||
public static function factory(array $translations)
|
||||
{
|
||||
$collection = new static;
|
||||
|
||||
foreach ($translations as $code => $props) {
|
||||
$translation = new Translation($code, $props);
|
||||
$collection->data[$translation->code()] = $translation;
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public static function load(string $root, array $inject = [])
|
||||
{
|
||||
$collection = new static;
|
||||
|
||||
foreach (Dir::read($root) as $filename) {
|
||||
if (F::extension($filename) !== 'json') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$locale = F::name($filename);
|
||||
$translation = Translation::load($locale, $root . '/' . $filename, $inject[$locale] ?? []);
|
||||
|
||||
$collection->data[$locale] = $translation;
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user