Skip to content

Commit 29d2a23

Browse files
committed
remove array-align
1 parent 08adb76 commit 29d2a23

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

src/PrettyVarExport.php

+32-22
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,39 @@
44

55
class PrettyVarExport
66
{
7-
// code from https://gist.github.com/lithrel/a224edb1ed2975992c73
8-
public function call($var, array $opts = [])
7+
public function call($translations)
98
{
10-
$opts = array_merge(['indent' => '', 'tab' => ' ', 'array-align' => false], $opts);
11-
switch (gettype($var)) {
12-
case 'array':
13-
$r = [];
14-
$indexed = array_keys($var) === range(0, count($var) - 1);
15-
$lengths = array_map('strlen', array_map('trim', array_keys($var)));
16-
$maxLength = ($opts['array-align'] && count($lengths) > 0) ? max($lengths) + 2 : 0;
17-
foreach ($var as $key => $value) {
18-
$key = str_replace("'' . \"\\0\" . '*' . \"\\0\" . ", "", $this->call($key));
19-
$r[] = $opts['indent'] . $opts['tab']
20-
. ($indexed ? '' : str_pad($key, $maxLength) . ' => ')
21-
. $this->call($value, array_merge($opts, ['indent' => $opts['indent'] . $opts['tab']]));
22-
}
23-
return "[\n" . implode(",\n", $r) . "\n" . $opts['indent'] . "]";
24-
case 'boolean':
25-
return $var ? 'true' : 'false';
26-
case 'NULL':
27-
return 'null';
28-
default:
29-
return var_export($var, true);
9+
$content = "[";
10+
11+
$content .= $this->stringLineMaker($translations);
12+
13+
$content .= "\n]";
14+
15+
return $content;
16+
}
17+
18+
/**
19+
* Write the lines of the inner array of the language file.
20+
*
21+
* @param $array
22+
* @return string
23+
*/
24+
private function stringLineMaker($array, $prepend = '')
25+
{
26+
$output = '';
27+
28+
foreach ($array as $key => $value) {
29+
if (is_array($value)) {
30+
$value = $this->stringLineMaker($value, $prepend.' ');
31+
32+
$output .= "\n{$prepend} '{$key}' => [{$value}\n{$prepend} ],";
33+
} else {
34+
$value = str_replace('\"', '"', addslashes($value));
35+
36+
$output .= "\n{$prepend} '{$key}' => '{$value}',";
37+
}
3038
}
39+
40+
return $output;
3141
}
3242
}

src/SourceSaver.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,17 @@ public function call($sourceEdit, $sourceLocale)
5454

5555
if ($this->filesystem->exists($groupFile)) {
5656
$translations = $this->filesystem->getRequire($groupFile);
57-
57+
dd($translations);
5858
$translations = $this->applySourceEditInTranslations($translations, $sourceEdit);
5959

6060
$fileContent = <<<'EOT'
61-
<?php
61+
<?php
62+
6263
return {{translations}};
6364
EOT;
65+
$fileContent = rtrim($fileContent);
6466

65-
$prettyTranslationsExport = $this->prettyVarExport->call($translations, ['array-align' => true]);
67+
$prettyTranslationsExport = $this->prettyVarExport->call($translations);
6668
$fileContent = str_replace('{{translations}}', $prettyTranslationsExport, $fileContent);
6769

6870
$this->filesystem->put($groupFile, $fileContent);

src/TranslationSaver.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ private function save($locale, $group, $translations)
6868
$this->filesystem->makeDirectory($dir, 0777, true, true);
6969

7070
$fileContent = <<<'EOT'
71-
<?php
71+
<?php
72+
7273
return {{translations}};
7374
EOT;
75+
$fileContent = rtrim($fileContent);
7476

75-
$prettyTranslationsExport = $this->prettyVarExport->call($translations, ['array-align' => true]);
77+
$prettyTranslationsExport = $this->prettyVarExport->call($translations);
7678
$fileContent = str_replace('{{translations}}', $prettyTranslationsExport, $fileContent);
7779

7880
$this->filesystem->put($dir . DIRECTORY_SEPARATOR . $group . '.php', $fileContent);

0 commit comments

Comments
 (0)