1
0
Fork 0
mirror of https://github.com/archtechx/gloss.git synced 2025-12-12 11:14:04 +00:00

Conditional overrides

This commit is contained in:
Samuel Štancl 2020-12-13 20:28:33 +01:00
parent fbf5049110
commit 914bdfb296
5 changed files with 143 additions and 26 deletions

View file

@ -23,11 +23,21 @@ class GlossTranslator extends Translator
*
* @param string $shortKey
* @param string $newKey
* @param array|null|callable $condition
* @return void
*/
public function key(string $shortKey, string $newKey)
public function key(string $shortKey, string $newKey, $condition = null)
{
$this->keyOverrides[$shortKey] = $newKey;
if ($condition === null) {
$condition = fn () => true;
} elseif (!is_callable($condition)) {
$condition = fn ($data) => array_intersect_assoc($data, $condition) !== [];
}
$this->keyOverrides[$shortKey][] = [
'condition' => $condition,
'value' => $newKey,
];
}
/**
@ -35,23 +45,35 @@ class GlossTranslator extends Translator
*
* @param string $shortKey
* @param string $value
* @param array|null|callable $condition
* @return void
*/
public function value(string $shortKey, string $value)
public function value(string $shortKey, string $value, $condition = null)
{
$this->valueOverrides[$shortKey] = $value;
if ($condition === null) {
$condition = fn () => true;
} elseif (! is_callable($condition)) {
$condition = fn ($data) => array_intersect_assoc($data, $condition) !== [];
}
$this->valueOverrides[$shortKey] = [
'condition' => $condition,
'value' => $value,
];
}
/**
* Register multiple value overrides.
*
* @param array $values
* @param array|null|callable $condition
* @return void
*/
public function values(array $values)
public function values(array $values, $condition = null)
{
foreach ($values as $key => $value) {
$this->valueOverrides[$key] = $value;
/** @var string $key */
$this->value($key, $value, $condition);
}
}
@ -98,12 +120,33 @@ class GlossTranslator extends Translator
protected function getWithoutExtensions($key, $replace = [], $locale = null, $fallback = true)
{
return array_key_exists($key, $this->keyOverrides)
? $this->get($this->keyOverrides[$key])
: $this->valueOverrides[$key]
return $this->getKeyOverride($key, $replace)
?? $this->getValueOverride($key, $replace)
?? parent::get($key, $replace, $locale, $fallback);
}
protected function getKeyOverride(string $key, array $data)
{
if (isset($this->keyOverrides[$key])) {
foreach ($this->keyOverrides[$key] as $override) {
if ($override['condition']($data)) {
return $this->get($override['value']);
}
}
}
return null;
}
protected function getValueOverride(string $key, array $data)
{
if (isset($this->valueOverrides[$key]) && $this->valueOverrides[$key]['condition']($data)) {
return $this->valueOverrides[$key]['value'];
}
return null;
}
public function choice($key, $number, array $replace = [], $locale = null)
{
if (array_key_exists($key, $this->extensions)) {

View file

@ -9,20 +9,22 @@ if (! function_exists('gloss')) {
* Resolve a translation string or Gloss instance.
*
* @param string|array|null $key
* @param array $replace
* @param array|callable|null $replace
* @param string|null $locale
* @return void|string|null|\Lean\Gloss\GlossTranslator
*/
function gloss($key = null, array $replace = [], string $locale = null)
function gloss($key = null, $replace = null, string $locale = null)
{
if (is_array($key)) {
Gloss::values($key);
[$overrides, $condition] = [$key, $replace];
Gloss::values($overrides, $condition);
return;
}
if (is_string($key)) {
return Gloss::get($key, $replace, $locale);
return Gloss::get($key, (array) $replace, $locale);
}
return Gloss::getFacadeRoot();