mirror of
https://github.com/archtechx/gloss.git
synced 2025-12-12 03:04:04 +00:00
Support multiple value overrides
This commit is contained in:
parent
666c5f2157
commit
b30c68ddd9
2 changed files with 32 additions and 4 deletions
|
|
@ -56,7 +56,7 @@ class GlossTranslator extends Translator
|
|||
$condition = fn ($data) => array_intersect_assoc($data, $condition) !== [];
|
||||
}
|
||||
|
||||
$this->valueOverrides[$shortKey] = [
|
||||
$this->valueOverrides[$shortKey][] = [
|
||||
'condition' => $condition,
|
||||
'value' => $value,
|
||||
];
|
||||
|
|
@ -71,8 +71,8 @@ class GlossTranslator extends Translator
|
|||
*/
|
||||
public function values(array $values, $condition = null)
|
||||
{
|
||||
/** @var string $key */
|
||||
foreach ($values as $key => $value) {
|
||||
/* @var string $key */
|
||||
$this->value($key, $value, $condition);
|
||||
}
|
||||
}
|
||||
|
|
@ -140,8 +140,12 @@ class GlossTranslator extends Translator
|
|||
|
||||
protected function getValueOverride(string $key, array $data)
|
||||
{
|
||||
if (isset($this->valueOverrides[$key]) && $this->valueOverrides[$key]['condition']($data)) {
|
||||
return $this->valueOverrides[$key]['value'];
|
||||
if (isset($this->valueOverrides[$key])) {
|
||||
foreach ($this->valueOverrides[$key] as $override) {
|
||||
if ($override['condition']($data)) {
|
||||
return $override['value'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -291,6 +291,30 @@ class GlossTest extends TestCase
|
|||
$this->assertSame('Edit Bar', Gloss::get('test.resource.edit', ['resource' => 'bar']));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function multiple_overrides_can_exist_for_each_key_and_value()
|
||||
{
|
||||
$this->addMessages('en', 'test', [
|
||||
'abc' => 'Abc',
|
||||
'foo' => 'Foo',
|
||||
'bar' => 'Bar',
|
||||
]);
|
||||
|
||||
gloss()->key('test.abc', 'test.foo', ['resource' => 'foo']);
|
||||
gloss()->key('test.abc', 'test.bar', ['resource' => 'bar']);
|
||||
|
||||
$this->assertSame('Abc', gloss('test.abc', ['resource' => 'anything']));
|
||||
$this->assertSame('Foo', gloss('test.abc', ['resource' => 'foo']));
|
||||
$this->assertSame('Bar', gloss('test.abc', ['resource' => 'bar']));
|
||||
|
||||
gloss()->key('test.abc', 'XXX', ['resource' => 'x']);
|
||||
gloss()->key('test.abc', 'YYY', ['resource' => 'y']);
|
||||
|
||||
$this->assertSame('Abc', gloss('test.abc', ['resource' => 'anything']));
|
||||
$this->assertSame('XXX', gloss('test.abc', ['resource' => 'x']));
|
||||
$this->assertSame('YYY', gloss('test.abc', ['resource' => 'y']));
|
||||
}
|
||||
|
||||
protected function addMessage(string $key, string $value, string $locale = 'en', string $group = 'test', string $namespace = null): void
|
||||
{
|
||||
$this->addMessages($locale, $group, [$key => $value], $namespace);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue