From bc6344ad171770e1249d18aab9449b50e03afb02 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 26 Jan 2023 13:29:34 +0000 Subject: [PATCH] - Fixed multiple language fallback --- src/I18n.php | 24 +++++++++++++----------- src/Iterate.php | 6 +++--- src/Reader/PhpReader.php | 2 +- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/I18n.php b/src/I18n.php index de5b842..860881a 100644 --- a/src/I18n.php +++ b/src/I18n.php @@ -16,14 +16,14 @@ class I18n implements MissingKeyInterface protected string $currentLanguage = ""; - protected Reader\ReaderInterface $dataReader; + protected ? Reader\ReaderInterface $dataReader; protected ? Reader\ReaderInterface $cacheReader = null; protected ? MissingKeyInterface $missingKey = null; public function __construct( - Reader\ReaderInterface $dataReader, + ? Reader\ReaderInterface $dataReader = null, ? Reader\ReaderInterface $cacheReader = null, ? MissingKeyInterface $missingKey = null ) @@ -57,18 +57,22 @@ class I18n implements MissingKeyInterface return $string && $variables ? $this->placeVariables($string, $variables) : $string; } - public function get(string $key) + public function get(string $key) : mixed { # 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); + + if ($string === null && $this->missingKey) { + return $this->missingKey->get($key); + } } return $string; } - public function set(string $key, $value) + public function set(string $key, $value) : mixed { $cache = explode(static::DELIMITER, $key); @@ -83,15 +87,14 @@ class I18n implements MissingKeyInterface return Iterate::arraySet($this->data, $key, $value, static::DELIMITER); } - public function locale(?string $set = null) + public function locale(? string $set = null) { - $locale = $set === null ? ( $this->locale ?? null ) : $this->locale = strtolower(explode(".", $this->locale)[0]); - if ( $set !== null ) { - $this->language = explode('_', $locale)[0]; + $this->locale = strtolower(explode(".", $set)[0]); + $this->language = explode('_', $this->locale)[0]; } - return $locale; + return $this->locale; } protected function mergeData($data) : void @@ -103,9 +106,8 @@ class I18n implements MissingKeyInterface { foreach($reader->pathList as $path) { foreach(Iterate::files($path) as $item) { - if ( $reader->accept($item->getExtension() )) { + 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); diff --git a/src/Iterate.php b/src/Iterate.php index a5d1b21..734ce81 100644 --- a/src/Iterate.php +++ b/src/Iterate.php @@ -69,7 +69,7 @@ class Iterate } } - return static::arrayGet($recurse, implode($delimiter, $pathArr)); + return static::arrayGet($recurse, implode($delimiter, $pathArr), $delimiter); } else { return $array[$pathArr[0]]; @@ -102,12 +102,12 @@ class Iterate $keylist = explode($delimiter, $key); $finalKey = array_shift($keylist); static::arrayKeysSet($keylist, $tmp, $value); - $swap[$finalKey] = static::splitKeys($tmp, $delimiter); + $swap[$finalKey] = static::splitKeys($tmp, $delimiter, $enclosed); unset($array[isset($oldKey) ? $oldKey : $key]); } else if ( is_array($value) ) { - $value = static::splitKeys($value, $delimiter); + $value = static::splitKeys($value, $delimiter, $enclosed); } } diff --git a/src/Reader/PhpReader.php b/src/Reader/PhpReader.php index 5064b9e..a090011 100644 --- a/src/Reader/PhpReader.php +++ b/src/Reader/PhpReader.php @@ -44,6 +44,6 @@ class PhpReader implements ReaderInterface { mkdir($path, '755', true); } - return (bool) file_put_contents($path . DIRECTORY_SEPARATOR . "$filepath." . $this->accept[0], "accept[0]}", "