mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
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:
parent
2753629dbe
commit
1930ed4d6a
@ -113,10 +113,10 @@ return [
|
||||
'users_social_connected' => ':socialAccount account was successfully attached to 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
|
||||
// translated in the language-specific files.
|
||||
// DELETE BELOW IF COPIED FROM EN
|
||||
///////////////////////////////////
|
||||
//! Since these labels are already localized this array does not need to be
|
||||
//! translated in the language-specific files.
|
||||
//! DELETE BELOW IF COPIED FROM EN
|
||||
//!////////////////////////////////
|
||||
'language_select' => [
|
||||
'en' => 'English',
|
||||
'ar' => 'العربية',
|
||||
@ -137,5 +137,5 @@ return [
|
||||
'zh_CN' => '简体中文',
|
||||
'zh_TW' => '繁體中文'
|
||||
]
|
||||
///////////////////////////////////
|
||||
//!////////////////////////////////
|
||||
];
|
||||
|
@ -15,7 +15,7 @@ if (count($args) < 2) {
|
||||
|
||||
$lang = formatLocale($args[0]);
|
||||
$fileName = explode('.', $args[1])[0];
|
||||
$filesNames = [$fileName];
|
||||
$fileNames = [$fileName];
|
||||
if ($fileName === '--all') {
|
||||
$fileNames = getTranslationFileNames();
|
||||
}
|
||||
@ -43,6 +43,7 @@ function formatFileContents(string $lang, string $fileName) : string {
|
||||
// Start formatted content
|
||||
$formatted = [];
|
||||
$mode = 'header';
|
||||
$skipArray = false;
|
||||
$arrayKeys = [];
|
||||
|
||||
foreach ($enLines as $index => $line) {
|
||||
@ -54,6 +55,18 @@ function formatFileContents(string $lang, string $fileName) : string {
|
||||
|
||||
if ($mode === 'body') {
|
||||
$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
|
||||
if (strpos($trimLine, '//') === 0) {
|
||||
@ -63,9 +76,13 @@ function formatFileContents(string $lang, string $fileName) : string {
|
||||
|
||||
// Arrays
|
||||
$arrayStartMatch = preg_match('/^\'(.*)\'\s+?=>\s+?\[(\],)?\s*?$/', $trimLine, $matches);
|
||||
$arrayEndMatch = preg_match('/]\s*,\s*$/', $trimLine);
|
||||
|
||||
$indent = count($arrayKeys) + 1;
|
||||
if ($arrayStartMatch === 1) {
|
||||
if ($fileName === 'settings' && $matches[1] === 'language_select') {
|
||||
$skipArray = true;
|
||||
continue;
|
||||
}
|
||||
$arrayKeys[] = $matches[1];
|
||||
$formatted[$index] = str_repeat(" ", $indent * 4) . str_pad("'{$matches[1]}'", $longestKeyLength) . "=> [";
|
||||
if ($arrayEndMatch !== 1) continue;
|
||||
@ -82,7 +99,7 @@ function formatFileContents(string $lang, string $fileName) : string {
|
||||
}
|
||||
|
||||
// Translation
|
||||
$translationMatch = preg_match('/^\'(.*)\'\s+?=>\s+?\'(.*)?\'.+?$/', $trimLine, $matches);
|
||||
$translationMatch = preg_match('/^\'(.*)\'\s+?=>\s+?[\'"](.*)?[\'"].+?$/', $trimLine, $matches);
|
||||
if ($translationMatch === 1) {
|
||||
$key = $matches[1];
|
||||
$keys = array_merge($arrayKeys, [$key]);
|
||||
@ -137,8 +154,14 @@ function formatFileContents(string $lang, string $fileName) : string {
|
||||
* @return string
|
||||
*/
|
||||
function formatTranslationLine(string $key, string $value, int $indent = 1, int $keyPad = 1) : string {
|
||||
$escapedValue = str_replace("'", "\\'", $value);
|
||||
return str_repeat(" ", $indent * 4) . str_pad("'{$key}'", $keyPad, ' ') ."=> '{$escapedValue}',";
|
||||
$start = str_repeat(" ", $indent * 4) . str_pad("'{$key}'", $keyPad, ' ');
|
||||
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);
|
||||
$top = max($top, $keyLen);
|
||||
}
|
||||
return $top + 3;
|
||||
return min(35, $top + 2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user