181 lines
8.5 KiB
PHP
181 lines
8.5 KiB
PHP
<?php
|
||
require __DIR__.'/vendor/autoload.php';
|
||
$app = require_once __DIR__.'/bootstrap/app.php';
|
||
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
|
||
|
||
use App\Models\SiteTranslation;
|
||
use App\Models\Translation;
|
||
use App\Models\Language;
|
||
|
||
echo "=== STARTING TRANSLATION PROCESS ===\n";
|
||
|
||
// Helper translation function using Google Translate single API
|
||
function translateToEnglish($text) {
|
||
if (empty($text) || is_numeric($text)) {
|
||
return $text;
|
||
}
|
||
|
||
// If it's a HTML tags only or empty paragraph, return as is
|
||
if (trim($text) === '<p></p>' || trim($text) === '') {
|
||
return $text;
|
||
}
|
||
|
||
$url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=tr&tl=en&dt=t&q=" . urlencode($text);
|
||
|
||
$ch = curl_init();
|
||
curl_setopt($ch, CURLOPT_URL, $url);
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
|
||
$response = curl_exec($ch);
|
||
curl_close($ch);
|
||
|
||
if (!$response) {
|
||
return false;
|
||
}
|
||
|
||
$data = json_decode($response, true);
|
||
if (!isset($data[0])) {
|
||
return false;
|
||
}
|
||
|
||
$translated = "";
|
||
foreach ($data[0] as $sentence) {
|
||
$translated .= $sentence[0];
|
||
}
|
||
|
||
return $translated;
|
||
}
|
||
|
||
// Check for Turkish specific characters or common Turkish words
|
||
function isTurkishText($text) {
|
||
if (empty($text) || is_numeric($text)) return false;
|
||
return preg_match('/[ğüşıöçĞÜŞİÖÇ]/u', $text) || preg_match('/\b(ve|bir|bu|ne|de|da|icin|için|ile|en|ki|ise|mi|mu|sonra|olarak|gibi|daha|hakkinda|hakkımızda|giris|giriş|kayit|kayıt|bizim|hizmet|iletisim|iletişim|neden|neler|yapıyoruz|biz|seviyoruz|ekibimiz|abone|adınız|soyadınız|telefon|adres|teknoloji)\b/iu', $text);
|
||
}
|
||
|
||
// ----------------------------------------------------
|
||
// PHASE 1: Translate Dynamic Model Translations
|
||
// ----------------------------------------------------
|
||
echo "\n--- Phase 1: Translating Dynamic Model Translations ---\n";
|
||
$trTranslations = Translation::where('language_code', 'tr')->get();
|
||
$translatedModelsCount = 0;
|
||
|
||
foreach ($trTranslations as $t) {
|
||
$trVal = trim($t->field_value);
|
||
if ($trVal === '' || $trVal === '<p></p>' || $trVal === 'null') {
|
||
continue;
|
||
}
|
||
|
||
$enTranslation = Translation::where('translatable_type', $t->translatable_type)
|
||
->where('translatable_id', $t->translatable_id)
|
||
->where('field_name', $t->field_name)
|
||
->where('language_code', 'en')
|
||
->first();
|
||
|
||
$enVal = $enTranslation ? trim($enTranslation->field_value) : '';
|
||
|
||
$needsTranslation = false;
|
||
if (empty($enVal) || $enVal === '<p></p>') {
|
||
$needsTranslation = true;
|
||
} else if ($enVal === $trVal && isTurkishText($trVal)) {
|
||
$needsTranslation = true;
|
||
}
|
||
|
||
// Specific exclusions or manual translations
|
||
if ($needsTranslation) {
|
||
echo "Translating [{$t->translatable_type} ID: {$t->translatable_id}] Field '{$t->field_name}'...\n";
|
||
|
||
// Manual override for some specific fields to ensure maximum quality
|
||
if ($t->translatable_type === 'App\\Models\\Page' && $t->translatable_id == 4 && $t->field_name === 'content') {
|
||
$translatedVal = '<p><img src="https://truncgil.com.tr/r.php?w=256&p=file/Truncgil_Logo_2014.png" alt="Trunçgil Teknoloji"></p><hr><p></p><p><strong>Our Mission</strong><br>We work with all our might to create human-oriented, rational, simple, and aesthetic applications that make life easier.</p><p><strong>Our Vision</strong><br>The world\'s greatest country Turkey, and Turkey\'s greatest company Trunçgil!</p><p><strong>Our Values</strong></p><ul><li><p><strong>Integrity:</strong><br>"No legacy is so rich as honesty."<br>Shakespeare</p></li><li><p><strong>Entrepreneurship:</strong><br>"The real voyage of discovery consists not in seeking new landscapes, but in having new eyes."<br>M. Proust</p></li><li><p><strong>Responsibility:</strong><br>"Self-responsibility means keeping the promises we make to ourselves as well as the promises we make to others." <br>Andre Gide</p></li><li><p><strong>Quality: </strong><br>"If a man is called to be a street sweeper, he should sweep streets even as Michelangelo painted, or Beethoven composed music, or Shakespeare wrote poetry. He should sweep streets so well that all the hosts of heaven and earth will pause to say, \'Here lived a great street sweeper who did his job well.\'"<br>Martin Luther King</p></li></ul>';
|
||
} else if ($t->translatable_type === 'App\\Models\\Page' && $t->translatable_id == 19 && $t->field_name === 'content') {
|
||
// Translate the Turkish helper text inside the otherwise English policy
|
||
$translatedVal = str_replace('(Lütfen kendi e-postanızı girin)', '(Please enter your own email)', $t->field_value);
|
||
} else {
|
||
$translatedVal = translateToEnglish($t->field_value);
|
||
usleep(150000); // 150ms delay
|
||
}
|
||
|
||
if ($translatedVal !== false) {
|
||
Translation::updateOrCreate(
|
||
[
|
||
'translatable_type' => $t->translatable_type,
|
||
'translatable_id' => $t->translatable_id,
|
||
'field_name' => $t->field_name,
|
||
'language_code' => 'en',
|
||
],
|
||
[
|
||
'field_value' => $translatedVal,
|
||
'status' => 'published',
|
||
'version' => $t->version,
|
||
'created_by' => $t->created_by,
|
||
'updated_by' => $t->updated_by,
|
||
'approved_by' => $t->approved_by,
|
||
'approved_at' => $t->approved_at,
|
||
'published_at' => $t->published_at,
|
||
]
|
||
);
|
||
echo "Successfully translated to: " . substr(strip_tags($translatedVal), 0, 50) . "...\n";
|
||
$translatedModelsCount++;
|
||
} else {
|
||
echo "Failed to translate [{$t->translatable_type} ID: {$t->translatable_id}] Field '{$t->field_name}' due to API error.\n";
|
||
}
|
||
}
|
||
}
|
||
echo "Phase 1 Completed. Total models translated/updated: {$translatedModelsCount}\n";
|
||
|
||
// ----------------------------------------------------
|
||
// PHASE 2: Translate Site Translations Table
|
||
// ----------------------------------------------------
|
||
echo "\n--- Phase 2: Translating Site Translations ---\n";
|
||
$siteTranslations = SiteTranslation::all();
|
||
$translatedSiteCount = 0;
|
||
$apiCallsCount = 0;
|
||
|
||
foreach ($siteTranslations as $st) {
|
||
$translations = $st->translations ?? [];
|
||
$enVal = isset($translations['en']) ? trim($translations['en']) : '';
|
||
$trVal = isset($translations['tr']) ? trim($translations['tr']) : '';
|
||
$keyVal = trim($st->key);
|
||
|
||
$sourceVal = !empty($trVal) ? $trVal : $keyVal;
|
||
|
||
$needsTranslation = false;
|
||
if (empty($enVal)) {
|
||
$needsTranslation = true;
|
||
} else if ($enVal === $sourceVal && isTurkishText($sourceVal)) {
|
||
$needsTranslation = true;
|
||
}
|
||
|
||
if ($needsTranslation) {
|
||
// Check if the source text is already in English
|
||
if (!isTurkishText($sourceVal)) {
|
||
// It's already in English! Populate the 'en' translation directly without calling the API
|
||
$translations['en'] = $sourceVal;
|
||
$st->translations = $translations;
|
||
$st->save();
|
||
$translatedSiteCount++;
|
||
echo "Skipped API for ID {$st->id}: Source is already in English: '" . substr($sourceVal, 0, 30) . "...'\n";
|
||
} else {
|
||
// It's in Turkish! Query Google Translate
|
||
echo "Translating Site Translation ID {$st->id}...\n";
|
||
$translatedVal = translateToEnglish($sourceVal);
|
||
$apiCallsCount++;
|
||
usleep(150000); // 150ms delay
|
||
|
||
if ($translatedVal !== false) {
|
||
// Remove trailing spaces or unwanted corrections
|
||
$translations['en'] = trim($translatedVal);
|
||
$st->translations = $translations;
|
||
$st->save();
|
||
$translatedSiteCount++;
|
||
echo "Translated ID {$st->id} from [{$sourceVal}] to [{$translatedVal}]\n";
|
||
} else {
|
||
echo "Failed to translate Site Translation ID {$st->id} due to API error.\n";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
echo "Phase 2 Completed. Total site translations processed: {$translatedSiteCount} (API queries: {$apiCallsCount})\n";
|
||
echo "\n=== ALL TRANSLATIONS COMPLETED SUCCESSFULLY ===\n";
|