mirror of
https://github.com/archtechx/gloss.git
synced 2025-12-12 03:04:04 +00:00
Conditional overrides
This commit is contained in:
parent
fbf5049110
commit
914bdfb296
5 changed files with 143 additions and 26 deletions
|
|
@ -130,7 +130,7 @@ Gloss::extend('foo.pagination', fn ($value, $replace) => $replace($value, [
|
||||||
':total' => '<span class="font-medium">:total</span>',
|
':total' => '<span class="font-medium">:total</span>',
|
||||||
]));
|
]));
|
||||||
|
|
||||||
Gloss::get('test.pagination', ['start' => 10, 'end' => 20, 'total' => 50])
|
Gloss::get('foo.pagination', ['start' => 10, 'end' => 20, 'total' => 50])
|
||||||
// Showing <span class="font-medium">10</span> to <span class="font-medium">20</span> of <span class="font-medium">50</span> results
|
// Showing <span class="font-medium">10</span> to <span class="font-medium">20</span> of <span class="font-medium">50</span> results
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,22 +16,22 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"illuminate/translation": "^8.18||^6.0"
|
"illuminate/translation": "^8.18"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"orchestra/testbench": "^6.4.0||^4.0.0",
|
"orchestra/testbench": "^6.4.0",
|
||||||
"orchestra/testbench-core": "6.7.0||^4.0.0",
|
"orchestra/testbench-core": "6.8.0",
|
||||||
"phpunit/phpunit": "^9.5",
|
"phpunit/phpunit": "^9.5",
|
||||||
"nunomaduro/larastan": "^0.6.11"
|
"nunomaduro/larastan": "^0.6.11"
|
||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"laravel": {
|
"laravel": {
|
||||||
"providers": [
|
"providers": [
|
||||||
"Lean\\Gloss\\GlossServiceProviders"
|
"Lean\\Gloss\\GlossServiceProviders"
|
||||||
],
|
],
|
||||||
"aliases": {
|
"aliases": {
|
||||||
"Gloss": "Lean\\Gloss\\Gloss"
|
"Gloss": "Lean\\Gloss\\Gloss"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -23,11 +23,21 @@ class GlossTranslator extends Translator
|
||||||
*
|
*
|
||||||
* @param string $shortKey
|
* @param string $shortKey
|
||||||
* @param string $newKey
|
* @param string $newKey
|
||||||
|
* @param array|null|callable $condition
|
||||||
* @return void
|
* @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 $shortKey
|
||||||
* @param string $value
|
* @param string $value
|
||||||
|
* @param array|null|callable $condition
|
||||||
* @return void
|
* @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.
|
* Register multiple value overrides.
|
||||||
*
|
*
|
||||||
* @param array $values
|
* @param array $values
|
||||||
|
* @param array|null|callable $condition
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function values(array $values)
|
public function values(array $values, $condition = null)
|
||||||
{
|
{
|
||||||
foreach ($values as $key => $value) {
|
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)
|
protected function getWithoutExtensions($key, $replace = [], $locale = null, $fallback = true)
|
||||||
{
|
{
|
||||||
return array_key_exists($key, $this->keyOverrides)
|
return $this->getKeyOverride($key, $replace)
|
||||||
? $this->get($this->keyOverrides[$key])
|
?? $this->getValueOverride($key, $replace)
|
||||||
: $this->valueOverrides[$key]
|
|
||||||
?? parent::get($key, $replace, $locale, $fallback);
|
?? 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)
|
public function choice($key, $number, array $replace = [], $locale = null)
|
||||||
{
|
{
|
||||||
if (array_key_exists($key, $this->extensions)) {
|
if (array_key_exists($key, $this->extensions)) {
|
||||||
|
|
|
||||||
|
|
@ -9,20 +9,22 @@ if (! function_exists('gloss')) {
|
||||||
* Resolve a translation string or Gloss instance.
|
* Resolve a translation string or Gloss instance.
|
||||||
*
|
*
|
||||||
* @param string|array|null $key
|
* @param string|array|null $key
|
||||||
* @param array $replace
|
* @param array|callable|null $replace
|
||||||
* @param string|null $locale
|
* @param string|null $locale
|
||||||
* @return void|string|null|\Lean\Gloss\GlossTranslator
|
* @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)) {
|
if (is_array($key)) {
|
||||||
Gloss::values($key);
|
[$overrides, $condition] = [$key, $replace];
|
||||||
|
|
||||||
|
Gloss::values($overrides, $condition);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_string($key)) {
|
if (is_string($key)) {
|
||||||
return Gloss::get($key, $replace, $locale);
|
return Gloss::get($key, (array) $replace, $locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Gloss::getFacadeRoot();
|
return Gloss::getFacadeRoot();
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@
|
||||||
namespace Lean\Gloss\Tests;
|
namespace Lean\Gloss\Tests;
|
||||||
|
|
||||||
use Lean\Gloss\Gloss;
|
use Lean\Gloss\Gloss;
|
||||||
use Lean\Gloss\GlossServiceProvider;
|
|
||||||
use Lean\Gloss\GlossTranslator;
|
use Lean\Gloss\GlossTranslator;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class GlossTest extends TestCase
|
class GlossTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -219,6 +219,78 @@ class GlossTest extends TestCase
|
||||||
$this->assertSame('Je tam <span class="font-medium">5</span> jablek', gloss()->choice('test.apples', 5));
|
$this->assertSame('Je tam <span class="font-medium">5</span> jablek', gloss()->choice('test.apples', 5));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function key_overrides_can_have_conditions()
|
||||||
|
{
|
||||||
|
$this->addMessages('en', 'test', [
|
||||||
|
'resource.create' => 'Create :Resource',
|
||||||
|
'foo.create' => 'Foo/Create',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Gloss::key('test.resource.create', 'test.foo.create', ['resource' => 'foo']);
|
||||||
|
|
||||||
|
$this->assertSame('Foo/Create', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Create Bar', Gloss::get('test.resource.create', ['resource' => 'bar']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function value_overrides_can_have_conditions()
|
||||||
|
{
|
||||||
|
$this->addMessages('en', 'test', [
|
||||||
|
'resource.create' => 'Create :Resource',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Gloss::value('test.resource.create', 'Bar/Create', ['resource' => 'bar']);
|
||||||
|
|
||||||
|
$this->assertSame('Create Foo', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Bar/Create', Gloss::get('test.resource.create', ['resource' => 'bar']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function bulk_value_overrides_can_have_conditions()
|
||||||
|
{
|
||||||
|
$this->addMessages('en', 'test', [
|
||||||
|
'resource.create' => 'Create :Resource',
|
||||||
|
'resource.edit' => 'Edit :Resource',
|
||||||
|
]);
|
||||||
|
|
||||||
|
gloss([
|
||||||
|
'test.resource.create' => 'Foo/Create',
|
||||||
|
'test.resource.edit' => 'Foo/Edit',
|
||||||
|
], ['resource' => 'foo']);
|
||||||
|
|
||||||
|
$this->assertSame('Foo/Create', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Foo/Edit', Gloss::get('test.resource.edit', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Create Bar', Gloss::get('test.resource.create', ['resource' => 'bar']));
|
||||||
|
$this->assertSame('Edit Bar', Gloss::get('test.resource.edit', ['resource' => 'bar']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function custom_condition_for_key_and_value_overrides_can_be_used()
|
||||||
|
{
|
||||||
|
$this->addMessages('en', 'test', [
|
||||||
|
'resource.index' => 'Index of :Resource',
|
||||||
|
'resource.create' => 'Create :Resource',
|
||||||
|
'resource.show' => 'Detail of :Resource',
|
||||||
|
'resource.edit' => 'Edit :Resource',
|
||||||
|
]);
|
||||||
|
|
||||||
|
gloss([
|
||||||
|
'test.resource.index' => 'custom',
|
||||||
|
'test.resource.create' => 'custom',
|
||||||
|
], fn ($data) => Str::startsWith($data['resource'], 'f'));
|
||||||
|
|
||||||
|
$this->assertSame('custom', Gloss::get('test.resource.index', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('custom', Gloss::get('test.resource.create', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Detail of Foo', Gloss::get('test.resource.show', ['resource' => 'foo']));
|
||||||
|
$this->assertSame('Edit Foo', Gloss::get('test.resource.edit', ['resource' => 'foo']));
|
||||||
|
|
||||||
|
$this->assertSame('Index of Bar', Gloss::get('test.resource.index', ['resource' => 'bar']));
|
||||||
|
$this->assertSame('Create Bar', Gloss::get('test.resource.create', ['resource' => 'bar']));
|
||||||
|
$this->assertSame('Detail of Bar', Gloss::get('test.resource.show', ['resource' => 'bar']));
|
||||||
|
$this->assertSame('Edit Bar', Gloss::get('test.resource.edit', ['resource' => 'bar']));
|
||||||
|
}
|
||||||
|
|
||||||
protected function addMessage(string $key, string $value, string $locale = 'en', string $group = 'test', string $namespace = null): void
|
protected function addMessage(string $key, string $value, string $locale = 'en', string $group = 'test', string $namespace = null): void
|
||||||
{
|
{
|
||||||
$this->addMessages($locale, $group, [$key => $value], $namespace);
|
$this->addMessages($locale, $group, [$key => $value], $namespace);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue