Add project files

This commit is contained in:
infosave2007
2025-11-07 13:34:06 +03:00
parent 3402b19f2c
commit a33af60f2d
41 changed files with 8128 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
#!/usr/bin/env php
<?php
/**
* CLI tool for auto-translating missing keys
* Usage: php translate.php <language_code>
* Example: php translate.php ru
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../inc/Config.php';
require_once __DIR__ . '/../inc/DB.php';
require_once __DIR__ . '/../inc/Translator.php';
Config::load(__DIR__ . '/../.env');
if ($argc < 2) {
echo "Usage: php translate.php <language_code>\n";
echo "Available languages: ru, es, de, fr, zh\n";
echo "\nExample:\n";
echo " php translate.php ru # Translate to Russian\n";
echo " php translate.php all # Translate all languages\n";
exit(1);
}
$targetLang = $argv[1];
// Initialize (without session)
session_start();
Translator::init();
if ($targetLang === 'all') {
$languages = ['ru', 'es', 'de', 'fr', 'zh'];
echo "Auto-translating all languages...\n\n";
foreach ($languages as $lang) {
echo "Translating to $lang...\n";
$stats = Translator::translateMissingKeys($lang);
echo " Total keys: {$stats['total']}\n";
echo " Translated: {$stats['translated']}\n";
echo " Failed: {$stats['failed']}\n";
echo " Progress: " . round(($stats['translated'] / $stats['total']) * 100, 2) . "%\n\n";
}
echo "✓ All translations completed!\n";
} else {
if (!Translator::isSupported($targetLang)) {
echo "Error: Language '$targetLang' is not supported\n";
echo "Available languages: en, ru, es, de, fr, zh\n";
exit(1);
}
if ($targetLang === 'en') {
echo "Error: English is the source language, no translation needed\n";
exit(1);
}
echo "Auto-translating to $targetLang...\n";
$stats = Translator::translateMissingKeys($targetLang);
echo "\nTranslation Statistics:\n";
echo " Total keys: {$stats['total']}\n";
echo " Translated: {$stats['translated']}\n";
echo " Failed: {$stats['failed']}\n";
echo " Progress: " . round(($stats['translated'] / $stats['total']) * 100, 2) . "%\n";
if ($stats['failed'] > 0) {
echo "\n⚠ Some translations failed. This might be due to API rate limits.\n";
echo " Try running the script again later.\n";
} else {
echo "\n✓ Translation completed successfully!\n";
}
}
+104
View File
@@ -0,0 +1,104 @@
#!/usr/bin/env php
<?php
/**
* Auto-translate all languages
* Usage: php bin/translate_all.php
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../inc/Config.php';
require_once __DIR__ . '/../inc/DB.php';
require_once __DIR__ . '/../inc/Translator.php';
Config::load(__DIR__ . '/../.env');
DB::conn();
echo "=== Auto-Translation Tool ===\n\n";
// Check if API key exists
$translator = new Translator();
$apiKey = $translator->getApiKey('openrouter');
if (empty($apiKey)) {
echo "❌ Error: OpenRouter API key not found in database.\n";
echo "Please add your API key in Settings page first.\n";
exit(1);
}
echo "✅ OpenRouter API key found\n\n";
// Get all languages except English
$pdo = DB::conn();
$stmt = $pdo->query("SELECT code, name FROM languages WHERE code != 'en' ORDER BY code");
$languages = $stmt->fetchAll();
echo "Languages to translate: " . count($languages) . "\n";
foreach ($languages as $lang) {
echo " - {$lang['name']} ({$lang['code']})\n";
}
echo "\nStarting translation...\n\n";
foreach ($languages as $lang) {
$langCode = $lang['code'];
$langName = $lang['name'];
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Translating to: {$langName} ({$langCode})\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
try {
$stats = Translator::translateMissingKeys($langCode);
echo "✅ Translation completed!\n";
echo " Total keys: {$stats['total']}\n";
echo " Translated: {$stats['translated']}\n";
echo " Already existed: {$stats['existing']}\n";
echo " Failed: {$stats['failed']}\n\n";
// Sleep to avoid rate limiting
if ($stats['translated'] > 0) {
echo "⏳ Waiting 5 seconds to avoid rate limits...\n\n";
sleep(5);
}
} catch (Exception $e) {
echo "❌ Error: {$e->getMessage()}\n\n";
}
}
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "✅ All translations completed!\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
// Show final statistics
echo "\nFinal Statistics:\n";
$stmt = $pdo->query("
SELECT
l.code,
l.name,
COUNT(DISTINCT t.translation_key) as translated,
(SELECT COUNT(DISTINCT translation_key) FROM translations WHERE language_code = 'en') as total
FROM languages l
LEFT JOIN translations t ON l.code = t.language_code
GROUP BY l.code, l.name
ORDER BY l.code
");
$results = $stmt->fetchAll();
foreach ($results as $row) {
$percent = round(($row['translated'] / $row['total']) * 100);
$bar = str_repeat('█', (int)($percent / 5));
$empty = str_repeat('░', 20 - (int)($percent / 5));
echo sprintf(
" %s (%s): [%s%s] %3d%% (%d/%d)\n",
$row['name'],
$row['code'],
$bar,
$empty,
$percent,
$row['translated'],
$row['total']
);
}
echo "\n";