Skip to content

Commit bc20c78

Browse files
committed
support nested folders
1 parent f24e998 commit bc20c78

36 files changed

+289
-241
lines changed

src/Collections/PhpTranslations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ public function except(array $keys): static
9898
$items = $this->items;
9999

100100
foreach ($keys as $key) {
101+
101102
if (array_key_exists($key, $items)) {
102103
unset($items[$key]);
103104
} elseif (str_contains($key, '.')) {
104-
105105
$this->recursiveForget(
106106
$items,
107107
explode('.', $key)
108108
);
109-
110109
}
110+
111111
}
112112

113113
return new static($items);

src/Drivers/PhpDriver.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Elegantly\Translator\Collections\PhpTranslations;
88
use Elegantly\Translator\Collections\Translations;
99
use Illuminate\Contracts\Filesystem\Filesystem;
10+
use Illuminate\Support\Arr;
1011
use Illuminate\Support\Facades\File;
1112
use Illuminate\Support\Facades\Storage;
1213

@@ -82,21 +83,27 @@ public function getLocales(): array
8283
*/
8384
public function getNamespaces(string $locale): array
8485
{
85-
return collect($this->storage->files($locale))
86+
return collect($this->storage->allFiles($locale))
8687
->filter(fn (string $file) => File::extension($file) === 'php')
87-
->map(fn (string $file) => File::name($file))
88+
->map(function (string $file) use ($locale) {
89+
return str($file)
90+
->after($locale.DIRECTORY_SEPARATOR)
91+
->beforeLast('.')
92+
->value();
93+
})
8894
->sort(SORT_NATURAL)
8995
->values()
9096
->all();
9197
}
9298

