Small translate improvement

This commit is contained in:
the-djmaze 2023-12-11 13:50:36 +01:00
parent 8ce2a244e9
commit f47b77347d

View file

@ -23,7 +23,10 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) {
if (!$zip->open($file, ZIPARCHIVE::CREATE)) { if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
exit("Failed to create zip"); exit("Failed to create zip");
} }
$lang = $lang ?: 'new'; if (!$lang) {
$lang = (empty($_POST['lang']) || !preg_match('/^[a-z]{2}(-[A-Z]{2})?$/D',$_POST['lang']))
? 'new' : $_POST['lang'];
}
$zip->addFromString("{$lang}/admin.json", toJSON($_POST['admin'])); $zip->addFromString("{$lang}/admin.json", toJSON($_POST['admin']));
$zip->addFromString("{$lang}/user.json", toJSON($_POST['user'])); $zip->addFromString("{$lang}/user.json", toJSON($_POST['user']));
$zip->close(); $zip->close();
@ -45,9 +48,9 @@ require 'demo/index.php';
$root = APP_VERSION_ROOT_PATH . 'app/localization'; $root = APP_VERSION_ROOT_PATH . 'app/localization';
$en = [ $en = [
'user' => '',
'admin' => '', 'admin' => '',
// 'static' => '', // 'static' => '',
'user' => ''
]; ];
foreach ($en as $name => $data) { foreach ($en as $name => $data) {
$en[$name] = json_decode(file_get_contents("{$root}/en/{$name}.json"), true); $en[$name] = json_decode(file_get_contents("{$root}/en/{$name}.json"), true);
@ -102,17 +105,20 @@ echo '<!DOCTYPE html>
<thead> <thead>
<tr> <tr>
<th>en</th> <th>en</th>
<th>'.$lang.'</th> <th>'.($lang ?: '<input name="lang" required="" pattern="[a-z]{2}(-[A-Z]{2})?">').'</th>
</tr> </tr>
</thead>'; </thead>';
foreach ($en as $name => $sections) { foreach ($en as $name => $sections) {
echo '<tbody><tr class="file"><th colspan="3">'.$name.'</th></tr>'; echo '<tbody><tr class="file"><th colspan="3">'.$name.'</th></tr>';
$data = is_readable("{$root}/{$lang}/{$name}.json") ? json_decode(file_get_contents("{$root}/{$lang}/{$name}.json"), true) : []; $data = $sections;
if ($lang && is_readable("{$root}/{$lang}/{$name}.json")) {
$data = json_decode(file_get_contents("{$root}/{$lang}/{$name}.json"), true);
}
foreach ($sections as $section => $values) { foreach ($sections as $section => $values) {
if (is_array($values)) { if (is_array($values)) {
echo '<tr class="section"><td colspan="3">'.$section.'</td><td></td></tr>'; echo '<tr class="section"><td colspan="3">'.$section.'</td><td></td></tr>';
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
echo '<tr'.(($lang && (empty($data[$section][$key]) || $value == $data[$section][$key])) ? ' class="untranslated"':'').'>'; echo '<tr'.((empty($data[$section][$key]) || $value == $data[$section][$key]) ? ' class="untranslated"':'').'>';
// echo '<td>'.$section.'/'.$key.'</td>'; // echo '<td>'.$section.'/'.$key.'</td>';
echo '<td>'.htmlspecialchars($value).'</td>'; echo '<td>'.htmlspecialchars($value).'</td>';
echo '<td><textarea name="'."{$name}[{$section}][{$key}]".'">'.htmlspecialchars($data[$section][$key] ?? '').'</textarea></td>'; echo '<td><textarea name="'."{$name}[{$section}][{$key}]".'">'.htmlspecialchars($data[$section][$key] ?? '').'</textarea></td>';