- First working version of Tell
- JsonReader and PhpReader added
This commit is contained in:
1 parent
a58efcdef4
commit
026126be76
7 files changed
+277
-88
No files matched your search
+107
-68
@@ -2,105 +2,144 @@
|
||||
|
||||
namespace Tell;
|
||||
|
||||
class I18n
|
||||
class I18n implements MissingKeyInterface
|
||||
{
|
||||
const DELIMITER = '.';
|
||||
|
||||
protected bool $updatedData = false;
|
||||
protected string $locale = "en_US";
|
||||
|
||||
protected array $data = [];
|
||||
protected string $language = "";
|
||||
|
||||
protected array $cache = [];
|
||||
|
||||
protected array $engine = [];
|
||||
|
||||
protected string $currentLanguage = "";
|
||||
protected array $data = [];
|
||||
|
||||
protected array $fileStack = [];
|
||||
|
||||
protected function __construct(bool $enableCache = false) {
|
||||
protected string $currentLanguage = "";
|
||||
|
||||
# $this->persistent = new Persistent(static::class);
|
||||
if ( $enableCache ) {
|
||||
# $data = $this->persistent->load();
|
||||
# $this->data = $data['data'];
|
||||
# $this->fileStack = $data['fileStack'];
|
||||
}
|
||||
protected Reader\ReaderInterface $dataReader;
|
||||
|
||||
protected ? Reader\ReaderInterface $cacheReader = null;
|
||||
|
||||
protected ? MissingKeyInterface $missingKey = null;
|
||||
|
||||
public function __construct(
|
||||
Reader\ReaderInterface $dataReader,
|
||||
? Reader\ReaderInterface $cacheReader = null,
|
||||
? MissingKeyInterface $missingKey = null
|
||||
)
|
||||
{
|
||||
$this->dataReader = $dataReader;
|
||||
$this->cacheReader = $cacheReader;
|
||||
$this->missingKey = $missingKey;
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
# $this->update_data() && $this->persistent->save([
|
||||
# 'fileStack' => $this->fileStack,
|
||||
# 'data' => $this->data
|
||||
# ]);
|
||||
}
|
||||
public function initialize(bool $useCache) : bool
|
||||
{
|
||||
$loaded = $useCache && $this->fromReader($this->cacheReader) ?: $this->fromReader($this->dataReader);
|
||||
|
||||
public function updateData() {
|
||||
return $this->updatedData;
|
||||
}
|
||||
|
||||
public static function get(?string $key = null) {
|
||||
if ( $key === null ) {
|
||||
return $this->data;
|
||||
if ( ! $useCache ) {
|
||||
$this->cacheReader->save($this->locale, $this->data);
|
||||
}
|
||||
|
||||
return isset( $this->cache[$key] ) ? $this->cache[$key] : $this->cache[$key] = Iterate::arrayGet($this->data, ( $this->currentLanguage ? $this->currentLanguage."." : "").$key, static::DELIMITER);
|
||||
return $loaded;
|
||||
}
|
||||
|
||||
public static function set(string $key, $value) {
|
||||
public function fromKey(string $key, array $variables) : ?string
|
||||
{
|
||||
if ( null === ( $string = $this->get($key) ) ) {
|
||||
$string = $this->missingKey ? $this->missingKey->get($key) : null;
|
||||
}
|
||||
|
||||
if (is_array($string)) {
|
||||
$string = json_encode($string);
|
||||
}
|
||||
|
||||
return $string && $variables ? $this->placeVariables($string, $variables) : $string;
|
||||
}
|
||||
|
||||
public function get(string $key)
|
||||
{
|
||||
# Testing full locale key first
|
||||
if ( null === ( $string = Iterate::arrayGet($this->data, "{$this->locale}.$key", static::DELIMITER)) ) {
|
||||
# Fallback on language only
|
||||
$string = Iterate::arrayGet($this->data, "{$this->language}.$key", static::DELIMITER);
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
public function set(string $key, $value)
|
||||
{
|
||||
$cache = explode(static::DELIMITER, $key);
|
||||
|
||||
while ( !empty($cache) ) {
|
||||
while ( ! empty($cache) ) {
|
||||
$imp = implode(static::DELIMITER, $cache);
|
||||
array_pop($cache);
|
||||
unset( $this->cache[$imp] );
|
||||
}
|
||||
|
||||
$this->cache[$key] = $value ;
|
||||
$this->cache[$key] = $value;
|
||||
|
||||
return Iterate::arraySet($this->data, $key, $value, static::DELIMITER);
|
||||
}
|
||||
|
||||
public function addEngine($engine) {
|
||||
$this->engine[] = $engine;
|
||||
}
|
||||
public function locale(?string $set = null)
|
||||
{
|
||||
$locale = $set === null ? ( $this->locale ?? null ) : $this->locale = strtolower(explode(".", $this->locale)[0]);
|
||||
|
||||
public function load(string $path, bool $filenameAsKey = false) {
|
||||
if ( !in_array($path, $this->fileStack) ) {
|
||||
$this->updatedData = true;
|
||||
$this->fileStack[] = $path;
|
||||
|
||||
foreach(Iterate::files($path) as $item) {
|
||||
foreach($this->engine as $engine) {
|
||||
if ( $engine->accept($item )) {
|
||||
|
||||
if ( $array = $engine->translate( $item ) ){
|
||||
if ($filenameAsKey ) {
|
||||
$base = basename($item);
|
||||
|
||||
$key = substr($base,0, strrpos($base, "."));
|
||||
$keysplit = explode(".", $key);
|
||||
$key = array_pop($keysplit);
|
||||
|
||||
$this->mergeData([ $keysplit ? "{".implode(".", array_merge([ $key ], $keysplit))."}" : $key => $array ]);
|
||||
}
|
||||
else {
|
||||
$this->mergeData($array);
|
||||
}
|
||||
}
|
||||
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( $set !== null ) {
|
||||
$this->language = explode('_', $locale)[0];
|
||||
}
|
||||
|
||||
return $locale;
|
||||
}
|
||||
|
||||
public static function currentLanguage($set = null) {
|
||||
return $set === null ? $this->currentLanguage : $this->currentLanguage = $set;
|
||||
}
|
||||
|
||||
protected function mergeData($data) {
|
||||
protected function mergeData($data) : void
|
||||
{
|
||||
$this->data = array_replace_recursive($this->data, Iterate::splitKeys($data));
|
||||
}
|
||||
|
||||
protected function fromReader(Reader\ReaderInterface $reader) : bool
|
||||
{
|
||||
foreach($reader->pathList as $path) {
|
||||
foreach(Iterate::files($path) as $item) {
|
||||
if ( $reader->accept($item->getExtension() )) {
|
||||
if ( null !== ( $data = $reader->load( $item ) )) {
|
||||
|
||||
if ( $reader->filenameAsKey ) {
|
||||
# Adding folder to full key
|
||||
$filekey = ltrim(substr($item->getPathname(), strlen($path)), DIRECTORY_SEPARATOR);
|
||||
|
||||
# Removing extension
|
||||
$filekey = substr($filekey, 0, strrpos($filekey, '.'));
|
||||
|
||||
# Replacing directory separator with dots
|
||||
$filekey = str_replace(DIRECTORY_SEPARATOR, '.', $filekey);
|
||||
|
||||
$this->mergeData([ '{' . $filekey . '}' => $data ]);
|
||||
}
|
||||
else {
|
||||
$this->mergeData($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function placeVariables(string $string, array $parameters) {
|
||||
if ( preg_match_all('~{$(.*?)}~si', $string, $matches, PREG_SET_ORDER) ) {
|
||||
$search = [];
|
||||
|
||||
foreach($matches as $item) {
|
||||
$search[ $item[0] ] = Iterate::arrayGet($parameters, $item[1]);
|
||||
}
|
||||
|
||||
return str_replace(array_keys($search), array_values($search), $string);
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user