Skip to content

Instantly share code, notes, and snippets.

@ohvitorino
Created February 3, 2018 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ohvitorino/1e09fcdcd5ea6f5fb745184d79ef4724 to your computer and use it in GitHub Desktop.
Save ohvitorino/1e09fcdcd5ea6f5fb745184d79ef4724 to your computer and use it in GitHub Desktop.
ForkCMS translation helper.
<?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