Compare commits
6
Commits
1683435d5d
..
v1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71dda03c3e | ||
|
|
114d4ebe1e | ||
|
|
f92baa03c8 | ||
|
|
bc6344ad17 | ||
|
|
c7c955ecad | ||
|
|
af4be4ed7f |
No files matched your search
+1
-1
@@ -6,7 +6,7 @@
|
||||
"authors": [
|
||||
{
|
||||
"name": "Dave Mc Nicoll",
|
||||
"email": "mcndave@gmail.com"
|
||||
"email": "info@mcnd.ca"
|
||||
}
|
||||
],
|
||||
"require": {},
|
||||
|
||||
+15
-18
@@ -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
|
||||
)
|
||||
@@ -31,8 +31,6 @@ class I18n implements MissingKeyInterface
|
||||
$this->dataReader = $dataReader;
|
||||
$this->cacheReader = $cacheReader;
|
||||
$this->missingKey = $missingKey;
|
||||
|
||||
$this->locale = \Locale::getDefault() ?: $this->locale;
|
||||
}
|
||||
|
||||
public function initialize(bool $useCache) : bool
|
||||
@@ -52,25 +50,25 @@ class I18n implements MissingKeyInterface
|
||||
$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)
|
||||
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);
|
||||
|
||||
@@ -85,15 +83,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
|
||||
@@ -105,9 +102,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);
|
||||
@@ -136,7 +132,8 @@ class I18n implements MissingKeyInterface
|
||||
$search = [];
|
||||
|
||||
foreach($matches as $item) {
|
||||
$search[ $item[0] ] = Iterate::arrayGet($parameters, $item[1]);
|
||||
$iterate = Iterate::arrayGet($parameters, $item[1]);
|
||||
$search[ $item[0] ] = $iterate !== null ? $iterate : $item[0];
|
||||
}
|
||||
|
||||
return str_replace(array_keys($search), array_values($search), $string);
|
||||
|
||||
+20
-8
@@ -49,6 +49,17 @@ class Iterate
|
||||
return false;
|
||||
}
|
||||
|
||||
# Parses $obj1.obj2.obj3.propery
|
||||
protected static function objectFromPath(object $object, array $pathArr) : array
|
||||
{
|
||||
while(count($pathArr) > 1) {
|
||||
$propertyName = array_shift($pathArr);
|
||||
$object = $object->$propertyName;
|
||||
}
|
||||
|
||||
return [ $object, end($pathArr) ];
|
||||
}
|
||||
|
||||
public static function arrayGet(iterable $array, string $path, string $delimiter = '.') {
|
||||
$pathArr = explode($delimiter, $path);
|
||||
|
||||
@@ -58,18 +69,19 @@ class Iterate
|
||||
|
||||
if ( ! is_iterable($recurse) ) {
|
||||
if ( is_object($recurse) ) {
|
||||
if ( count($pathArr) > 1 ) {
|
||||
throw new \Exception("An error occured trying to fetch a value from an object using path $path");
|
||||
}
|
||||
else {
|
||||
list($object, $method) = static::objectFromPath($recurse, $pathArr);
|
||||
|
||||
# @TODO An object caller would be a WAY better idea then this line of code
|
||||
# Calling $obj->varname
|
||||
return $recurse->{$pathArr[0]} ?? "";
|
||||
|
||||
return $object->$method ?? "";
|
||||
}
|
||||
elseif (is_scalar($recurse)) {
|
||||
throw new \Exception("An error occured trying to fetch a value from a scalar value using path '$path'");
|
||||
}
|
||||
}
|
||||
|
||||
return static::arrayGet($recurse, implode($delimiter, $pathArr));
|
||||
return static::arrayGet($recurse, implode($delimiter, $pathArr), $delimiter);
|
||||
}
|
||||
else {
|
||||
return $array[$pathArr[0]];
|
||||
@@ -102,12 +114,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,9 +41,9 @@ class PhpReader implements ReaderInterface {
|
||||
$path = $this->pathList[0];
|
||||
|
||||
if ( ! file_exists($path) ) {
|
||||
mkdir($path, '755', true);
|
||||
mkdir($path, 0755, 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) . ";");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user