function extractTextFromNode($node, &$strings) { global $file; $attrs = array('span' => array('text', 'head', 'title')); if ($node instanceof DOMText) { $text = checkExtractedString($node->textContent); if (strlen($text) == 0) { return; } $line_no = null; if (version_compare(PHP_VERSION, '5.3.0') >= 0) { $line_no = $node->getLineNo(); } setSourceComment($strings, $text, $line_no); return; } if ($node instanceof DOMElement) { $check_children = true; if (strtolower($node->tagName) == 'script' || strtolower($node->tagName) == 'style') { //skip a script node: Thanks to Federico Vera for pointing this out. return; } if ($node->hasAttribute('printf')) { $text = checkExtractedPrintF($node->getAttribute('printf')); if (strlen($text) == 0) { continue; } if ($node->hasAttribute('printf_plural') && $node->hasAttribute('printf_form')) { $text_form = checkExtractedString($node->getAttribute('printf_form')); if (strlen($text_form) == 0) { continue; } $text_plural = checkExtractedString($node->getAttribute('printf_plural')); if (strlen($text_plural) == 0) { continue; } addPluralString($strings, $text, $text_plural); } else { //assume it is only the singular form addString($strings, $text); } setFormatComment($strings, $text, 'c-format'); $line_no = null; if (version_compare(PHP_VERSION, '5.3.0') >= 0) { $line_no = $node->getLineNo(); } setSourceComment($strings, $text, $line_no); if ($node->hasAttribute('translator_comment')) { setExtractedComment($strings, $text, $node->getAttribute('translator_comment')); } 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; $text = checkExtractedString(getInnerHTML($node)); if (strlen($text) == 0) { return; } $line_no = null; if (version_compare(PHP_VERSION, '5.3.0') >= 0) { $line_no = $node->getLineNo(); } setSourceComment($strings, $text, $line_no); if ($node->hasAttribute('translator_comment')) { setExtractedComment($strings, $text, $node->getAttribute('translator_comment')); } $check_children = false; } } } if (array_key_exists($node->tagName, $attrs)) { foreach ($attrs[$node->tagName] as $attr) { if ($node->hasAttribute($attr)) { $text = checkExtractedString($node->getAttribute($attr)); if (strlen($text) == 0) { continue; } $line_no = null; if (version_compare(PHP_VERSION, '5.3.0') >= 0) { $line_no = $node->getLineNo(); } setSourceComment($strings, $text, $line_no); } } } if ($check_children) { for ($i = 0; $i < $node->childNodes->length; $i++) { extractTextFromNode($node->childNodes->item($i), $strings); } } } }
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); } } }