- Fixed multiple language fallback
This commit is contained in:
parent
c7c955ecad
commit
bc6344ad17
24
src/I18n.php
24
src/I18n.php
|
@ -16,14 +16,14 @@ class I18n implements MissingKeyInterface
|
||||||
|
|
||||||
protected string $currentLanguage = "";
|
protected string $currentLanguage = "";
|
||||||
|
|
||||||
protected Reader\ReaderInterface $dataReader;
|
protected ? Reader\ReaderInterface $dataReader;
|
||||||
|
|
||||||
protected ? Reader\ReaderInterface $cacheReader = null;
|
protected ? Reader\ReaderInterface $cacheReader = null;
|
||||||
|
|
||||||
protected ? MissingKeyInterface $missingKey = null;
|
protected ? MissingKeyInterface $missingKey = null;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Reader\ReaderInterface $dataReader,
|
? Reader\ReaderInterface $dataReader = null,
|
||||||
? Reader\ReaderInterface $cacheReader = null,
|
? Reader\ReaderInterface $cacheReader = null,
|
||||||
? MissingKeyInterface $missingKey = null
|
? MissingKeyInterface $missingKey = null
|
||||||
)
|
)
|
||||||
|
@ -57,18 +57,22 @@ class I18n implements MissingKeyInterface
|
||||||
return $string && $variables ? $this->placeVariables($string, $variables) : $string;
|
return $string && $variables ? $this->placeVariables($string, $variables) : $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(string $key)
|
public function get(string $key) : mixed
|
||||||
{
|
{
|
||||||
# Testing full locale key first
|
# Testing full locale key first
|
||||||
if ( null === ( $string = Iterate::arrayGet($this->data, "{$this->locale}.$key", static::DELIMITER)) ) {
|
if ( null === ( $string = Iterate::arrayGet($this->data, "{$this->locale}.$key", static::DELIMITER)) ) {
|
||||||
# Fallback on language only
|
# Fallback on language only
|
||||||
$string = Iterate::arrayGet($this->data, "{$this->language}.$key", static::DELIMITER);
|
$string = Iterate::arrayGet($this->data, "{$this->language}.$key", static::DELIMITER);
|
||||||
|
|
||||||
|
if ($string === null && $this->missingKey) {
|
||||||
|
return $this->missingKey->get($key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set(string $key, $value)
|
public function set(string $key, $value) : mixed
|
||||||
{
|
{
|
||||||
$cache = explode(static::DELIMITER, $key);
|
$cache = explode(static::DELIMITER, $key);
|
||||||
|
|
||||||
|
@ -83,15 +87,14 @@ class I18n implements MissingKeyInterface
|
||||||
return Iterate::arraySet($this->data, $key, $value, static::DELIMITER);
|
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 ) {
|
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
|
protected function mergeData($data) : void
|
||||||
|
@ -103,9 +106,8 @@ class I18n implements MissingKeyInterface
|
||||||
{
|
{
|
||||||
foreach($reader->pathList as $path) {
|
foreach($reader->pathList as $path) {
|
||||||
foreach(Iterate::files($path) as $item) {
|
foreach(Iterate::files($path) as $item) {
|
||||||
if ( $reader->accept($item->getExtension() )) {
|
if ( $reader->accept($item->getExtension()) ) {
|
||||||
if ( null !== ( $data = $reader->load( $item ) )) {
|
if ( null !== ( $data = $reader->load( $item ) )) {
|
||||||
|
|
||||||
if ( $reader->filenameAsKey ) {
|
if ( $reader->filenameAsKey ) {
|
||||||
# Adding folder to full key
|
# Adding folder to full key
|
||||||
$filekey = ltrim(substr($item->getPathname(), strlen($path)), DIRECTORY_SEPARATOR);
|
$filekey = ltrim(substr($item->getPathname(), strlen($path)), DIRECTORY_SEPARATOR);
|
||||||
|
|
|
@ -69,7 +69,7 @@ class Iterate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return static::arrayGet($recurse, implode($delimiter, $pathArr));
|
return static::arrayGet($recurse, implode($delimiter, $pathArr), $delimiter);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return $array[$pathArr[0]];
|
return $array[$pathArr[0]];
|
||||||
|
@ -102,12 +102,12 @@ class Iterate
|
||||||
$keylist = explode($delimiter, $key);
|
$keylist = explode($delimiter, $key);
|
||||||
$finalKey = array_shift($keylist);
|
$finalKey = array_shift($keylist);
|
||||||
static::arrayKeysSet($keylist, $tmp, $value);
|
static::arrayKeysSet($keylist, $tmp, $value);
|
||||||
$swap[$finalKey] = static::splitKeys($tmp, $delimiter);
|
$swap[$finalKey] = static::splitKeys($tmp, $delimiter, $enclosed);
|
||||||
|
|
||||||
unset($array[isset($oldKey) ? $oldKey : $key]);
|
unset($array[isset($oldKey) ? $oldKey : $key]);
|
||||||
}
|
}
|
||||||
else if ( is_array($value) ) {
|
else if ( is_array($value) ) {
|
||||||
$value = static::splitKeys($value, $delimiter);
|
$value = static::splitKeys($value, $delimiter, $enclosed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,6 @@ class PhpReader implements ReaderInterface {
|
||||||
mkdir($path, '755', true);
|
mkdir($path, '755', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (bool) file_put_contents($path . DIRECTORY_SEPARATOR . "$filepath." . $this->accept[0], "<?php return " . var_export($content, true) . ";");
|
return (bool) file_put_contents($path . DIRECTORY_SEPARATOR . "$filepath.{$this->accept[0]}", "<?php return " . var_export($content, true) . ";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue