Skip to content

Commit

Permalink
Merge pull request #4 from omatech/translations
Browse files Browse the repository at this point in the history
add generate translations functionality
  • Loading branch information
omacesc authored Dec 4, 2024
2 parents 885dd3c + 7d1438b commit adbd490
Show file tree
Hide file tree
Showing 9 changed files with 503 additions and 58 deletions.
248 changes: 248 additions & 0 deletions src/Admin/Accions/AdminGenerateTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
<?php

namespace Omatech\Editora\Admin\Accions;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Event;
use Omatech\Editora\Admin\Models\attributes;
use Omatech\Editora\Admin\Models\Instances;
use Omatech\Editora\Admin\Models\statictext;
use Illuminate\Support\Facades\Session;
use DeepL\Translator;
use Omatech\Editora\Admin\Events\EditInstance2UpdatedEvent;

class AdminGenerateTranslations extends AuthController
{
public function __construct()
{
parent::__construct();

$this->instances = new Instances;
$this->attributes = new attributes();
$this->statictext = new statictext();
}

public function render()
{
$params = get_params_info();
$params['p_mode'] = $p_mode = 'V';
$page = 1;
$langs = $this->instances->busca_idiomes();
$last_translated = [];

$staticTexts = $this->statictext->get_static_text_keys();
$staticTextsData = $this->statictext->get_static_texts_by_keys_and_langs($staticTexts, $langs);
$this->translateStaticTexts($staticTextsData);

$instances = $this->instances->getAllInstances();
$translatedInstances = $this->translateInstances($instances, $langs);

if(!empty($translatedInstances)) {
$this->dispatchEvent($translatedInstances);

$last_translated = array_map(function ($instance) {
return $this->instances->getInstInfo($instance);
}, $translatedInstances);
}

if ($params['param3'] != "") {
$page = $params['param3'];
}
if ($params['param1'] != "") {
$params['param1'] = "";
}

$count = count($last_translated);
$menu = $this->loadMenu($this->instances, $params);
$title = EDITORA_NAME;

$viewData = array_merge($menu, [
'title' => $title,
'p_mode' => $p_mode,
'last_translated' => $last_translated,
'count' => $count,
'page' => $page,
]);

return response()->view('editora::pages.translated_instances', $viewData);
}

private function translateInstances($instances, $langs)
{
$excludedClassIds = explode(',', env('EXCLUDED_CLASS_IDS'));
$translatedInstances = [];

foreach ($instances as $instance) {
if (!in_array($instance['class_id'], $excludedClassIds) && $instance['status'] == 'O') {
$params['param1'] = $instance['class_id'];
$params['param2'] = $instance['id'];
$attributes = $this->attributes->getInstanceAttributes('U', $params);
$textAttributes = $this->filterTextAttributes($attributes['instance_info']['instance_tabs']);
$missingAttrLangs = $this->getMissingAttrLangs($textAttributes);
$translated = $this->processMissingTranslations($textAttributes, $missingAttrLangs, $langs);
if ($translated) {
$translatedInstances[] = $this->attributes->insertAttributeValues($translated, $instance['id']);
}
}
}
return $translatedInstances;
}

private function translateStaticTexts($staticTextsData)
{
$translatedTexts = [];
foreach ($staticTextsData as $key => $values) {
$text = '';
$targetLangs = [];
$sourceLang = [];
foreach ($values as $value) {
if(!empty($value['text_value'])){
if(empty($text) && $value['language'] != 'ca') {
$sourceLang[] = $value['language'];
$text = $value['text_value'];
}
} else {
$targetLangs[] = $value['language'];
}
}

if($text && $targetLangs && $sourceLang) {
foreach($targetLangs as $targetLang) {
$translatedTexts[$key][$targetLang] = $this->translateText($text, $targetLang);
}
}

}
$this->statictext->insert_static_texts($translatedTexts);
}

private function processMissingTranslations($textAttributes, $missingAttrLangs, $langs)
{
$translated = [];

foreach ($missingAttrLangs as $attrName => $attrInfo) {
if (count($attrInfo) !== 7) {
$existingLangs = array_diff($langs, array_column($attrInfo, 'lang'));
$langToTranslateFrom = in_array('en', $existingLangs) ? 'en' :
(in_array('us', $existingLangs) ? 'us' :
(in_array('es', $existingLangs) ? 'es' :
(in_array('fr', $existingLangs) ? 'fr' :
(in_array('de', $existingLangs) ? 'de' :
(in_array('jp', $existingLangs) ? 'jp' : null)))));

if ($langToTranslateFrom) {
$textToTranslateFrom = $this->getTextValForLang($textAttributes, $langToTranslateFrom, $attrName);
foreach ($attrInfo as $info) {
$translated[$info['id']] = [
'lang' => $info['lang'],
'text' => $this->translateText($textToTranslateFrom['text_val'], $info['lang'])
];
}
}
}
}

return $translated;
}

private function translateText($text, $targetLang)
{
try {
if ($targetLang === 'ca') {
return null;
}
if ($targetLang === 'us') {
$targetLang = 'EN-US';
}
if ($targetLang === 'en') {
$targetLang = 'EN-GB';
}
if ($targetLang === 'jp') {
$targetLang = 'JA';
}

$translator = new Translator(config('editora-admin.deepl_key'));
$result = $translator->translateText($text, null, $targetLang);
return html_entity_decode($result->text);

} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
return null;
}
}

