-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e32c5d
commit 6c6247c
Showing
9 changed files
with
185 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Translator\Concerns; | ||
|
||
use LaravelLang\Translator\Services\Parameters; | ||
|
||
trait Extractable | ||
{ | ||
/** @var array<Parameters> */ | ||
protected array $parameters = []; | ||
|
||
protected function extractParameters(iterable|string $text): array|string | ||
{ | ||
if (is_string($text)) { | ||
$service = Parameters::make(); | ||
|
||
$result = $service->extract($text); | ||
|
||
$this->setParameter($text, $service); | ||
|
||
return $result; | ||
} | ||
|
||
return collect($text)->map( | ||
fn (string $value) => $this->extractParameters($value) | ||
)->all(); | ||
} | ||
|
||
protected function injectParameters(iterable|string $hash, iterable|string $translated): array|string | ||
{ | ||
if (is_string($hash)) { | ||
return $this->getParameter($hash)?->inject($translated) ?? $translated; | ||
} | ||
|
||
return collect($translated)->map( | ||
fn (string $text, mixed $key) => $this->getParameter($hash[$key])?->inject($text) ?? $text | ||
)->all(); | ||
} | ||
|
||
protected function getParameter(string $key): ?Parameters | ||
{ | ||
return $this->parameters[md5($key)] ?? null; | ||
} | ||
|
||
protected function setParameter(string $key, Parameters $parameters): void | ||
{ | ||
$this->parameters[md5($key)] = $parameters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Translator\Services; | ||
|
||
use DragonCode\Support\Concerns\Makeable; | ||
use DragonCode\Support\Facades\Helpers\Str; | ||
use LaravelLang\Config\Facades\Config; | ||
|
||
class Parameters | ||
{ | ||
use Makeable; | ||
|
||
protected array $values = []; | ||
|
||
public function extract(float|int|string $text): float|int|string | ||
{ | ||
if (! $pattern = $this->pattern()) { | ||
return $text; | ||
} | ||
|
||
if (is_numeric($text)) { | ||
return $text; | ||
} | ||
|
||
return preg_replace_callback($pattern, function (array $matches) { | ||
$index = count($this->values); | ||
|
||
$this->values[$index] = $matches[0]; | ||
|
||
return '{' . $index . '}'; | ||
}, $text); | ||
} | ||
|
||
public function inject(float|int|string $text): float|int|string | ||
{ | ||
if (! $this->values || ! $this->pattern()) { | ||
return $text; | ||
} | ||
|
||
if (is_numeric($text)) { | ||
return $text; | ||
} | ||
|
||
return Str::replaceFormat($text, $this->values, '{%d}'); | ||
} | ||
|
||
public function extracted(): array | ||
{ | ||
return $this->values; | ||
} | ||
|
||
protected function pattern(): bool|string | ||
{ | ||
$value = $this->preserveParameters(); | ||
|
||
if (is_string($value)) { | ||
return $value; | ||
} | ||
|
||
if ($value === true) { | ||
return '/:(\w+)/'; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function preserveParameters(): bool|string | ||
{ | ||
return Config::shared()->translators->options->preserveParameters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
dataset('translatable-with-parameters', fn () => [ | ||
[ | ||
'Foo :attr1 bar :attr2: q :attr3 w :attr4 e :attr5.', | ||
'Foo {0} bar {1}: q {2} w {3} e {4}.', | ||
[':attr1', ':attr2', ':attr3', ':attr4', ':attr5'], | ||
], | ||
[ | ||
'Foo :attr1', | ||
'Foo {0}', | ||
[':attr1'], | ||
], | ||
[ | ||
'Foo bar', | ||
'Foo bar', | ||
[], | ||
], | ||
[ | ||
'Foo 123', | ||
'Foo 123', | ||
[], | ||
], | ||
[ | ||
1234, | ||
1234, | ||
[], | ||
], | ||
[ | ||
1234.56, | ||
1234.56, | ||
[], | ||
], | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use LaravelLang\Translator\Services\Parameters; | ||
|
||
test('extract', function (mixed $source, mixed $target, array $extracts) { | ||
$service = Parameters::make(); | ||
|
||
$extracted = $service->extract($source); | ||
|
||
expect($extracted)->toBe($target); | ||
|
||
expect($service->inject($extracted))->toBe($source); | ||
|
||
expect($service->extracted())->toBe($extracts); | ||
})->with('translatable-with-parameters'); |