2022-06-17 17:51:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Kirby\Cms;
|
|
|
|
|
|
|
|
use Kirby\Filesystem\Dir;
|
|
|
|
use Kirby\Filesystem\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
|
|
|
|
*
|
|
|
|
* @package Kirby Cms
|
|
|
|
* @author Bastian Allgeier <bastian@getkirby.com>
|
|
|
|
* @link https://getkirby.com
|
|
|
|
* @copyright Bastian Allgeier
|
|
|
|
* @license https://getkirby.com/license
|
|
|
|
*/
|
|
|
|
class Translations extends Collection
|
|
|
|
{
|
2023-06-01 16:54:20 +02:00
|
|
|
/**
|
|
|
|
* All registered translations methods
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $methods = [];
|
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
/**
|
|
|
|
* @param string $code
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function start(string $code): void
|
|
|
|
{
|
|
|
|
F::move($this->parent->contentFile('', true), $this->parent->contentFile($code, true));
|
|
|
|
}
|
2022-06-17 17:51:59 +02:00
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
/**
|
|
|
|
* @param string $code
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function stop(string $code): void
|
|
|
|
{
|
|
|
|
F::move($this->parent->contentFile($code, true), $this->parent->contentFile('', true));
|
|
|
|
}
|
2022-06-17 17:51:59 +02:00
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
/**
|
|
|
|
* @param array $translations
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function factory(array $translations)
|
|
|
|
{
|
|
|
|
$collection = new static();
|
2022-06-17 17:51:59 +02:00
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
foreach ($translations as $code => $props) {
|
|
|
|
$translation = new Translation($code, $props);
|
|
|
|
$collection->data[$translation->code()] = $translation;
|
|
|
|
}
|
2022-06-17 17:51:59 +02:00
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
return $collection;
|
|
|
|
}
|
2022-06-17 17:51:59 +02:00
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
/**
|
|
|
|
* @param string $root
|
|
|
|
* @param array $inject
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function load(string $root, array $inject = [])
|
|
|
|
{
|
|
|
|
$collection = new static();
|
2022-06-17 17:51:59 +02:00
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
foreach (Dir::read($root) as $filename) {
|
|
|
|
if (F::extension($filename) !== 'json') {
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-17 17:51:59 +02:00
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
$locale = F::name($filename);
|
|
|
|
$translation = Translation::load($locale, $root . '/' . $filename, $inject[$locale] ?? []);
|
2022-06-17 17:51:59 +02:00
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
$collection->data[$locale] = $translation;
|
|
|
|
}
|
2022-06-17 17:51:59 +02:00
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
return $collection;
|
|
|
|
}
|
2022-06-17 17:51:59 +02:00
|
|
|
}
|