9399
public function getTranslations(string $locale): PhpTranslations
94100
{
95-
$values = collect($this->getNamespaces($locale))
96-
->mapWithKeys(function ($namespace) use ($locale) {
97-
return [$namespace => $this->getTranslationsInNamespace($locale, $namespace)];
98-
})
99-
->all();
101+
$values = Arr::mapWithKeys(
102+
$this->getNamespaces($locale),
103+
fn ($namespace) => [
104+
$namespace => $this->getTranslationsInNamespace($locale, $namespace),
105+
]
106+
);
100107

101108
return new PhpTranslations(
102109
PhpTranslations::prepareTranslations($values) ?? []

src/Translator.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515

1616
class Translator
1717
{
18+
/**
19+
* @param string[] $ignoredTranslations
20+
*/
1821
final public function __construct(
1922
public Driver $driver,
23+
public array $ignoredTranslations = [],
2024
public ?TranslateServiceInterface $translateService = null,
2125
public ?ProofreadServiceInterface $proofreadService = null,
2226
public ?SearchCodeServiceInterface $searchcodeService = null,
@@ -29,6 +33,7 @@ public function driver(null|string|Driver $name): static
2933
{
3034
return new static(
3135
driver: $name instanceof Driver ? $name : TranslatorServiceProvider::getDriverFromConfig($name),
36+
ignoredTranslations: $this->ignoredTranslations,
3237
translateService: $this->translateService,
3338
proofreadService: $this->proofreadService,
3439
searchcodeService: $this->searchcodeService,
@@ -40,6 +45,7 @@ public function withProofreadService(ProofreadServiceInterface $service): static
4045
{
4146
return new static(
4247
driver: $this->driver,
48+
ignoredTranslations: $this->ignoredTranslations,
4349
translateService: $this->translateService,
4450
proofreadService: $service,
4551
searchcodeService: $this->searchcodeService,
@@ -51,6 +57,7 @@ public function withTranslateService(TranslateServiceInterface $service): static
5157
{
5258
return new static(
5359
driver: $this->driver,
60+
ignoredTranslations: $this->ignoredTranslations,
5461
translateService: $service,
5562
proofreadService: $this->proofreadService,
5663
searchcodeService: $this->searchcodeService,
@@ -62,6 +69,7 @@ public function withSearchcodeService(SearchCodeServiceInterface $service): stat
6269
{
6370
return new static(
6471
driver: $this->driver,
72+
ignoredTranslations: $this->ignoredTranslations,
6573
translateService: $this->translateService,
6674
proofreadService: $this->proofreadService,
6775
searchcodeService: $service,
@@ -79,9 +87,11 @@ public function getLocales(): array
7987
}
8088

8189
if ($validator = TranslatorServiceProvider::getLocaleValidator()) {
90+
$validator = $validator::make();
91+
8292
return array_values(array_filter(
8393
$this->driver->getLocales(),
84-
fn ($locale) => $validator::make()->isValid($locale),
94+
fn ($locale) => $validator->isValid($locale),
8595
));
8696
}
8797

@@ -125,9 +135,7 @@ public function getMissingTranslations(
125135
);
126136

127137
return collect($keys)
128-
->filter(function ($value, $key) use ($translations) {
129-
return ! $translations->has($key);
130-
})
138+
->filter(fn ($value, $key) => ! $translations->has($key))
131139
->all();
132140
}
133141

@@ -140,11 +148,14 @@ public function getDeadTranslations(string $locale): Translations
140148
throw TranslatorServiceException::missingSearchcodeService();
141149
}
142150

143-
$defined = $this->searchcodeService->filesByTranslations();
151+
$keys = array_keys($this->searchcodeService->filesByTranslations());
144152

145153
return $this
146154
->getTranslations($locale)
147-
->except(array_keys($defined));
155+
->except([
156+
...$this->ignoredTranslations,
157+
...$keys,
158+
]);
148159
}
149160

150161
public function getUntranslatedTranslations(

src/TranslatorServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function registeringPackage(): void
6161
$this->app->scoped(Translator::class, function () {
6262
return new Translator(
6363
driver: static::getDriverFromConfig(),
64+
ignoredTranslations: static::getIgnoredTranslationsFromConfig(),
6465
translateService: static::getTranslateServiceFromConfig(),
6566
proofreadService: static::getProofreadServiceFromConfig(),
6667
searchcodeService: static::getSearchcodeServiceFromConfig(),
@@ -80,6 +81,14 @@ public static function getDriverFromConfig(?string $driverName = null): Driver
8081
};
8182
}
8283

84+
/**
85+
* @return string[]
86+
*/
87+
public static function getIgnoredTranslationsFromConfig(): array
88+
{
89+
return (array) config('translator.searchcode.ignored_translations');
90+
}
91+
8392
public static function getTranslateServiceFromConfig(?string $serviceName = null): ?TranslateServiceInterface
8493
{
8594
/** @var string|null $service */

tests/Feature/JsonDriverTest.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
driver: $this->getJsonDriver(),
1010
);
1111

12-
expect($translator->getLocales())->toBe(['fr']);
12+
expect($translator->getLocales())->toBe(['fr', 'it']);
1313
});
1414

1515
it('gets translations', function () {
@@ -21,11 +21,12 @@
2121

2222
expect($translations->toArray())->toBe([
2323
'All rights reserved.' => 'Tous droits réservés.',
24-
'This one is used.' => 'Celui-ci est utilisé.',
24+
'This one is untranslated' => 'Celui-ci est manquant',
25+
'This one is dead' => 'Celui-ci est mort',
2526
]);
2627
});
2728

28-
it('gets undefined translations', function () {
29+
it('gets missing translations', function () {
2930
$translator = new Translator(
3031
driver: $this->getJsonDriver(),
3132
searchcodeService: $this->getSearchCodeService()
@@ -34,29 +35,34 @@
3435
$keys = $translator->getMissingTranslations('fr');
3536

3637
expect($keys)->toBe([
37-
'messages.dummy.class' => [
38+
'This one is missing' => [
3839
'count' => 1,
3940
'files' => [
40-
$this->formatPath($this->getAppPath().'/DummyClass.php'),
41+
$this->formatPath($this->getResourcesPath().'/views/json/foo.blade.php'),
42+
],
43+
],
44+
'messages.missing' => [
45+
'count' => 2,
46+
'files' => [
47+
$this->formatPath($this->getResourcesPath().'/views/foo.blade.php'),
4148
],
4249
],
43-
'messages.dummy.component' => [
50+
'messages.nested.missing' => [
4451
'count' => 1,
4552
'files' => [
46-
$this->formatPath($this->getResourcesPath().'/components/dummy-component.blade.php'),
53+
$this->formatPath($this->getResourcesPath().'/views/foo.blade.php'),
4754
],
4855
],
49-
'messages.dummy.nested' => [
56+
'messages.title' => [
5057
'count' => 1,
5158
'files' => [
52-
$this->formatPath($this->getResourcesPath().'/views/dummy-view.blade.php'),
59+
$this->formatPath($this->getAppPath().'/Foo.php'),
5360
],
5461
],
55-
'messages.dummy.view' => [
56-
'count' => 3,
62+
'users/account.title' => [
63+
'count' => 1,
5764
'files' => [
58-
$this->formatPath($this->getResourcesPath().'/components/dummy-component.blade.php'),
59-
$this->formatPath($this->getResourcesPath().'/views/dummy-view.blade.php'),
65+
$this->formatPath($this->getResourcesPath().'/components/users/account.blade.php'),
6066
],
6167
],
6268
]);
@@ -72,25 +78,24 @@
7278
$dead = $translator->getDeadTranslations('fr');
7379

7480
expect($dead->keys())->toBe([
75-
'All rights reserved.',
81+
'This one is dead',
7682
]);
7783

7884
});
7985

80-
it('gets missing translations', function () {
86+
it('gets untranslated translations', function () {
8187
$translator = new Translator(
8288
driver: $this->getJsonDriver(),
8389
searchcodeService: $this->getSearchCodeService()
8490
);
8591

8692
$keys = $translator->getUntranslatedTranslations(
8793
source: 'fr',
88-
target: 'en'
94+
target: 'it'
8995
);
9096

9197
expect($keys->toArray())->toBe([
92-
'All rights reserved.' => 'Tous droits réservés.',
93-
'This one is used.' => 'Celui-ci est utilisé.',
98+
'This one is untranslated' => 'Celui-ci est manquant',
9499
]);
95100

96101
});

0 commit comments

Comments
 (0)