-
-
Save jeroendesloovere/a67f46ccce84eac719ab7b405f71526f to your computer and use it in GitHub Desktop.
ForkCMS translation helper.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$document = new DOMDocument(); | |
$document->load($argv[1]); | |
$existingTranslations = []; | |
function getUserInput() { | |
return readline("\nTranslation PT: "); | |
} | |
/** @var \DOMElement $item */ | |
/** | |
* @param \DOMElement $item | |
* @param $document | |
*/ | |
function printCurrentItem($item, $document): void | |
{ | |
echo sprintf( | |
"Type: %s\n", | |
$item->attributes->getNamedItem('type')->textContent | |
); | |
echo sprintf( | |
"Name: %s\n", | |
$item->attributes->getNamedItem('name')->textContent | |
); | |
echo $document->saveXML($item); | |
} | |
foreach ($document->getElementsByTagName('item') as $item) { | |
$hasPtTranslation = false; | |
/** @var \DOMElement $translation */ | |
foreach ($item->getElementsByTagName('translation') as $translation) { | |
if ($translation->getAttribute('language') === 'pt') { | |
$hasPtTranslation = true; | |
$existingTranslations[ | |
$item->getAttribute('name') | |
] = $translation->textContent; | |
break; | |
} | |
} | |
if ($hasPtTranslation) { | |
continue; | |
} | |
printCurrentItem($item, $document); | |
if (isset($existingTranslations[$item->getAttribute('name')])) { | |
$translationValue = $existingTranslations[$item->getAttribute('name')]; | |
echo "\n\nRepeated value " . $item->getAttribute('name') . ". Using: $translationValue\n"; | |
} else { | |
$translationValue = getUserInput(); | |
} | |
if (empty($translationValue)) { | |
continue; | |
} | |
$cdata = $document->createCDATASection($translationValue); | |
$translation = $document->createElement('translation'); | |
$translation->setAttribute('language', 'pt'); | |
$translation->appendChild($cdata); | |
$item->appendChild($translation); | |
$response = readline("\nContinue? "); | |
if ($response === 'n' || $response === 'no') { | |
break; | |
} | |
} | |
$document->preserveWhiteSpace = false; | |
$document->formatOutput = true; | |
$document->save($argv[1]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment