mirror of
https://github.com/getgrav/grav.git
synced 2026-07-08 19:41:33 +02:00
Some lang improvements
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
# v1.6.0-beta.4
|
||||
## mm/dd/2018
|
||||
|
||||
1. [](#new)
|
||||
* Added `Utils::arrayFlattenDotNotation()` and `Utils::arrayUnflattenDotNotation()` helper methods
|
||||
1. [](#improved)
|
||||
* Added apcu autoloader optimization
|
||||
* Additional helper methods in `Language`, `Languages`, and `LanguageCodes` classes
|
||||
1. [](#bugfix)
|
||||
* Use login provider User avatar if set
|
||||
|
||||
|
||||
@@ -52,4 +52,15 @@ class Languages extends Data
|
||||
{
|
||||
$this->items = Utils::arrayMergeRecursiveUnique($this->items, $data);
|
||||
}
|
||||
|
||||
public function flattenByLang($lang)
|
||||
{
|
||||
$language = $this->items[$lang];
|
||||
return Utils::arrayFlattenDotNotation($language);
|
||||
}
|
||||
|
||||
public function unflatten($array)
|
||||
{
|
||||
return Utils::arrayUnflattenDotNotation($array);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -504,4 +504,16 @@ class Language
|
||||
return $this->http_accept_language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accessible wrapper to LanguageCodes
|
||||
*
|
||||
* @param $code
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function getLanguageCode($code, $type = 'name')
|
||||
{
|
||||
return LanguageCodes::get($code, $type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ class LanguageCodes
|
||||
return $results;
|
||||
}
|
||||
|
||||
protected static function get($code, $type)
|
||||
public static function get($code, $type)
|
||||
{
|
||||
if (isset(static::$codes[$code][$type])) {
|
||||
return static::$codes[$code][$type];
|
||||
|
||||
@@ -708,6 +708,55 @@ abstract class Utils
|
||||
return $flatten;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten a multi-dimensional associative array into dot notation
|
||||
*
|
||||
* @param array $array
|
||||
* @param string $prepend
|
||||
* @return array
|
||||
*/
|
||||
public static function arrayFlattenDotNotation($array, $prepend = '')
|
||||
{
|
||||
$results = array();
|
||||
foreach ($array as $key => $value)
|
||||
{
|
||||
if (is_array($value))
|
||||
{
|
||||
$results = array_merge($results, static::arrayFlattenDotNotation($value, $prepend.$key.'.'));
|
||||
}
|
||||
else
|
||||
{
|
||||
$results[$prepend.$key] = $value;
|
||||
}
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opposite of flatten, convert flat dot notation array to multi dimensional array
|
||||
*
|
||||
* @param $array
|
||||
* @param string $separator
|
||||
* @return array
|
||||
*/
|
||||
public static function arrayUnflattenDotNotation($array, $separator = '.') {
|
||||
$newArray = array();
|
||||
foreach($array as $key => $value) {
|
||||
$dots = explode($separator, $key);
|
||||
if(count($dots) > 1) {
|
||||
$last = &$newArray[ $dots[0] ];
|
||||
foreach($dots as $k => $dot) {
|
||||
if($k == 0) continue;
|
||||
$last = &$last[$dot];
|
||||
}
|
||||
$last = $value;
|
||||
} else {
|
||||
$newArray[$key] = $value;
|
||||
}
|
||||
}
|
||||
return $newArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the passed path contains the language code prefix
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user