Skip to content

Commit

Permalink
Merge pull request #29 from LarsWiegers/json-files-instead-of-dirs
Browse files Browse the repository at this point in the history
supporting top level json files
  • Loading branch information
LarsWiegers authored Mar 30, 2023
2 parents f132e6a + f8ebb52 commit 74e73b5
Show file tree
Hide file tree
Showing 11 changed files with 1,164 additions and 877 deletions.
1,935 changes: 1,066 additions & 869 deletions composer.lock

Large diffs are not rendered by default.

46 changes: 38 additions & 8 deletions src/Console/Commands/CheckIfTranslationsAreAllThereCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,30 @@ public function handle()

foreach ($languages as $language) {

$fileKey = basename($key);
$fileNameWithoutKey = substr($key, 0, strpos($key, "**"));
$fileKey = basename($fileNameWithoutKey);
$keyWithoutFile = substr($key, strpos($key, "**") + 2, strlen($key));

$exists = array_key_exists($directory . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . $fileKey, $this->realLines);
$exists = $this->translationExistsAsJsonOrAsSubDir($directory, $language, $fileKey, $keyWithoutFile);

if ($this->isDirInExcludedDirectories($language)) {
continue;
}
if (!$exists) {
$fileName = Str::replace(['.php', '.json'], '', $fileKey);

$missing[] = $language . '.' . $fileName;
foreach($languages as $checkingLanguage) {
if(Str::contains($fileName, $checkingLanguage)) {
$fileName = str_replace($checkingLanguage, '', $fileName);
}
}

if(Str::contains($fileKey, $languages)) {
$missing[] = $language . '.' . $keyWithoutFile;
}else {
$missing[] = $language . '.' . $fileName . '.' . $keyWithoutFile;
}

}
}
}
Expand Down Expand Up @@ -149,10 +162,10 @@ public function handleFile($languageDir, $langFile): void
foreach ($lines as $index => $line) {
if (is_array($line)) {
foreach ($line as $index2 => $line2) {
$this->realLines[$languageDir . $fileName . '.' . $index . '.' . $index2] = $line2;
$this->realLines[$languageDir . $fileName . '.' . $index . '**' . $index2] = $line2;
}
} else {
$this->realLines[$languageDir . $fileName . '.' . $index] = $line;
$this->realLines[$languageDir . $fileName . '**' . $index] = $line;
}
}
}
Expand All @@ -177,7 +190,7 @@ private function getLanguages(string $directory): array
if ($handle = opendir($directory)) {
while (false !== ($languageDir = readdir($handle))) {
if ($languageDir !== '.' && $languageDir !== '..') {
$languages[] = $languageDir;
$languages[] = str_replace('.json', '', $languageDir);
}
}
}
Expand All @@ -196,9 +209,11 @@ private function getLanguages(string $directory): array
private function checkIfFileExistsForOtherLanguages($languages, $fileName, $baseDirectory): array
{
$languagesWhereFileIsMissing = [];

foreach ($languages as $language) {
if (!File::exists($baseDirectory . '/' . $language . '/' . $fileName)) {
if (
!File::exists($baseDirectory . '/' . $language . '/' . $fileName)
&& !File::exists($baseDirectory . '/' . $fileName)
) {
$languagesWhereFileIsMissing[] = $language;
}
}
Expand All @@ -216,4 +231,19 @@ private function isDirInExcludedDirectories($directoryToCheck): bool

return false;
}

/**
* @param $directory
* @param $language
* @param string $fileKey
* @return bool
*/
public function translationExistsAsJsonOrAsSubDir($directory, $language, string $fileKey, string $keyWithoutFile): bool
{
$existsAsSubDirValue = array_key_exists($directory . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . $fileKey . '**' . $keyWithoutFile, $this->realLines);

$fileKeyWithoutLangComponent = explode('.', $fileKey, 2)[1];
$existsAsJSONValue = array_key_exists($directory . DIRECTORY_SEPARATOR . $language . '.' . $fileKeyWithoutLangComponent . '**' . $keyWithoutFile, $this->realLines);
return $existsAsSubDirValue || $existsAsJSONValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Unit\Console\Commands;

use Illuminate\Support\Facades\Artisan;
use Tests\TestCase;

final class CheckIfTranslationsAreAllThereCommandTest extends TestCase
Expand All @@ -23,6 +24,7 @@ public function test_it_returns_errors_if_one_key_is_missing($directory)
$command = $this->artisan('translations:check', [
'--directory' => $directory,
]);

$command->expectsOutput('Missing the translation with key: nl.test.test_key');
}

Expand Down Expand Up @@ -144,6 +146,43 @@ public function test_we_can_exclude_two_directories($directory)
$command->assertExitCode(0);
}

public function test_it_handles_one_toplevel_language_file() {
$command = $this->artisan('translations:check', [
'--directory' => $this->jsonDir . 'toplevel_json_files/one',
]);

$command->expectsOutput('✔ All translations are okay!');

$command->assertExitCode(0);
}

public function test_it_handles_two_toplevel_language_file() {
$command = $this->artisan('translations:check', [
'--directory' => $this->jsonDir . 'toplevel_json_files/two',
]);

$command->expectsOutput('✔ All translations are okay!');

$command->assertExitCode(0);
}

public function test_it_handles_missing_key_in_toplevel_language_file() {
$command = $this->artisan('translations:check', [
'--directory' => $this->jsonDir . 'toplevel_json_files/missing_key_in_one_lang',
]);

$command->expectsOutput('Missing the translation with key: nl.test_key');

$command->assertExitCode(1);
}

public function test_it_handles_slashes_in_json_keys() {
$command = $this->artisan('translations:check', [
'--directory' => $this->jsonDir . 'toplevel_json_files/slashes_in_title',
]);

$command->assertExitCode(0);
}

public function one_missing_file_provider(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Unit\Console\Commands;

use Illuminate\Support\Facades\Artisan;
use Tests\TestCase;

final class CommandCanHandleErrorsTest extends TestCase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test_key": "test_value"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
3 changes: 3 additions & 0 deletions tests/resources/lang/json/toplevel_json_files/one/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test_key": "test_value"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"You cannot edit/update the super admin.": "test_value"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"You cannot edit/update the super admin.": "test_value"
}
3 changes: 3 additions & 0 deletions tests/resources/lang/json/toplevel_json_files/two/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test_key": "test_value"
}
3 changes: 3 additions & 0 deletions tests/resources/lang/json/toplevel_json_files/two/nl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test_key": "test_value"
}

0 comments on commit 74e73b5

Please sign in to comment.