refactor: remove temporary translation script translate_site.php
This commit is contained in:
@@ -1,238 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
define('LARAVEL_START', microtime(true));
|
|
||||||
|
|
||||||
require __DIR__.'/vendor/autoload.php';
|
|
||||||
|
|
||||||
$app = require_once __DIR__.'/bootstrap/app.php';
|
|
||||||
|
|
||||||
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
|
|
||||||
$kernel->bootstrap();
|
|
||||||
|
|
||||||
use App\Models\Language;
|
|
||||||
use App\Models\SiteTranslation;
|
|
||||||
use App\Models\Translation;
|
|
||||||
|
|
||||||
echo "=== STARTING QATAR (ARABIC) LOCALIZATION TRANSLATOR ===\n";
|
|
||||||
|
|
||||||
// Helper function to translate text using Google Translate
|
|
||||||
function translate_text_to_ar($text) {
|
|
||||||
if (empty($text) || is_numeric($text)) {
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it's already Arabic, don't translate
|
|
||||||
if (preg_match('/[\x{0600}-\x{06FF}]/u', $text)) {
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
|
|
||||||
$url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=ar&dt=t&q=" . urlencode($text);
|
|
||||||
|
|
||||||
$options = [
|
|
||||||
"http" => [
|
|
||||||
"header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36\r\n"
|
|
||||||
]
|
|
||||||
];
|
|
||||||
$context = stream_context_create($options);
|
|
||||||
|
|
||||||
for ($attempt = 1; $attempt <= 3; $attempt++) {
|
|
||||||
$response = @file_get_contents($url, false, $context);
|
|
||||||
if ($response !== false) {
|
|
||||||
$json = json_decode($response, true);
|
|
||||||
if (isset($json[0])) {
|
|
||||||
$translated = "";
|
|
||||||
foreach ($json[0] as $part) {
|
|
||||||
if (isset($part[0])) {
|
|
||||||
$translated .= $part[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!empty($translated)) {
|
|
||||||
return $translated;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
usleep(300000); // 300ms delay between retries
|
|
||||||
}
|
|
||||||
|
|
||||||
return $text; // Fallback
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1. STEP: Activate Arabic (ar) language in Database with Qatar Flag
|
|
||||||
echo "\n--- Step 1: Configuring Arabic Language in Database ---\n";
|
|
||||||
$arabic = Language::where('code', 'ar')->first();
|
|
||||||
if ($arabic) {
|
|
||||||
$arabic->update([
|
|
||||||
'is_active' => true,
|
|
||||||
'flag' => '🇶🇦', // Qatar Flag
|
|
||||||
'direction' => 'rtl',
|
|
||||||
]);
|
|
||||||
echo "Arabic language ('ar') activated and configured with Qatar flag 🇶🇦 (RTL direction).\n";
|
|
||||||
} else {
|
|
||||||
Language::create([
|
|
||||||
'code' => 'ar',
|
|
||||||
'name' => 'Arabic',
|
|
||||||
'native_name' => 'العربية',
|
|
||||||
'flag' => '🇶🇦', // Qatar Flag
|
|
||||||
'direction' => 'rtl',
|
|
||||||
'is_active' => true,
|
|
||||||
'is_default' => false,
|
|
||||||
'sort_order' => 5,
|
|
||||||
]);
|
|
||||||
echo "Arabic language ('ar') created, activated and configured with Qatar flag 🇶🇦 (RTL direction).\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. STEP: Translate PHP Language Files
|
|
||||||
echo "\n--- Step 2: Translating PHP Language Files (TR to AR) ---\n";
|
|
||||||
$trPath = __DIR__ . '/lang/tr';
|
|
||||||
$arPath = __DIR__ . '/lang/ar';
|
|
||||||
|
|
||||||
if (!is_dir($arPath)) {
|
|
||||||
mkdir($arPath, 0755, true);
|
|
||||||
echo "Created directory: lang/ar\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
$files = glob($trPath . '/*.php');
|
|
||||||
foreach ($files as $file) {
|
|
||||||
$filename = basename($file);
|
|
||||||
echo "Processing PHP lang file: $filename\n";
|
|
||||||
|
|
||||||
$trArray = include $file;
|
|
||||||
$arFile = $arPath . '/' . $filename;
|
|
||||||
|
|
||||||
$arArray = [];
|
|
||||||
if (file_exists($arFile)) {
|
|
||||||
$arArray = include $arFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function for recursive array translation
|
|
||||||
if (!function_exists('translate_value_recursive')) {
|
|
||||||
function translate_value_recursive($val, $arVal = null, &$count = 0) {
|
|
||||||
if (is_array($val)) {
|
|
||||||
$res = [];
|
|
||||||
foreach ($val as $k => $v) {
|
|
||||||
$arSubVal = isset($arVal[$k]) ? $arVal[$k] : null;
|
|
||||||
$res[$k] = translate_value_recursive($v, $arSubVal, $count);
|
|
||||||
}
|
|
||||||
return $res;
|
|
||||||
} else {
|
|
||||||
if (!empty($arVal)) {
|
|
||||||
return $arVal;
|
|
||||||
}
|
|
||||||
$translated = translate_text_to_ar($val);
|
|
||||||
$count++;
|
|
||||||
usleep(50000); // 50ms delay
|
|
||||||
return $translated;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$translatedCount = 0;
|
|
||||||
$translatedArray = translate_value_recursive($trArray, $arArray, $translatedCount);
|
|
||||||
|
|
||||||
// Save back to lang/ar/filename.php
|
|
||||||
$content = "<?php\n\nreturn " . var_export($translatedArray, true) . ";\n";
|
|
||||||
$content = str_replace("array (", "[", $content);
|
|
||||||
$content = str_replace("),", "],", $content);
|
|
||||||
if (substr($content, -3) === ");\n") {
|
|
||||||
$content = substr($content, 0, -3) . "];\n";
|
|
||||||
}
|
|
||||||
file_put_contents($arFile, $content);
|
|
||||||
echo "Saved lang/ar/$filename. Translated $translatedCount new keys.\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. STEP: Translate Database SiteTranslations (site_translations table)
|
|
||||||
echo "\n--- Step 3: Translating Site Translations Table (330+ records) ---\n";
|
|
||||||
$siteTranslations = SiteTranslation::all();
|
|
||||||
echo "Found " . $siteTranslations->count() . " records in site_translations.\n";
|
|
||||||
$translatedCount = 0;
|
|
||||||
|
|
||||||
foreach ($siteTranslations as $st) {
|
|
||||||
$translations = $st->translations ?? [];
|
|
||||||
if (!isset($translations['ar']) || empty($translations['ar'])) {
|
|
||||||
// Find source text
|
|
||||||
$sourceText = $st->key;
|
|
||||||
if (isset($translations['tr']) && !empty($translations['tr'])) {
|
|
||||||
$sourceText = $translations['tr'];
|
|
||||||
} elseif (isset($translations['en']) && !empty($translations['en'])) {
|
|
||||||
$sourceText = $translations['en'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$translatedText = translate_text_to_ar($sourceText);
|
|
||||||
$translations['ar'] = $translatedText;
|
|
||||||
|
|
||||||
$st->translations = $translations;
|
|
||||||
$st->save();
|
|
||||||
$translatedCount++;
|
|
||||||
|
|
||||||
if ($translatedCount % 20 === 0) {
|
|
||||||
echo "Translated $translatedCount site translation records...\n";
|
|
||||||
}
|
|
||||||
usleep(60000); // 60ms delay
|
|
||||||
}
|
|
||||||
}
|
|
||||||
echo "Completed Step 3. Translated $translatedCount site translation keys to Arabic.\n";
|
|
||||||
|
|
||||||
// 4. STEP: Translate Database Model Translations (translations table)
|
|
||||||
echo "\n--- Step 4: Translating Model Translations Table (Pages, Products, Blogs, Categories) ---\n";
|
|
||||||
// Get all unique model + field combinations that have translations
|
|
||||||
$uniqueTranslations = Translation::select('translatable_type', 'translatable_id', 'field_name')
|
|
||||||
->groupBy('translatable_type', 'translatable_id', 'field_name')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
echo "Found " . $uniqueTranslations->count() . " unique translatable model fields.\n";
|
|
||||||
$modelTranslationsCount = 0;
|
|
||||||
|
|
||||||
foreach ($uniqueTranslations as $ut) {
|
|
||||||
// Check if Arabic translation already exists
|
|
||||||
$arTransExists = Translation::where('translatable_type', $ut->translatable_type)
|
|
||||||
->where('translatable_id', $ut->ut_id ?? $ut->translatable_id)
|
|
||||||
->where('field_name', $ut->field_name)
|
|
||||||
->where('language_code', 'ar')
|
|
||||||
->exists();
|
|
||||||
|
|
||||||
if ($arTransExists) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the best source translation
|
|
||||||
$sourceTrans = Translation::where('translatable_type', $ut->translatable_type)
|
|
||||||
->where('translatable_id', $ut->translatable_id)
|
|
||||||
->where('field_name', $ut->field_name)
|
|
||||||
->whereIn('language_code', ['tr', 'en', 'de', 'se'])
|
|
||||||
->orderByRaw("FIELD(language_code, 'tr', 'en', 'de', 'se') ASC")
|
|
||||||
->first();
|
|
||||||
|
|
||||||
if (!$sourceTrans || empty($sourceTrans->field_value)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$sourceText = $sourceTrans->field_value;
|
|
||||||
$translatedText = translate_text_to_ar($sourceText);
|
|
||||||
|
|
||||||
// Create new Arabic translation record
|
|
||||||
Translation::create([
|
|
||||||
'translatable_type' => $ut->translatable_type,
|
|
||||||
'translatable_id' => $ut->translatable_id,
|
|
||||||
'language_code' => 'ar',
|
|
||||||
'field_name' => $ut->field_name,
|
|
||||||
'field_value' => $translatedText,
|
|
||||||
'status' => 'published',
|
|
||||||
'version' => $sourceTrans->version ?? 1,
|
|
||||||
'created_by' => $sourceTrans->created_by,
|
|
||||||
'updated_by' => $sourceTrans->updated_by,
|
|
||||||
'approved_by' => $sourceTrans->approved_by,
|
|
||||||
'approved_at' => now(),
|
|
||||||
'published_at' => now(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
$modelTranslationsCount++;
|
|
||||||
|
|
||||||
if ($modelTranslationsCount % 20 === 0) {
|
|
||||||
echo "Translated $modelTranslationsCount model fields...\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
usleep(80000); // 80ms politeness delay
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Completed Step 4. Generated $modelTranslationsCount new Arabic model translation records.\n";
|
|
||||||
echo "\n=== ALL TRANSLATIONS COMPLETED SUCCESSFULLY ===\n";
|
|
||||||
Reference in New Issue
Block a user