Skip to content

output translation array as short array syntax #469

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,27 @@ public function missingKey($namespace, $group, $key)
}
}

protected static function arrayToShortSyntax($data, $indent = '')
{
if (!is_array($data)) {
return var_export($data, true);
}

$isAssoc = array_keys($data) !== range(0, count($data) - 1);
$lines = [];
$innerIndent = $indent . ' ';

foreach ($data as $key => $value) {
$keyPart = $isAssoc ? static::arrayToShortSyntax($key) . ' => ' : '';
$valuePart = is_array($value)
? static::arrayToShortSyntax($value, $innerIndent)
: var_export($value, true);
$lines[] = $innerIndent . $keyPart . $valuePart;
}

return "[\n" . implode(",\n", $lines) . "\n" . $indent . "]";
}

public function exportTranslations($group = null, $json = false)
{
$group = basename($group);
Expand Down Expand Up @@ -300,7 +321,7 @@ public function exportTranslations($group = null, $json = false)
$path = $path.DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.$group.'.php';
}

$output = "<?php\n\nreturn ".var_export($translations, true).';'.\PHP_EOL;
$output = "<?php\n\nreturn " . static::arrayToShortSyntax($translations) . ";\n";
$this->files->put($path, $output);
}
}
Expand Down