Made some further fixes to the formatting script

Takes into account single and double quotes.
Ignores //! comments and the 'language_select' array.

Language files may need some cleaning up and may encounter some other bugs when running.
This commit is contained in:
Dan Brown 2018-12-16 14:04:04 +00:00
parent 2753629dbe
commit 1930ed4d6a
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 35 additions and 12 deletions

View File

@ -113,10 +113,10 @@ return [
'users_social_connected' => ':socialAccount account was successfully attached to your profile.', 'users_social_connected' => ':socialAccount account was successfully attached to your profile.',
'users_social_disconnected' => ':socialAccount account was successfully disconnected from your profile.', 'users_social_disconnected' => ':socialAccount account was successfully disconnected from your profile.',
// Since these labels are already localized this array does not need to be //! Since these labels are already localized this array does not need to be
// translated in the language-specific files. //! translated in the language-specific files.
// DELETE BELOW IF COPIED FROM EN //! DELETE BELOW IF COPIED FROM EN
/////////////////////////////////// //!////////////////////////////////
'language_select' => [ 'language_select' => [
'en' => 'English', 'en' => 'English',
'ar' => 'العربية', 'ar' => 'العربية',
@ -137,5 +137,5 @@ return [
'zh_CN' => '简体中文', 'zh_CN' => '简体中文',
'zh_TW' => '繁體中文' 'zh_TW' => '繁體中文'
] ]
/////////////////////////////////// //!////////////////////////////////
]; ];

View File

@ -15,7 +15,7 @@ if (count($args) < 2) {
$lang = formatLocale($args[0]); $lang = formatLocale($args[0]);
$fileName = explode('.', $args[1])[0]; $fileName = explode('.', $args[1])[0];
$filesNames = [$fileName]; $fileNames = [$fileName];
if ($fileName === '--all') { if ($fileName === '--all') {
$fileNames = getTranslationFileNames(); $fileNames = getTranslationFileNames();
} }
@ -43,9 +43,10 @@ function formatFileContents(string $lang, string $fileName) : string {
// Start formatted content // Start formatted content
$formatted = []; $formatted = [];
$mode = 'header'; $mode = 'header';
$skipArray = false;
$arrayKeys = []; $arrayKeys = [];
foreach($enLines as $index => $line) { foreach ($enLines as $index => $line) {
$trimLine = trim($line); $trimLine = trim($line);
if ($mode === 'header') { if ($mode === 'header') {
$formatted[$index] = $line; $formatted[$index] = $line;
@ -54,6 +55,18 @@ function formatFileContents(string $lang, string $fileName) : string {
if ($mode === 'body') { if ($mode === 'body') {
$matches = []; $matches = [];
$arrayEndMatch = preg_match('/]\s*,\s*$/', $trimLine);
if ($skipArray) {
if ($arrayEndMatch) $skipArray = false;
continue;
}
// Comment to ignore
if (strpos($trimLine, '//!') === 0) {
$formatted[$index] = "";
continue;
}
// Comment // Comment
if (strpos($trimLine, '//') === 0) { if (strpos($trimLine, '//') === 0) {
@ -63,9 +76,13 @@ function formatFileContents(string $lang, string $fileName) : string {
// Arrays // Arrays
$arrayStartMatch = preg_match('/^\'(.*)\'\s+?=>\s+?\[(\],)?\s*?$/', $trimLine, $matches); $arrayStartMatch = preg_match('/^\'(.*)\'\s+?=>\s+?\[(\],)?\s*?$/', $trimLine, $matches);
$arrayEndMatch = preg_match('/]\s*,\s*$/', $trimLine);
$indent = count($arrayKeys) + 1; $indent = count($arrayKeys) + 1;
if ($arrayStartMatch === 1) { if ($arrayStartMatch === 1) {
if ($fileName === 'settings' && $matches[1] === 'language_select') {
$skipArray = true;
continue;
}
$arrayKeys[] = $matches[1]; $arrayKeys[] = $matches[1];
$formatted[$index] = str_repeat(" ", $indent * 4) . str_pad("'{$matches[1]}'", $longestKeyLength) . "=> ["; $formatted[$index] = str_repeat(" ", $indent * 4) . str_pad("'{$matches[1]}'", $longestKeyLength) . "=> [";
if ($arrayEndMatch !== 1) continue; if ($arrayEndMatch !== 1) continue;
@ -82,7 +99,7 @@ function formatFileContents(string $lang, string $fileName) : string {
} }
// Translation // Translation
$translationMatch = preg_match('/^\'(.*)\'\s+?=>\s+?\'(.*)?\'.+?$/', $trimLine, $matches); $translationMatch = preg_match('/^\'(.*)\'\s+?=>\s+?[\'"](.*)?[\'"].+?$/', $trimLine, $matches);
if ($translationMatch === 1) { if ($translationMatch === 1) {
$key = $matches[1]; $key = $matches[1];
$keys = array_merge($arrayKeys, [$key]); $keys = array_merge($arrayKeys, [$key]);
@ -137,8 +154,14 @@ function formatFileContents(string $lang, string $fileName) : string {
* @return string * @return string
*/ */
function formatTranslationLine(string $key, string $value, int $indent = 1, int $keyPad = 1) : string { function formatTranslationLine(string $key, string $value, int $indent = 1, int $keyPad = 1) : string {
$escapedValue = str_replace("'", "\\'", $value); $start = str_repeat(" ", $indent * 4) . str_pad("'{$key}'", $keyPad, ' ');
return str_repeat(" ", $indent * 4) . str_pad("'{$key}'", $keyPad, ' ') ."=> '{$escapedValue}',"; if (strpos($value, "\n") !== false) {
$escapedValue = '"' . str_replace("\n", '\n', $value) . '"';
$escapedValue = '"' . str_replace('"', '\"', $escapedValue) . '"';
} else {
$escapedValue = "'" . str_replace("'", "\\'", $value) . "'";
}
return "{$start} => {$escapedValue},";
} }
/** /**
@ -153,7 +176,7 @@ function calculateKeyPadding(array $array) : int {
$keyLen = strlen($key); $keyLen = strlen($key);
$top = max($top, $keyLen); $top = max($top, $keyLen);
} }
return $top + 3; return min(35, $top + 2);
} }
/** /**