Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to dump also non dotted translations #4

Merged
merged 2 commits into from
May 12, 2024
Merged
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
29 changes: 27 additions & 2 deletions src/TranslationDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@

public function dump(array $translationKeys): void
{
$dottedStrings = $this->filterTranslationKeys($translationKeys);
$dottedStrings = $this->filterForDottedTranslationKeys($translationKeys);
$this->dumpDottedKeys($dottedStrings);
$this->dumpNonDottedKeys(array_unique(array_diff($translationKeys, $dottedStrings)));
}

private function dumpDottedKeys(array $dottedStrings): void
{
$result = $this->transformDottedStringsToArray($dottedStrings);

foreach ($result as $key => $value) {
Expand All @@ -36,7 +42,7 @@
}
}

private function filterTranslationKeys(array $unfilteredKeys): array
private function filterForDottedTranslationKeys(array $unfilteredKeys): array
{
$keys = array_filter(
$unfilteredKeys,
Expand Down Expand Up @@ -111,4 +117,23 @@

return $merged;
}

private function dumpNonDottedKeys(array $nonDottedKeys): void
{
if (empty($nonDottedKeys)) {
return;
}
$file = "{$this->languageFilePath}/{$this->locale}.json";
if ($this->filesystem->exists($file)) {
$existingKeys = json_decode($this->filesystem->get($file), true);
} else {
$existingKeys = [];

Check warning on line 130 in src/TranslationDumper.php

View check run for this annotation

Codecov / codecov/patch

src/TranslationDumper.php#L130

Added line #L130 was not covered by tests
}

$nonDottedKeys = array_combine($nonDottedKeys, array_map(fn ($key) => $this->dumpPrefix.$key, $nonDottedKeys));
$keys = array_merge($existingKeys, $nonDottedKeys);
ksort($keys, SORT_NATURAL | SORT_FLAG_CASE);
$this->filesystem->ensureDirectoryExists($this->languageFilePath);
$this->filesystem->put($file, json_encode($keys, JSON_PRETTY_PRINT).PHP_EOL);
}
}
44 changes: 44 additions & 0 deletions tests/Feature/TranslationDumperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);

namespace Bambamboole\LaravelTranslationDumper\Tests\Feature;

use Bambamboole\LaravelTranslationDumper\ArrayExporter;
use Bambamboole\LaravelTranslationDumper\TranslationDumper;
use Illuminate\Filesystem\Filesystem;
use PHPUnit\Framework\TestCase;

class TranslationDumperTest extends TestCase
{
private const TEST_LANGUAGE_PATH = __DIR__.'/fixtures/lang';

public function testItDumpsKeysAsExpected(): void
{
$fs = new Filesystem();
$testFolderName = uniqid();
$workingPath = str_replace('lang', $testFolderName, self::TEST_LANGUAGE_PATH);

$fs->copyDirectory(self::TEST_LANGUAGE_PATH, $workingPath);
$this->createSubject($workingPath)->dump(['test.dotted.additional', 'test undotted key', 'word', 'Works.']);

$files = $fs->allFiles($workingPath);

foreach ($files as $file) {
$this->assertFileEquals(
str_replace($testFolderName, 'expected', $file->getPathname()),
$file->getPathname(),
);
}

$fs->deleteDirectory($workingPath);
}

private function createSubject(string $folder): TranslationDumper
{
return new TranslationDumper(
new Filesystem(),
new ArrayExporter(),
$folder,
'en',
);
}
}
6 changes: 6 additions & 0 deletions tests/Feature/fixtures/expected/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"test undotted key": "x-test undotted key",
"This is a non dotted translation key": "This is a non dotted translation key",
"word": "x-word",
"Works.": "x-Works."
}
8 changes: 8 additions & 0 deletions tests/Feature/fixtures/expected/en/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

return [
'dotted' => [
'additional' => 'x-test.dotted.additional',
'nested' => 'dotted nested key',
],
];
3 changes: 3 additions & 0 deletions tests/Feature/fixtures/lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"This is a non dotted translation key": "This is a non dotted translation key"
}
7 changes: 7 additions & 0 deletions tests/Feature/fixtures/lang/en/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

return [
'dotted' => [
'nested' => 'dotted nested key',
],
];
2 changes: 1 addition & 1 deletion tests/Unit/LaravelTranslationDumperServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testItDoesOnlyPublishConfigIfAppIsRunningInConsole(): void
self::assertEquals(
[
LaravelTranslationDumperServiceProvider::class => [
dirname(__DIR__).'/config/config.php' => '/foo/bar/config',
dirname(__DIR__, 2).'/config/config.php' => '/foo/bar/config',
],
],
$serviceProvider::$publishes,
Expand Down
Loading