Skip to content

Commit 497c66d

Browse files
ramonrietdijkcalebdw
authored andcommitted
Add support for translations in subdirectories (larastan#2363)
1 parent a374167 commit 497c66d

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

src/Rules/NoMissingTranslationsRule.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Filesystem\Filesystem;
88
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Str;
910
use Larastan\Larastan\Collectors\UsedTranslationFacadeCollector;
1011
use Larastan\Larastan\Collectors\UsedTranslationFunctionCollector;
1112
use Larastan\Larastan\Collectors\UsedTranslationTranslatorCollector;
@@ -26,6 +27,9 @@
2627
use function is_dir;
2728
use function json_decode;
2829
use function lang_path;
30+
use function strlen;
31+
32+
use const DIRECTORY_SEPARATOR;
2933

3034
/** @implements Rule<CollectedDataNode> */
3135
final class NoMissingTranslationsRule implements Rule
@@ -69,8 +73,17 @@ public function processNode(Node $node, Scope $scope): array
6973
$translations = [];
7074

7175
if ($file->getExtension() === 'php') {
76+
$prefix = Str::of($file->getRelativePathname())
77+
->explode(DIRECTORY_SEPARATOR)
78+
->slice(1, -1) // Trim locale and filename
79+
->join('/');
80+
81+
$key = strlen($prefix) > 0
82+
? $prefix . '/' . $file->getFilenameWithoutExtension()
83+
: $file->getFilenameWithoutExtension();
84+
7285
$translations = Arr::dot([
73-
$file->getFilenameWithoutExtension() => $this->filesystem->getRequire($file->getPathname()),
86+
$key => $this->filesystem->getRequire($file->getPathname()),
7487
]);
7588
} elseif ($file->getExtension() === 'json') {
7689
$translations = json_decode($this->filesystem->get($file->getPathname()), true);

tests/Rules/NoMissingTranslationsRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public function testRule(): void
6767
'Translation "messages.test" has not been found.',
6868
32,
6969
],
70+
[
71+
'Translation "sub/lines.farewell" has not been found.',
72+
38,
73+
],
7074
]);
7175
}
7276

tests/Rules/data/Translation.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@ public function translate(Translator $translator): void
3333

3434
__('foo bar baz');
3535
__('messages.nested.key');
36+
37+
Lang::get('sub/lines.greeting');
38+
Lang::get('sub/lines.farewell');
3639
}
3740
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'greeting' => 'Hello world!',
5+
];

0 commit comments

Comments
 (0)