Skip to content

Commit

Permalink
Keep numbered keys as strings in PHP lang files (#37)
Browse files Browse the repository at this point in the history
* Keep numbered keys as strings in PHP lang files

* Fix styling

* Fix test for Windows

Co-authored-by: amiranagram <amiranagram@users.noreply.github.com>
  • Loading branch information
Amir Rami and amiranagram committed Apr 11, 2022
1 parent f075e17 commit a3cc83e
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
39 changes: 36 additions & 3 deletions src/Services/Writers/DefaultWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \(/" => '[',
Expand All @@ -63,7 +74,7 @@ protected function exportArray(array $contents): string
$export
);

return sprintf("<?php\n\nreturn %s;\n", $export);
return sprintf("<?php\n\nreturn %s;\n", str_replace("_$this->tempUuid", '', $export));
}

/**
Expand All @@ -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();
}
}
4 changes: 3 additions & 1 deletion tests/Concerns/CreatesTestFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Amirami\Localizator\Tests\Concerns;

use Amirami\Localizator\Services\Writers\DefaultWriter;
use RuntimeException;

trait CreatesTestFiles
Expand Down Expand Up @@ -66,7 +67,8 @@ protected function createTestJsonLangFile(array $contents, string $locale): void
*/
protected function createTestDefaultLangFile(array $contents, string $fileName, string $locale): void
{
$export = sprintf("<?php\n\nreturn %s;\n", var_export($contents, true));
$writer = app(DefaultWriter::class);
$export = $writer->exportArray($contents);
$dir = lang_path($locale);

if (! file_exists($dir) && ! mkdir($dir, 0755) && ! is_dir($dir)) {
Expand Down
84 changes: 84 additions & 0 deletions tests/LocalizatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,88 @@ public function testLocalizeCommandWithMultilineMessagesAndSpaces(): void
// Cleanup.
self::flushDirectories('lang', 'views');
}

public function testIntTranslationKeysAreBeingSavedAsStrings(): void
{
$this->createTestView("{{ __('errors.401.title') }}<br/>{{ __('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'
<?php
return [
'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.',
],
];

PHP;

$this->assertSame(preg_replace('/\r\n|\r|\n/', "\n", $expected), $contents);
}

public function testIntTranslationNestedKeysAreBeingSavedAsStrings(): void
{
$this->createTestView("{{ __('errors.4.401') }}<br/>{{ __('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'
<?php
return [
'4' => [
'401' => '401 - Unauthorized',
'404' => '404 - Page Not Found',
],
];

PHP;

$this->assertSame(preg_replace('/\r\n|\r|\n/', "\n", $expected), $contents);
}
}

0 comments on commit a3cc83e

Please sign in to comment.