diff --git a/src/Services/Writers/DefaultWriter.php b/src/Services/Writers/DefaultWriter.php index 61863f6..d42246f 100644 --- a/src/Services/Writers/DefaultWriter.php +++ b/src/Services/Writers/DefaultWriter.php @@ -7,9 +7,20 @@ use Amirami\Localizator\Contracts\Writable; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; +use Illuminate\Support\Str; class DefaultWriter implements Writable { + /** + * @var string + */ + protected $tempUuid; + + public function __construct() + { + $this->tempUuid = Str::uuid(); + } + /** * @param string $locale * @param Translatable $keys @@ -46,9 +57,9 @@ protected function elevate(Translatable $keys): DefaultKeyCollection * @param array $contents * @return string */ - protected function exportArray(array $contents): string + public function exportArray(array $contents): string { - $export = var_export($contents, true); + $export = var_export($this->temporarilyModifyIntKeys($contents), true); $patterns = [ "/array \(/" => '[', @@ -63,7 +74,7 @@ protected function exportArray(array $contents): string $export ); - return sprintf("tempUuid", '', $export)); } /** @@ -75,4 +86,26 @@ protected function getFile(string $locale, string $fileName): string { return lang_path($locale.DIRECTORY_SEPARATOR.$fileName.'.php'); } + + /** + * @param array $contents + * @return array + */ + public function temporarilyModifyIntKeys(array $contents): array + { + $collection = collect($contents) + ->mapWithKeys(function ($value, $key) { + if (is_int($key)) { + $key .= '_'.$this->tempUuid; + } + + if (is_array($value)) { + $value = $this->temporarilyModifyIntKeys($value); + } + + return [$key => $value]; + }); + + return $collection->toArray(); + } } diff --git a/tests/Concerns/CreatesTestFiles.php b/tests/Concerns/CreatesTestFiles.php index 8233245..5321e76 100644 --- a/tests/Concerns/CreatesTestFiles.php +++ b/tests/Concerns/CreatesTestFiles.php @@ -2,6 +2,7 @@ namespace Amirami\Localizator\Tests\Concerns; +use Amirami\Localizator\Services\Writers\DefaultWriter; use RuntimeException; trait CreatesTestFiles @@ -66,7 +67,8 @@ protected function createTestJsonLangFile(array $contents, string $locale): void */ protected function createTestDefaultLangFile(array $contents, string $fileName, string $locale): void { - $export = sprintf("exportArray($contents); $dir = lang_path($locale); if (! file_exists($dir) && ! mkdir($dir, 0755) && ! is_dir($dir)) { diff --git a/tests/LocalizatorTest.php b/tests/LocalizatorTest.php index 4334297..788c6b9 100644 --- a/tests/LocalizatorTest.php +++ b/tests/LocalizatorTest.php @@ -211,4 +211,88 @@ public function testLocalizeCommandWithMultilineMessagesAndSpaces(): void // Cleanup. self::flushDirectories('lang', 'views'); } + + public function testIntTranslationKeysAreBeingSavedAsStrings(): void + { + $this->createTestView("{{ __('errors.401.title') }}
{{ __('errors.401.message') }}"); + + $this->createTestDefaultLangFile([ + '401' => [ + 'title' => '401 - Unauthorized', + 'message' => 'Sorry, you are not authorized to view this page.', + ], + '404' => [ + 'title' => '404 - Page Not Found', + 'message' => 'Sorry, we couldn\'t find this page.', + ], + ], 'errors', 'en'); + + config(['localizator.sort' => false]); + + // Run localize command. + $this->artisan('localize', ['lang' => 'en']) + ->assertExitCode(0); + + // Do created locale files exist? + self::assertDefaultLangFilesExist(['en'], ['errors']); + + // Get exported contents. + $path = $this->getLangFilePath('en'.DIRECTORY_SEPARATOR.'errors.php'); + $contents = file_get_contents($path); + $expected = <<<'PHP' + [ + 'title' => '401 - Unauthorized', + 'message' => 'Sorry, you are not authorized to view this page.', + ], + '404' => [ + 'title' => '404 - Page Not Found', + 'message' => 'Sorry, we couldn\'t find this page.', + ], +]; + +PHP; + + $this->assertSame(preg_replace('/\r\n|\r|\n/', "\n", $expected), $contents); + } + + public function testIntTranslationNestedKeysAreBeingSavedAsStrings(): void + { + $this->createTestView("{{ __('errors.4.401') }}
{{ __('errors.4.404') }}"); + + $this->createTestDefaultLangFile([ + '4' => [ + '401' => '401 - Unauthorized', + '404' => '404 - Page Not Found', + ], + ], 'errors', 'en'); + + config(['localizator.sort' => false]); + + // Run localize command. + $this->artisan('localize', ['lang' => 'en']) + ->assertExitCode(0); + + // Do created locale files exist? + self::assertDefaultLangFilesExist(['en'], ['errors']); + + // Get exported contents. + $path = $this->getLangFilePath('en'.DIRECTORY_SEPARATOR.'errors.php'); + $contents = file_get_contents($path); + $expected = <<<'PHP' + [ + '401' => '401 - Unauthorized', + '404' => '404 - Page Not Found', + ], +]; + +PHP; + + $this->assertSame(preg_replace('/\r\n|\r|\n/', "\n", $expected), $contents); + } }