- First commit, some small work porting an old lib I had done into this first draft
This commit is contained in:
commit
a58efcdef4
4 files changed
+288
No files matched your search
+106
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace Tell;
|
||||
|
||||
class I18n
|
||||
{
|
||||
const DELIMITER = '.';
|
||||
|
||||
protected bool $updatedData = false;
|
||||
|
||||
protected array $data = [];
|
||||
|
||||
protected array $cache = [];
|
||||
|
||||
protected array $engine = [];
|
||||
|
||||
protected string $currentLanguage = "";
|
||||
|
||||
protected array $fileStack = [];
|
||||
|
||||
protected function __construct(bool $enableCache = false) {
|
||||
|
||||
# $this->persistent = new Persistent(static::class);
|
||||
if ( $enableCache ) {
|
||||
# $data = $this->persistent->load();
|
||||
# $this->data = $data['data'];
|
||||
# $this->fileStack = $data['fileStack'];
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
# $this->update_data() && $this->persistent->save([
|
||||
# 'fileStack' => $this->fileStack,
|
||||
# 'data' => $this->data
|
||||
# ]);
|
||||
}
|
||||
|
||||
public function updateData() {
|
||||
return $this->updatedData;
|
||||
}
|
||||
|
||||
public static function get(?string $key = null) {
|
||||
if ( $key === null ) {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
return isset( $this->cache[$key] ) ? $this->cache[$key] : $this->cache[$key] = Iterate::arrayGet($this->data, ( $this->currentLanguage ? $this->currentLanguage."." : "").$key, static::DELIMITER);
|
||||
}
|
||||
|
||||
public static function set(string $key, $value) {
|
||||
$cache = explode(static::DELIMITER, $key);
|
||||
|
||||
while ( !empty($cache) ) {
|
||||
$imp = implode(static::DELIMITER, $cache);
|
||||
array_pop($cache);
|
||||
unset( $this->cache[$imp] );
|
||||
}
|
||||
|
||||
$this->cache[$key] = $value ;
|
||||
|
||||
return Iterate::arraySet($this->data, $key, $value, static::DELIMITER);
|
||||
}
|
||||
|
||||
public function addEngine($engine) {
|
||||
$this->engine[] = $engine;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function currentLanguage($set = null) {
|
||||
return $set === null ? $this->currentLanguage : $this->currentLanguage = $set;
|
||||
}
|
||||
|
||||
protected function mergeData($data) {
|
||||
$this->data = array_replace_recursive($this->data, Iterate::splitKeys($data));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user