Skip to content

Commit

Permalink
Merge pull request #72 from Laravel-Lang/2.x
Browse files Browse the repository at this point in the history
Improved detection of translation keys
  • Loading branch information
andrey-helldar authored Mar 22, 2024
2 parents 8c965b2 + ba00869 commit 3716bc0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Services/Packages/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Finder
protected array $contains = [
'__(',
'$t(',
'fail',
'$fail(',
'lang(',
'Lang::choice(',
'Lang::get(',
Expand All @@ -23,6 +23,8 @@ class Finder
'trans_choice(',
'wTrans(',
'wTransChoice(',
'getTranslator()->get(',
'getTranslator()->choice(',
];

protected array $files = [];
Expand Down
23 changes: 21 additions & 2 deletions src/Services/Packages/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Parser
'trans_choice',
'wTrans',
'wTransChoice',
'get',
'choice',
];

protected string $trim_chars = "\t\n\r\0\x0B'\"";
Expand Down Expand Up @@ -72,6 +74,14 @@ protected function parse(string $content): void
foreach ($this->match($content) as $match) {
$value = $match;

if (Str::contains((string) $value, '$')) {
continue;
}

if ($this->isKeyName((string) $value)) {
continue;
}

if (Str::contains((string) $value, $this->subMethods())) {
$sub_key = $this->subkey($value);

Expand Down Expand Up @@ -123,8 +133,10 @@ protected function trim($value): mixed

protected function regex(): string
{
$methods
= Arr::of($this->trans_methods)->implode('|')->replace(['$', '(', ')'], ['\$', '\(', '\)'])->toString();
$methods = Arr::of($this->trans_methods)
->implode('|')
->replace(['$', '(', ')'], ['\$', '\(', '\)'])
->toString();

return sprintf($this->regex, $methods);
}
Expand All @@ -135,4 +147,11 @@ protected function subMethods(): array
->map(fn (string $method) => Str::finish($method, '('))
->toArray();
}

protected function isKeyName(string $value): bool
{
return Str::contains($value, '.')
&& ! Str::contains($value, ' ')
&& $value === Str::lower($value);
}
}

0 comments on commit 3716bc0

Please sign in to comment.