Skip to content

Commit a3cc83e

Browse files
Amir Ramiamiranagram
Amir Rami
andauthored
Keep numbered keys as strings in PHP lang files (#37)
* Keep numbered keys as strings in PHP lang files * Fix styling * Fix test for Windows Co-authored-by: amiranagram <amiranagram@users.noreply.github.com>
1 parent f075e17 commit a3cc83e

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed

Diff for: src/Services/Writers/DefaultWriter.php

+36-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@
77
use Amirami\Localizator\Contracts\Writable;
88
use Illuminate\Filesystem\Filesystem;
99
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Str;
1011

1112
class DefaultWriter implements Writable
1213
{
14+
/**
15+
* @var string
16+
*/
17+
protected $tempUuid;
18+
19+
public function __construct()
20+
{
21+
$this->tempUuid = Str::uuid();
22+
}
23+
1324
/**
1425
* @param string $locale
1526
* @param Translatable $keys
@@ -46,9 +57,9 @@ protected function elevate(Translatable $keys): DefaultKeyCollection
4657
* @param array $contents
4758
* @return string
4859
*/
49-
protected function exportArray(array $contents): string
60+
public function exportArray(array $contents): string
5061
{
51-
$export = var_export($contents, true);
62+
$export = var_export($this->temporarilyModifyIntKeys($contents), true);
5263

5364
$patterns = [
5465
"/array \(/" => '[',
@@ -63,7 +74,7 @@ protected function exportArray(array $contents): string
6374
$export
6475
);
6576

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

6980
/**
@@ -75,4 +86,26 @@ protected function getFile(string $locale, string $fileName): string
7586
{
7687
return lang_path($locale.DIRECTORY_SEPARATOR.$fileName.'.php');
7788
}
89+
90+
/**
91+
* @param array $contents
92+
* @return array
93+
*/
94+
public function temporarilyModifyIntKeys(array $contents): array
95+
{
96+
$collection = collect($contents)
97+
->mapWithKeys(function ($value, $key) {
98+
if (is_int($key)) {
99+
$key .= '_'.$this->tempUuid;
100+
}
101+
102+
if (is_array($value)) {
103+
$value = $this->temporarilyModifyIntKeys($value);
104+
}
105+
106+
return [$key => $value];
107+
});
108+
109+
return $collection->toArray();
110+
}
78111
}

Diff for: tests/Concerns/CreatesTestFiles.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Amirami\Localizator\Tests\Concerns;
44

5+
use Amirami\Localizator\Services\Writers\DefaultWriter;
56
use RuntimeException;
67

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

7274
if (! file_exists($dir) && ! mkdir($dir, 0755) && ! is_dir($dir)) {

Diff for: tests/LocalizatorTest.php

+84
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,88 @@ public function testLocalizeCommandWithMultilineMessagesAndSpaces(): void
211211
// Cleanup.
212212
self::flushDirectories('lang', 'views');
213213
}
214+
215+
public function testIntTranslationKeysAreBeingSavedAsStrings(): void
216+
{
217+
$this->createTestView("{{ __('errors.401.title') }}<br/>{{ __('errors.401.message') }}");
218+
219+
$this->createTestDefaultLangFile([
220+
'401' => [
221+
'title' => '401 - Unauthorized',
222+
'message' => 'Sorry, you are not authorized to view this page.',
223+
],
224+
'404' => [
225+
'title' => '404 - Page Not Found',
226+
'message' => 'Sorry, we couldn\'t find this page.',
227+
],
228+
], 'errors', 'en');
229+
230+
config(['localizator.sort' => false]);
231+
232+
// Run localize command.
233+
$this->artisan('localize', ['lang' => 'en'])
234+
->assertExitCode(0);
235+
236+
// Do created locale files exist?
237+
self::assertDefaultLangFilesExist(['en'], ['errors']);
238+
239+
// Get exported contents.
240+
$path = $this->getLangFilePath('en'.DIRECTORY_SEPARATOR.'errors.php');
241+
$contents = file_get_contents($path);
242+
$expected = <<<'PHP'
243+
<?php
244+
245+
return [
246+
'401' => [
247+
'title' => '401 - Unauthorized',
248+
'message' => 'Sorry, you are not authorized to view this page.',
249+
],
250+
'404' => [
251+
'title' => '404 - Page Not Found',
252+
'message' => 'Sorry, we couldn\'t find this page.',
253+
],
254+
];
255+
256+
PHP;
257+
258+
$this->assertSame(preg_replace('/\r\n|\r|\n/', "\n", $expected), $contents);
259+
}
260+
261+
public function testIntTranslationNestedKeysAreBeingSavedAsStrings(): void
262+
{
263+
$this->createTestView("{{ __('errors.4.401') }}<br/>{{ __('errors.4.404') }}");
264+
265+
$this->createTestDefaultLangFile([
266+
'4' => [
267+
'401' => '401 - Unauthorized',
268+
'404' => '404 - Page Not Found',
269+
],
270+
], 'errors', 'en');
271+
272+
config(['localizator.sort' => false]);
273+
274+
// Run localize command.
275+
$this->artisan('localize', ['lang' => 'en'])
276+
->assertExitCode(0);
277+
278+
// Do created locale files exist?
279+
self::assertDefaultLangFilesExist(['en'], ['errors']);
280+
281+
// Get exported contents.
282+
$path = $this->getLangFilePath('en'.DIRECTORY_SEPARATOR.'errors.php');
283+
$contents = file_get_contents($path);
284+
$expected = <<<'PHP'
285+
<?php
286+
287+
return [
288+
'4' => [
289+
'401' => '401 - Unauthorized',
290+
'404' => '404 - Page Not Found',
291+
],
292+
];
293+
294+
PHP;
295+
296+
$this->assertSame(preg_replace('/\r\n|\r|\n/', "\n", $expected), $contents);
297+
}
214298
}

0 commit comments

Comments
 (0)