Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 45 additions & 65 deletions ProcessMaker/ProcessTranslations/ProcessTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,80 +522,60 @@ public function getImportData($payload)

public function importTranslations($payload)
{
$screens = [];
foreach ($payload as $screenUuid => $value) {
$screen = Screen::where('uuid', $screenUuid)->first();
$newScreenTranslations = [];
if ($screen) {
$screenTranslations = $screen->translations;
$availableStrings = $this->getStringsInScreen($screen);

foreach ($value as $languageCode => $translations) {
if (!array_key_exists($languageCode, $screenTranslations)) {
$screenTranslations[$languageCode]['strings'] = [];
}

$newTranslations = [];
// For each of the available elements in the screens
foreach ($availableStrings as $availableString) {
// We need to check if there are current translations in the translation
// column (old translations) for that available string
$foundOld = false;
foreach ($screenTranslations[$languageCode]['strings'] as $inDbTranslation) {
if ($availableString === $inDbTranslation['key']) {
$foundOld = true;
$oldTranslation = [
'key' => $inDbTranslation['key'],
'string' => $inDbTranslation['string'],
];
}
}

// We need to check if there are some translation in the translation file
// for that available string
$foundInFile = false;
foreach ($translations as $importedTranslation) {
if ($availableString === $importedTranslation['key']) {
$foundInFile = true;
$inFileTranslation = [
'key' => $importedTranslation['key'],
'string' => $importedTranslation['string'],
];
}
}

if ($foundOld && !$foundInFile) {
$newTranslations[] = $oldTranslation;
}
$screen->translations = $this->generateNewTranslations($value, $screen);
$screen->save();
}
}
}

if (!$foundOld && $foundInFile) {
$newTranslations[] = $inFileTranslation;
}
protected function generateNewTranslations($value, $screen)
{
$newScreenTranslations = $screen->translations;
$availableStrings = $this->getStringsInScreen($screen);
foreach ($value as $languageCode => $translations) {
$newScreenTranslations[$languageCode]['strings'] = $this->generateNewLanguageTranslations(
$translations,
$availableStrings,
$newScreenTranslations[$languageCode]['strings'] ?? []
);
$createdAt = $newScreenTranslations[$languageCode]['created_at'] ?? Carbon::now();
$newScreenTranslations[$languageCode]['updated_at'] = Carbon::now();
$newScreenTranslations[$languageCode]['created_at'] = $createdAt;
}

if ($foundOld && $foundInFile && $inFileTranslation['string'] === '') {
$newTranslations[] = $inFileTranslation;
}
return $newScreenTranslations;
}

if ($foundOld && $foundInFile && $inFileTranslation['string'] === null) {
$newTranslations[] = $oldTranslation;
}
protected function generateNewLanguageTranslations($translations, $availableStrings, $existingTranslations)
{
$newTranslations = [];
foreach ($availableStrings as $availableString) {
$oldTranslation = $this->getTranslation($availableString, $existingTranslations);
$inFileTranslation = $this->getTranslation($availableString, $translations);
if (($oldTranslation !== null && $inFileTranslation === null)
|| ($oldTranslation !== null && $inFileTranslation !== null && $inFileTranslation['string'] === null)) {
$newTranslations[] = $oldTranslation;
}
if ($inFileTranslation !== null
&& ($oldTranslation === null || $oldTranslation !== null && $inFileTranslation !== null)) {
$newTranslations[] = $inFileTranslation;
}
}

if ($foundOld && $foundInFile && $inFileTranslation['string'] && $inFileTranslation['string'] !== '') {
$newTranslations[] = $inFileTranslation;
}
}
return $newTranslations;
}

// Assign new translations to language in screen
// $newScreenTranslations[$languageCode]['strings'] = $newTranslations;
foreach ($screenTranslations as $language => $translations) {
if ($language === $languageCode) {
$screenTranslations[$languageCode]['strings'] = $newTranslations;
}
}
}
$screen->translations = $screenTranslations;
$screen->save();
protected function getTranslation($key, $translationArray)
{
foreach ($translationArray as $translation) {
if ($key === $translation['key']) {
return $translation;
}
}

return null;
}
}