Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/Console/Commands/FindMissingTranslationStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Console\Command;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Symfony\Component\Console\Formatter\OutputFormatter;
Expand Down Expand Up @@ -131,21 +132,31 @@ protected function printErrors(array $errors, OutputInterface $output): void
* @return \Illuminate\Support\Collection
*/
protected function findInArray(string $baseLocale, mixed $locale)

{
if ($baseLocale === $locale) {
return Collection::empty();
}
return Collection::make($this->files->files(lang_path($baseLocale)))
->mapWithKeys(function (SplFileInfo $file) {
return [$file->getFilenameWithoutExtension() => $this->translator->get($file->getFilenameWithoutExtension())];
})
->dot()
->keys()

$data = Collection::make($this->translator->get('*'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you let me know what this part does?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. $this->translator->get('*') loads the global (non-namespaced) translations in lang/{$baseLocale}.json.

I wonder if it might be better to use the found translations via scanning to build a list of groups/namespaces to load, that would support non-standard loaders.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the point of the lang/{lang}.json, that the key is already in the base language?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't think there's anything technically preventing you from using a json file for the base locale, but it doesn't seem like it is very common.


if ($this->files->exists(lang_path($baseLocale))) {
$data = $data->merge(
Arr::dot(
Collection::make($this->files->files(lang_path($baseLocale)))
->mapWithKeys(function (SplFileInfo $file) {
return [$file->getFilenameWithoutExtension() => $this->translator->get($file->getFilenameWithoutExtension())];
})
->toArray()
)
);
}

return $data->keys()
->filter(function ($key) use ($locale) {
return !$this->translator->hasForLocale($key, $locale);
});
}

/**
* @return mixed
*/
Expand All @@ -161,4 +172,4 @@ protected function collectFiles()
return Str::endsWith($file->getExtension(), 'php');
});
}
}
}
47 changes: 47 additions & 0 deletions tests/Console/Commands/FindMissingTranslationStringsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace CodingSocks\LostInTranslation\Tests\Console\Commands;

use CodingSocks\LostInTranslation\LostInTranslationServiceProvider;
use CodingSocks\LostInTranslation\Tests\TestCase;
use Illuminate\Config\Repository;
use Illuminate\Support\Facades\Artisan;

class FindMissingTranslationStringsTest extends TestCase
{
public function testBasicCommand(): void
{
$exit_code = $this
->withoutMockingConsoleOutput()
->artisan('lost-in-translation:find ja --no-progress --sorted');

$output = Artisan::output();

$this->assertSame("foobar\nglobal_key\nmessages.namespaced_key\n", $output);
$this->assertSame(0, $exit_code);
}

public function testMissingLocale(): void
{
$exit_code = $this
->withoutMockingConsoleOutput()
->artisan('lost-in-translation:find zh --no-progress --sorted');

$output = Artisan::output();

$this->assertSame("foobar\nglobal_key\nkey_in_both\nmessages.namespaced_key\n", $output);
$this->assertSame(0, $exit_code);
}

public function defineEnvironment($app)
{
$appPath = __DIR__ . '/../../application';
$app->useLangPath($appPath . '/lang');

tap($app['config'], static function (Repository $config) use ($appPath) {
$config->set('lost-in-translation.paths', [
$appPath . '/resources/views'
]);
});
}
}
4 changes: 4 additions & 0 deletions tests/application/lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"global_key": "this is a global key",
"key_in_both": "both should have this key"
}
3 changes: 3 additions & 0 deletions tests/application/lang/en/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php return [
'namespaced_key' => 'this is a namespaced key',
];
4 changes: 4 additions & 0 deletions tests/application/lang/ja.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"another_global_key": "this is another global key",
"key_in_both": "both should have this key"
}
3 changes: 3 additions & 0 deletions tests/application/lang/ja/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php return [
'another_namespaced_key' => 'this is another namespaced key',
];
1 change: 1 addition & 0 deletions tests/application/resources/views/sample.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ __('foobar') }}