more fixes
This commit is contained in:
32
scripts/check_fp_translations.php
Normal file
32
scripts/check_fp_translations.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$app = require_once __DIR__ . '/../bootstrap/app.php';
|
||||
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
|
||||
$kernel->bootstrap();
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
$file = $argv[1] ?? 'fp';
|
||||
|
||||
echo "Checking translations for file: $file\n";
|
||||
|
||||
$translationsCount = DB::table('translations')->where('file', $file)->count();
|
||||
$translationsSample = DB::table('translations')->where('file', $file)->limit(5)->get();
|
||||
|
||||
$activeLanguages = DB::table('active_languages')->where('type', $file)->get();
|
||||
$activeLanguagesCount = $activeLanguages->count();
|
||||
|
||||
echo "translations.count = $translationsCount\n";
|
||||
echo "active_languages.count = $activeLanguagesCount\n";
|
||||
echo "active_languages rows:\n";
|
||||
foreach ($activeLanguages as $al) {
|
||||
echo " - iso={$al->iso} active={$al->active} type={$al->type}\n";
|
||||
}
|
||||
|
||||
echo "sample translations:\n";
|
||||
foreach ($translationsSample as $t) {
|
||||
echo " - key={$t->keycode} file={$t->file} value=" . substr($t->value,0,80) . "...\n";
|
||||
}
|
||||
|
||||
exit(0);
|
||||
103
scripts/fill_translations_csv.php
Normal file
103
scripts/fill_translations_csv.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
// Fill CSV `Value` column from resources/lang files when available.
|
||||
$input = __DIR__ . '/../storage/app/translations_missing_admin_converted.csv';
|
||||
$output = __DIR__ . '/../storage/app/translations_missing_admin_converted_filled.csv';
|
||||
|
||||
if (!file_exists($input)) {
|
||||
fwrite(STDERR, "Input CSV not found: $input\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
$in = fopen($input, 'r');
|
||||
$out = fopen($output, 'w');
|
||||
if (!$in || !$out) {
|
||||
fwrite(STDERR, "Failed to open input/output files.\n");
|
||||
exit(3);
|
||||
}
|
||||
|
||||
// Read header
|
||||
$headerLine = fgets($in);
|
||||
if ($headerLine === false) {
|
||||
fwrite(STDERR, "Empty CSV file.\n");
|
||||
exit(4);
|
||||
}
|
||||
// Normalize header and write it back
|
||||
fwrite($out, rtrim($headerLine, "\r\n") . PHP_EOL);
|
||||
|
||||
$langCache = [];
|
||||
$rowCount = 0;
|
||||
// Try to bootstrap Laravel to access DB translations if available
|
||||
$appBooted = false;
|
||||
try {
|
||||
if (file_exists(__DIR__ . '/../vendor/autoload.php') && file_exists(__DIR__ . '/../bootstrap/app.php')) {
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
$app = require_once __DIR__ . '/../bootstrap/app.php';
|
||||
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
|
||||
$kernel->bootstrap();
|
||||
$appBooted = true;
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
// Ignore bootstrap failures and continue with file-based filling
|
||||
$appBooted = false;
|
||||
}
|
||||
while (($line = fgets($in)) !== false) {
|
||||
// Parse semicolon-delimited CSV with double-quote enclosure
|
||||
$cols = str_getcsv(trim($line, "\n\r"), ';', '"');
|
||||
// Support both quoted and unquoted CSVs
|
||||
if (count($cols) < 4) {
|
||||
// pad if necessary
|
||||
$cols = array_pad($cols, 4, '');
|
||||
}
|
||||
$filename = trim($cols[0], ' "');
|
||||
$language = trim($cols[1], ' "');
|
||||
$keycode = trim($cols[2], ' "');
|
||||
$value = isset($cols[3]) ? $cols[3] : '';
|
||||
|
||||
// Only fill if empty
|
||||
if ($value === null || $value === '') {
|
||||
$langKey = $language . '::' . $filename;
|
||||
if (!isset($langCache[$langKey])) {
|
||||
$path = __DIR__ . "/../resources/lang/{$language}/{$filename}.php";
|
||||
if (file_exists($path)) {
|
||||
$data = include $path;
|
||||
if (!is_array($data)) $data = [];
|
||||
$langCache[$langKey] = $data;
|
||||
} else {
|
||||
$langCache[$langKey] = null; // mark missing
|
||||
}
|
||||
}
|
||||
$translations = $langCache[$langKey];
|
||||
if (is_array($translations) && array_key_exists($keycode, $translations)) {
|
||||
$value = $translations[$keycode];
|
||||
} elseif ($appBooted) {
|
||||
// Try DB translations using the Translation model if available
|
||||
try {
|
||||
$modelClass = '\\Klevze\\ControlPanel\\Models\\Content\\Translation';
|
||||
if (class_exists($modelClass)) {
|
||||
$rec = $modelClass::where('file', $filename)->where('keycode', $keycode)->first();
|
||||
if ($rec && isset($rec->value) && is_array($rec->value) && array_key_exists($language, $rec->value)) {
|
||||
$value = $rec->value[$language];
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
// ignore DB issues
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure value is string
|
||||
if (is_array($value)) {
|
||||
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
// Write row using semicolon delimiter and double quotes
|
||||
// fputcsv will escape quotes correctly
|
||||
fputcsv($out, [$filename, $language, $keycode, $value], ';', '"');
|
||||
$rowCount++;
|
||||
}
|
||||
|
||||
fclose($in);
|
||||
fclose($out);
|
||||
|
||||
fwrite(STDOUT, "Filled {$rowCount} rows. Output: {$output}\n");
|
||||
exit(0);
|
||||
120
scripts/populate_sl_translations.php
Normal file
120
scripts/populate_sl_translations.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
// Usage: php scripts/populate_sl_translations.php [in-file] [out-file]
|
||||
// Default input: storage/app/translations_missing_admin.csv
|
||||
// This script will call LibreTranslate (https://libretranslate.com) to translate
|
||||
// an English guess of each key into Slovenian and update the CSV's suggested_sl column.
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
$in = $argv[1] ?? $root . '/storage/app/translations_missing_admin.csv';
|
||||
$out = $argv[2] ?? $in; // overwrite by default
|
||||
|
||||
if (!file_exists($in)) {
|
||||
echo "Input CSV not found: $in\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$backup = $in . '.bak.' . date('YmdHis');
|
||||
copy($in, $backup);
|
||||
echo "Backup written to: $backup\n";
|
||||
|
||||
$rows = [];
|
||||
$fh = fopen($in, 'r');
|
||||
$header = fgetcsv($fh);
|
||||
if ($header === false) {
|
||||
echo "Empty CSV\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while (($data = fgetcsv($fh)) !== false) {
|
||||
$rows[] = $data;
|
||||
}
|
||||
fclose($fh);
|
||||
|
||||
$libreUrl = 'https://libretranslate.com/translate';
|
||||
|
||||
function translateText($text, $url)
|
||||
{
|
||||
$payload = json_encode([
|
||||
'q' => $text,
|
||||
'source' => 'en',
|
||||
'target' => 'sl',
|
||||
'format' => 'text'
|
||||
]);
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||
|
||||
$resp = curl_exec($ch);
|
||||
$err = curl_error($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($err) {
|
||||
throw new Exception('Curl error: ' . $err);
|
||||
}
|
||||
if ($code < 200 || $code >= 300) {
|
||||
throw new Exception('HTTP ' . $code . ' response: ' . $resp);
|
||||
}
|
||||
|
||||
$json = json_decode($resp, true);
|
||||
return $json['translatedText'] ?? null;
|
||||
}
|
||||
|
||||
echo "Processing " . count($rows) . " rows...\n";
|
||||
|
||||
$i = 0;
|
||||
foreach ($rows as &$r) {
|
||||
$i++;
|
||||
// CSV columns: file,keycode,suggested_sl,suggested_en,placeholder
|
||||
$file = $r[0] ?? '';
|
||||
$keycode = $r[1] ?? '';
|
||||
$s_sl = $r[2] ?? '';
|
||||
$s_en = $r[3] ?? '';
|
||||
|
||||
if (trim($s_sl) !== '') {
|
||||
// already populated
|
||||
continue;
|
||||
}
|
||||
|
||||
// Build an English humanized guess from keycode if suggested_en is empty
|
||||
if (trim($s_en) !== '') {
|
||||
$source = $s_en;
|
||||
} else {
|
||||
$source = strtolower(str_replace('_', ' ', $keycode));
|
||||
// Make it sentence-like: capitalize first letter and replace multiple spaces
|
||||
$source = preg_replace('/\s+/', ' ', $source);
|
||||
$source = ucfirst($source);
|
||||
}
|
||||
|
||||
try {
|
||||
$translated = translateText($source, $libreUrl);
|
||||
if ($translated === null) {
|
||||
$translated = '';
|
||||
}
|
||||
$r[2] = $translated;
|
||||
echo "[{$i}] OK: {$keycode} -> {$translated}\n";
|
||||
} catch (\Throwable $e) {
|
||||
echo "[{$i}] WARN: {$keycode} -> " . $e->getMessage() . "\n";
|
||||
// leave blank on error
|
||||
}
|
||||
|
||||
// Rate limit: sleep 0.5s
|
||||
usleep(500000);
|
||||
}
|
||||
unset($r);
|
||||
|
||||
// Write out CSV
|
||||
$ofh = fopen($out, 'w');
|
||||
fputcsv($ofh, $header);
|
||||
foreach ($rows as $row) {
|
||||
fputcsv($ofh, $row);
|
||||
}
|
||||
fclose($ofh);
|
||||
|
||||
echo "Wrote updated CSV to: $out\n";
|
||||
|
||||
exit(0);
|
||||
Reference in New Issue
Block a user