private function getTextValForLang($textAttributes, $langToTranslateFrom, $attrName)
{
$attrVal = [];

foreach ($textAttributes as $textAttr) {
if ($textAttr['name'] === $langToTranslateFrom) {
$attrVal = array_map(function ($a) use ($attrName) {
if ($a['tag'] === $attrName) {
return $a['atrib_values'][0]['text_val'];
}
}, $textAttr['elsatribs']);

$attrVal = array_filter($attrVal, function ($value) {
return !is_null($value);
});
break;
}
}

return ['lang' => $langToTranslateFrom, 'text_val' => array_values($attrVal)[0]];
}

private function filterTextAttributes($attributes)
{
$tabs_lang = array_filter($attributes, function ($value) {
return $value['name'] !== 'data';
});

foreach ($tabs_lang as &$data) {
$data['elsatribs'] = $this->filterEachLangAttributes($data['elsatribs']);
}

return $tabs_lang;
}

private function filterEachLangAttributes($elsatribs)
{
return array_filter($elsatribs, function ($value) {
return in_array($value['type'], ['S', 'K']);
});
}

private function getMissingAttrLangs(array $tabs_lang)
{
$missingLangs = [];

foreach ($tabs_lang as &$data) {
$data['elsatribs'] = $this->filterEachLangAttributes($data['elsatribs']);

foreach ($data['elsatribs'] as $elsatrib) {
if (empty($elsatrib['atrib_values']) ||
(!empty($elsatrib['atrib_values']) && $elsatrib['atrib_values'][0]['text_val'] === '<p>&nbsp;</p>')) {
$missingLangs[$elsatrib['tag']][] = [
'id' => $elsatrib['id'],
'lang' => $elsatrib['language']
];
}
}
}
return $missingLangs;
}

private function dispatchEvent(array $instanceIds): void
{
try {
foreach ($instanceIds as $instanceId) {
if ($instanceId !== 0) {
Event::dispatch(new EditInstance2UpdatedEvent($instanceId));
}
}
} catch (\Exception $exception) {
}
}
}

6 changes: 6 additions & 0 deletions src/Admin/Models/Instances.php
Original file line number Diff line number Diff line change
Expand Up @@ -1423,4 +1423,10 @@ function isUrlBroken($url = NULL)
return true;
}
}

public function getAllInstances()
{
$sql = "SELECT * FROM omp_instances";
return parent::get_data($sql);
}
}
19 changes: 19 additions & 0 deletions src/Admin/Models/attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Omatech\Editora\Admin\Models;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class attributes extends Model
Expand Down Expand Up @@ -397,4 +398,22 @@ private function niceurlFromAtri($atri_id, $inst_id)

return $ret['niceurl'];
}

public function insertAttributeValues($values, $instanceId)
{
DB::beginTransaction();
try {
foreach ($values as $atriId => $value) {
DB::table('omp_values')->updateOrInsert(
['inst_id' => $instanceId, 'atri_id' => $atriId,],
['text_val' => $value['text']]
);
}
DB::commit();
return $instanceId;
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
}
42 changes: 42 additions & 0 deletions src/Admin/Models/statictext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Omatech\Editora\Admin\Models;

use Illuminate\Support\Facades\DB;

class statictext extends Model
{
public function get_static_text_keys()
Expand Down Expand Up @@ -73,4 +75,44 @@ public function set_static_text($values, $key)
}
return true;
}

public function get_static_texts_by_keys_and_langs($keys)
{
$structuredResult = [];
$keysStr = implode(",", array_map('intval', $keys));

$sql = "SELECT text_key, language, text_value FROM omp_static_text WHERE text_key IN ($keysStr) AND language not in ('ALL')";
$result = parent::get_data($sql);

foreach ($result as $row) {
if (!isset($structuredResults[$row['text_key']])) {
$structuredResults[$row['text_key']] = [];
}

$structuredResults[$row['text_key']][] = [
'language' => $row['language'],
'text_value' => $row['text_value'],
];
}

return $structuredResults;
}

public function insert_static_texts($static_texts)
{
DB::beginTransaction();
try {
foreach ($static_texts as $key => $translations) {
foreach ($translations as $language => $textValue) {
DB::table('omp_static_text')
->where(['text_key' => $key, 'language' => $language])
->update(['text_value' => $textValue]);
}
}
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
}
2 changes: 2 additions & 0 deletions src/Admin/langs/ca/messages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ function getMessage ($id) {
$mess_arr['classes_sample_modal'] = 'Exemple:';
$mess_arr['classes_modal_not_image'] = 'Sense previsualització';
$mess_arr['urls_broken'] = 'Enllaços trencats';
$mess_arr['info_word_last_translations'] = 'Últims objectes traduïts';
$mess_arr['generate_translations'] = 'Generar traduccions';

return $mess_arr[$id];
}
Expand Down
2 changes: 2 additions & 0 deletions src/Admin/langs/en/messages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ function getMessage ($id) {
$mess_arr['classes_sample_modal'] = 'Sample:';
$mess_arr['classes_modal_not_image'] = 'No preview';
$mess_arr['urls_broken'] = 'Broken links';
$mess_arr['info_word_last_translations'] = 'Last translated objects';
$mess_arr['generate_translations'] = 'Generate translations';

return $mess_arr[$id];
}
Expand Down
2 changes: 2 additions & 0 deletions src/Admin/langs/es/messages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ function getMessage ($id) {
$mess_arr['classes_sample_modal'] = 'Ejemplo:';
$mess_arr['classes_modal_not_image'] = 'Sin previsualización';
$mess_arr['urls_broken'] = 'Enlaces rotos';
$mess_arr['info_word_last_translations'] = 'Últimos objetos traducidos';
$mess_arr['generate_translations'] = 'Generar traducciones';

return $mess_arr[$id];
}
Expand Down
Loading

0 comments on commit adbd490

Please sign in to comment.