function extractStringsFromDocument($template, &$strings) { libxml_use_internal_errors(true); libxml_clear_errors(); $dom = new DOMDocument('1.0', 'UTF-8'); $dom->substituteEntities = false; $dom->encoding = 'UTF-8'; $contents = file_get_contents($template); if ($contents === false) { I2CE::raiseError("Could not read file {$template}"); return false; } libxml_clear_errors(); if (!$dom->loadHTML(wrapHTMLinUTF8($contents))) { I2CE::raiseError("Problem importing {$template}. Something is wrong with the html."); return false; } $errors = libxml_get_errors(); libxml_clear_errors(); //$success = ($success && (count($errors) == 0)); if (count($errors) > 0) { I2CE::raiseError("Warning on loading {$template}:\n" . print_r($errors, true)); } $start_node = null; $xpath = new DOMXPath($dom); if (preg_match('/\\<\\s*html/m', $contents)) { $start_node = $xpath->query('//html'); if ($start_node->length != 1) { I2CE::raiseError("Could not find html node"); return false; } $start_node = $start_node->item(0); } else { if (preg_match('/\\<\\s*head/m', $contents)) { $start_node = $xpath->query('//head'); if ($start_node->length != 1) { I2CE::raiseError("Could not find head node"); return false; } $start_node = $start_node->item(0); if (preg_match('/\\<\\s*body/m', $contents)) { $start_node = $start_node->parentNode; } } else { $start_node = $xpath->query('//body'); if ($start_node->length != 1) { I2CE::raiseError("Could not find body node"); return false; } $start_node = $start_node->item(0); } } extractTextFromNode($start_node, $strings); return true; }
function translateNode($node) { if (!$node instanceof DOMNode) { var_dump($node); die; return; } global $locale_dashed; global $translations; global $changed_text; global $rtl; $attrs = array('span' => array('text', 'head', 'title')); if ($node instanceof DOMText) { $trans = translate($node->textContent); if ($trans !== false) { $node->deleteData(0, $node->length); $node->appendData($trans); if ($rtl && $node->parentNode instanceof DOMElement) { $node->parentNode->setAttribute('dir', 'rtl'); } } } $block = false; if ($node instanceof DOMElement) { if (strtolower($node->tagName) == 'script' || strtolower($node->tagName) == 'style') { return; } if ($node->hasAttribute('translator_comment')) { $node->removeAttribute('translator_comment'); } if ($node->hasAttribute('printf')) { list($trans_sing, $trans_remainder) = extractPrintF($node->getAttribute('printf')); if ($node->hasAttribute('printf_plural') && $node->hasAttribute('printf_form')) { if (array_key_exists($trans_sing, $translations) && is_array($translations[$trans_sing])) { $changed_text = true; //we have translated plural information. $node->removeAttribute('printf_plural'); $node->removeAttribute('printf_sing'); foreach ($translations[$trans_sing] as $n => $trans) { $node->setAttribute('printf_' . $n, "'" . $trans . "'"); } $node->setAttribute('lang', $locale_dashed); if ($rtl) { $node->setAttribute('dir', 'rtl'); } } } else { $trans_sing = translate($trans_sing); if ($trans_sing !== false) { $node->setAttribute('printf', "'" . $trans_sing . "'" . $trans_remainder); if ($rtl) { $node->setAttribute('dir', 'rtl'); } } } return; } else { if ($node->hasAttribute('lang')) { $lang = $node->getAttribute('lang'); if (strtr($lang, '-', '-') == I2CE_Locales::DEFAULT_LOCALE) { //if we have lang='en-US' of lang='en_US' attribute, treat this as a block $block = $node->tagName; } } else { if (array_key_exists($node->tagName, $attrs)) { foreach ($attrs[$node->tagName] as $attr) { if ($node->hasAttribute($attr)) { $trans = translate($node->getAttribute($attr)); if ($trans !== false) { $node->setAttribute($attr, $trans); } } } } } } } if (!$block) { if ($node->childNodes instanceof DOMNodeList) { for ($i = 0; $i < $node->childNodes->length; $i++) { translateNode($node->childNodes->item($i)); } } } else { $text = checkExtractedString(getInnerHTML($node)); if (strlen($text) > 0 && ($trans = translate($text)) !== false) { $html = false; $body = false; $head = false; $wrapped = "<{$block}>{$trans}</{$block}>"; $wrapped = wrapHTMLinUTF8($wrapped); $import_dom = new DOMDocument(); $orig_dom = $node->ownerDocument; libxml_clear_errors(); $success = $import_dom->loadHTML($wrapped); $success = $success && count(libxml_get_errors()) == 0; if (!$success) { $tmp_dom = new DOMDocument(); if (!$tmp_dom->loadHTML($wrapped)) { die("Problem importing translated block"); } else { die("Yell at carl -- problem with wrapping"); } } $xpath = new DOMXpath($import_dom); $nodeList = $xpath->query('//' . $block . '[1]'); if ($nodeList->length != 1) { die("Yell at carl about finding blocks //{$block}" . "[1]" . " -- " . $nodeList->length . "\n"); } $new_node = $orig_dom->importNode($nodeList->item(0), true); // var_dump($orig_dom); // var_dump($import_dom); // var_dump($new_node->ownerDocument); $new_node->setAttribute('lang', $locale_dashed); if ($rtl) { $new_node->setAttribute('dir', 'rtl'); } $node->parentNode->replaceChild($new_node, $node); } } }