mirror of
https://github.com/archtechx/gloss.git
synced 2025-12-12 11:14:04 +00:00
Conditional overrides
This commit is contained in:
parent
fbf5049110
commit
914bdfb296
5 changed files with 143 additions and 26 deletions
|
|
@ -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)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue