229 lines
9.9 KiB
PHP
229 lines
9.9 KiB
PHP
<?php
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||
use Illuminate\Support\Facades\Cache;
|
||
|
||
function converterMapReplacer(?Spreadsheet $spreadsheet, $type="replacer") {
|
||
// Converter-map verilerini al
|
||
$converterMap = j(setting("converter-map"));
|
||
|
||
// Mapping arrays oluştur ve cache'le
|
||
$cacheKey = "converter_map_data";
|
||
$mappings = Cache::remember($cacheKey, 3600, function() use ($converterMap) {
|
||
$termToRu = $termToEn = $enToRu = $ruToEn = $enToTerm = $ruToTerm = [];
|
||
|
||
if (is_array($converterMap)) {
|
||
foreach ($converterMap as $row) {
|
||
if (!empty($row['term']) && !empty($row['term_ru'])) {
|
||
$termToRu[$row['term']] = $row['term_ru'];
|
||
}
|
||
if (!empty($row['term']) && !empty($row['term_eng'])) {
|
||
$termToEn[$row['term']] = $row['term_eng'];
|
||
}
|
||
if (!empty($row['term_eng']) && !empty($row['term_ru'])) {
|
||
$enToRu[$row['term_eng']] = $row['term_ru'];
|
||
}
|
||
if (!empty($row['term_ru']) && !empty($row['term_eng'])) {
|
||
$ruToEn[$row['term_ru']] = $row['term_eng'];
|
||
}
|
||
if (!empty($row['term_eng']) && !empty($row['term'])) {
|
||
$enToTerm[$row['term_eng']] = $row['term'];
|
||
}
|
||
if (!empty($row['term_ru']) && !empty($row['term'])) {
|
||
$ruToTerm[$row['term_ru']] = $row['term'];
|
||
}
|
||
}
|
||
}
|
||
|
||
return [
|
||
'termToRu' => $termToRu,
|
||
'termToEn' => $termToEn,
|
||
'enToRu' => $enToRu,
|
||
'ruToEn' => $ruToEn,
|
||
'enToTerm' => $enToTerm,
|
||
'ruToTerm' => $ruToTerm
|
||
];
|
||
});
|
||
|
||
if ($type == "replacer") {
|
||
// Sadece spreadsheet null değilse işle
|
||
if ($spreadsheet !== null) {
|
||
try {
|
||
// Spreadsheet'teki her sheet'i dolaş
|
||
foreach ($spreadsheet->getAllSheets() as $sheet) {
|
||
// Her satır ve hücreyi dolaş
|
||
foreach ($sheet->getRowIterator() as $row) {
|
||
foreach ($row->getCellIterator() as $cell) {
|
||
$cellValue = $cell->getValue();
|
||
if (is_string($cellValue)) {
|
||
$originalValue = $cellValue;
|
||
|
||
// _en pattern'lerini bul ve değiştir (TERM_en -> İngilizce karşılığı)
|
||
$cellValue = preg_replace_callback('/(\w+)_en\b/', function($matches) use ($mappings) {
|
||
$term = $matches[1];
|
||
if (isset($mappings['termToEn'][$term])) {
|
||
return $mappings['termToEn'][$term];
|
||
}
|
||
return $matches[0]; // Bulunamazsa orijinal değeri döndür
|
||
}, $cellValue);
|
||
|
||
// _ru pattern'lerini bul ve değiştir (TERM_ru -> Rusça karşılığı)
|
||
$cellValue = preg_replace_callback('/(\w+)_ru\b/', function($matches) use ($mappings) {
|
||
$term = $matches[1];
|
||
if (isset($mappings['termToRu'][$term])) {
|
||
return $mappings['termToRu'][$term];
|
||
}
|
||
return $matches[0]; // Bulunamazsa orijinal değeri döndür
|
||
}, $cellValue);
|
||
|
||
// _tr pattern'lerini bul ve değiştir (TERM_tr -> Türkçe karşılığı)
|
||
$cellValue = preg_replace_callback('/(\w+)_tr\b/', function($matches) use ($mappings) {
|
||
$term = $matches[1];
|
||
// Türkçe için term alanını kullan (orijinal)
|
||
return $term; // Terim kendisi zaten Türkçe
|
||
}, $cellValue);
|
||
|
||
// Placeholder formatında da çeviri yap: {TERM_en}, {TERM_ru}
|
||
$cellValue = preg_replace_callback('/\{(\w+)_en\}/', function($matches) use ($mappings) {
|
||
$term = $matches[1];
|
||
if (isset($mappings['termToEn'][$term])) {
|
||
return $mappings['termToEn'][$term];
|
||
}
|
||
return $matches[0]; // Bulunamazsa orijinal değeri döndür
|
||
}, $cellValue);
|
||
|
||
$cellValue = preg_replace_callback('/\{(\w+)_ru\}/', function($matches) use ($mappings) {
|
||
$term = $matches[1];
|
||
if (isset($mappings['termToRu'][$term])) {
|
||
return $mappings['termToRu'][$term];
|
||
}
|
||
return $matches[0]; // Bulunamazsa orijinal değeri döndür
|
||
}, $cellValue);
|
||
|
||
$cellValue = preg_replace_callback('/\{(\w+)_tr\}/', function($matches) use ($mappings) {
|
||
$term = $matches[1];
|
||
// Türkçe için term alanını kullan (orijinal)
|
||
return $term; // Terim kendisi zaten Türkçe
|
||
}, $cellValue);
|
||
|
||
// Eğer değişiklik olduysa hücreyi güncelle
|
||
if ($cellValue !== $originalValue) {
|
||
$cell->setValue($cellValue);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
// Değiştirilmiş spreadsheet'i döndür
|
||
return $spreadsheet;
|
||
} catch (\Throwable $th) {
|
||
// Hata durumunda log ama devam et
|
||
logProcessInfo('Error in converterMapReplacer', [
|
||
'error' => $th->getMessage(),
|
||
'trace' => $th->getTraceAsString()
|
||
], 'error');
|
||
}
|
||
}
|
||
|
||
// Spreadsheet null ise veya hata oluştuysa boş array döndür
|
||
return [];
|
||
} else {
|
||
// "data" tipi için mevcut terimleri placeholder listesi olarak döndür
|
||
$placeholders = [];
|
||
|
||
if (is_array($converterMap)) {
|
||
foreach ($converterMap as $row) {
|
||
if (!empty($row['term'])) {
|
||
$term = $row['term'];
|
||
// Farklı dil pattern'leri ekle
|
||
$placeholders[] = $term . "_en";
|
||
$placeholders[] = $term . "_ru";
|
||
$placeholders[] = $term . "_tr";
|
||
$placeholders[] = "{" . $term . "_en}";
|
||
$placeholders[] = "{" . $term . "_ru}";
|
||
$placeholders[] = "{" . $term . "_tr}";
|
||
}
|
||
}
|
||
}
|
||
|
||
return array_unique($placeholders);
|
||
}
|
||
}
|
||
|
||
// String replacement için yardımcı fonksiyon
|
||
function converterMapStringReplacer($string) {
|
||
// Cache kullanarak converter-map verilerini al (1 saat süreyle)
|
||
$mappings = Cache::remember('converter_map_string_data', 3600, function() {
|
||
$converterMap = j(setting("converter-map"));
|
||
$termToRu = $termToEn = [];
|
||
|
||
if (is_array($converterMap)) {
|
||
foreach ($converterMap as $row) {
|
||
if (!empty($row['term']) && !empty($row['term_ru'])) {
|
||
$termToRu[$row['term']] = $row['term_ru'];
|
||
}
|
||
if (!empty($row['term']) && !empty($row['term_eng'])) {
|
||
$termToEn[$row['term']] = $row['term_eng'];
|
||
}
|
||
}
|
||
}
|
||
|
||
return [
|
||
'termToRu' => $termToRu,
|
||
'termToEn' => $termToEn
|
||
];
|
||
});
|
||
|
||
$termToRu = $mappings['termToRu'];
|
||
$termToEn = $mappings['termToEn'];
|
||
|
||
|
||
// _en pattern'lerini değiştir
|
||
$string = preg_replace_callback('/(\w+)_en\b/', function($matches) use ($termToEn) {
|
||
$term = $matches[1];
|
||
if (isset($termToEn[$term])) {
|
||
return $termToEn[$term];
|
||
}
|
||
return $matches[0];
|
||
}, $string);
|
||
|
||
// _ru pattern'lerini değiştir
|
||
$string = preg_replace_callback('/(\w+)_ru\b/', function($matches) use ($termToRu) {
|
||
$term = $matches[1];
|
||
if (isset($termToRu[$term])) {
|
||
return $termToRu[$term];
|
||
}
|
||
return $matches[0];
|
||
}, $string);
|
||
|
||
// _tr pattern'lerini değiştir (Türkçe - orijinal terim)
|
||
$string = preg_replace_callback('/(\w+)_tr\b/', function($matches) {
|
||
$term = $matches[1];
|
||
return $term; // Terim kendisi zaten Türkçe
|
||
}, $string);
|
||
|
||
// Placeholder formatında da çeviri yap
|
||
$string = preg_replace_callback('/\{(\w+)_en\}/', function($matches) use ($termToEn) {
|
||
$term = $matches[1];
|
||
if (isset($termToEn[$term])) {
|
||
return $termToEn[$term];
|
||
}
|
||
return $matches[0];
|
||
}, $string);
|
||
|
||
$string = preg_replace_callback('/\{(\w+)_ru\}/', function($matches) use ($termToRu) {
|
||
$term = $matches[1];
|
||
if (isset($termToRu[$term])) {
|
||
return $termToRu[$term];
|
||
}
|
||
return $matches[0];
|
||
}, $string);
|
||
|
||
$string = preg_replace_callback('/\{(\w+)_tr\}/', function($matches) {
|
||
$term = $matches[1];
|
||
return $term; // Terim kendisi zaten Türkçe
|
||
}, $string);
|
||
|
||
return $string;
|
||
}
|
||
?>
|