public function run() { $extensions = common_ext_ExtensionsManager::singleton()->getInstalledExtensions(); $languages = tao_helpers_translation_Utils::getAvailableLanguages(); $path = ROOT_PATH . 'tao/views/locales/'; foreach ($languages as $langCode) { try { $bundle = new TranslationBundle($langCode, $extensions); $file = $bundle->generateTo($path); if ($file) { $this->out('Success: ' . $file); } else { $this->out('Failure generating: ' . $file); } } catch (common_excpetion_Error $e) { $this->out('Failure: ' . $e->getMessage()); } } }
/** * Creates a new instance of TranslationFile for a specific source and * language. * * @access public * @author Jerome Bogaerts, <*****@*****.**> * @return mixed */ public function __construct() { $this->setSourceLanguage(tao_helpers_translation_Utils::getDefaultLanguage()); $this->setTargetLanguage(tao_helpers_translation_Utils::getDefaultLanguage()); $this->setTranslationUnits(array()); }
public function testPOAnnotationsReading() { $string = "# This is a comment.\n"; $string .= "#, flag1 composed-flag flag2"; $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string); $this->assertEquals($annotations, array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => 'This is a comment.', tao_helpers_translation_POTranslationUnit::FLAGS => 'flag1 composed-flag flag2')); $string = "# The first line of my comment continues...\n"; $string .= "# At the second line.\n"; $string .= "#. Please do not touch this!\n"; $string .= "#| msgctxt A previous testing context.\n"; $string .= "#| msgid previous-untranslated-string-singular\n"; $string .= "#| msgid_plural previous-untranslated-string-plural\n"; $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string); $this->assertEquals($annotations, array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => "The first line of my comment continues...\nAt the second line.", tao_helpers_translation_POTranslationUnit::EXTRACTED_COMMENTS => "Please do not touch this!", tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGCTXT => "A previous testing context.", tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID => "previous-untranslated-string-singular", tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGID_PLURAL => "previous-untranslated-string-plural")); $string = "# هذا تعليق\n"; $string .= "# مع خطوط متعددة في الداخل.\n"; $string .= "#. لا تغير من فضلك!\n"; $string .= "#| msgctxt السابقة السياق."; $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string); $this->assertEquals($annotations, array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => "هذا تعليق\nمع خطوط متعددة في الداخل.", tao_helpers_translation_POTranslationUnit::EXTRACTED_COMMENTS => "لا تغير من فضلك!", tao_helpers_translation_POTranslationUnit::PREVIOUS_MSGCTXT => "السابقة السياق.")); $string = "^ This should not w#ork but the next...\n"; $string .= "#, flag-read"; $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string); $this->assertEquals($annotations, array(tao_helpers_translation_POTranslationUnit::FLAGS => 'flag-read')); $string = ""; $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($string); $this->assertEquals($annotations, array()); $reader = new tao_helpers_translation_POFileReader(dirname(__FILE__) . self::ANNOTATIONS_PO); $reader->read(); $tf = $reader->getTranslationFile(); $tus = $tf->getTranslationUnits(); $this->assertEquals(count($tus), 6); $this->assertEquals($tus[0]->getAnnotations(), array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => 'This is a comment', 'sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage())); $this->assertEquals($tus[1]->getAnnotations(), array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => 'This is another comment', tao_helpers_translation_POTranslationUnit::FLAGS => 'flag1 composed-flag flag2 tao-public', 'sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage())); $this->assertEquals($tus[2]->getAnnotations(), array(tao_helpers_translation_POTranslationUnit::TRANSLATOR_COMMENTS => "This is a multiline...\ncomment.", 'sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage())); $this->assertEquals($tus[3]->getAnnotations(), array('sourceLanguage' => tao_helpers_translation_Utils::getDefaultLanguage(), 'targetLanguage' => tao_helpers_translation_Utils::getDefaultLanguage())); // Test flag related interface on POTranslationUnit & POTranslationFile. $this->assertTrue($tus[5]->hasFlag('flag4')); $this->assertEquals($tus[5]->getFlags(), array('flag4')); $tus[5]->addFlag('new-flag'); $this->assertTrue($tus[5]->hasFlag('new-flag')); $this->assertEquals($tus[5]->getFlags(), array('flag4', 'new-flag')); $tus[5]->addFlag('new-flag'); $this->assertEquals($tus[5]->getFlags(), array('flag4', 'new-flag')); $tus[5]->addFlag('flag5'); $this->assertEquals($tus[5]->getAnnotation(tao_helpers_translation_POTranslationUnit::FLAGS), array('name' => tao_helpers_translation_POTranslationUnit::FLAGS, 'value' => 'flag4 new-flag flag5')); $tus[5]->removeFlag('new-flag'); $this->assertEquals($tus[5]->getFlags(), array('flag4', 'flag5')); $flagTus = $tf->getByFlag('composed-flag'); $this->assertEquals(count($flagTus), 2); $this->assertEquals($flagTus[0]->getSource(), "Thïs téxt cöntàin\$ wéîRd chárâctêrS beçÁuse öf I18N"); $this->assertEquals($flagTus[1]->getSource(), "This one contains the same flag as the second one"); $flagTus = $tf->getByFlags(array('composed-flag', 'flag2')); $this->assertEquals(count($flagTus), 2); $this->assertEquals($flagTus[0]->getSource(), "Thïs téxt cöntàin\$ wéîRd chárâctêrS beçÁuse öf I18N"); $this->assertEquals($flagTus[1]->getSource(), "This one contains the same flag as the second one"); // Reload the file. // We will check if when the file is written again, we get the same result. // In other words, we check idempotency after read/write. // We will compare TranslationFiles $tf1 & $tf2. $reader->read(); $tf1 = $reader->getTranslationFile(); // We write $tf1. $path = tempnam('/tmp', self::TEMP_PO); $writer = new tao_helpers_translation_POFileWriter($path, $tf1); $writer->write(); // We read $tf2 to be compared with $tf1 $reader->setFilePath($path); $reader->read(); $tf2 = $reader->getTranslationFile(); $this->assertEquals($tf1->count(), 6); $this->assertEquals($tf2->count(), 6); $tus1 = $tf1->getTranslationUnits(); $tus2 = $tf2->getTranslationUnits(); $this->assertEquals($tus1[0]->getAnnotations(), $tus2[0]->getAnnotations()); $this->assertEquals($tus1[1]->getAnnotations(), $tus2[1]->getAnnotations()); $this->assertEquals($tus1[2]->getAnnotations(), $tus2[2]->getAnnotations()); $this->assertEquals($tus1[3]->getAnnotations(), $tus2[3]->getAnnotations()); $this->assertEquals($tus1[4]->getAnnotations(), $tus2[4]->getAnnotations()); $this->assertEquals($tus1[5]->getAnnotations(), $tus2[5]->getAnnotations()); unlink($path); }
/** * Creates a new instance of Translation Unit with specific source & target. * * @access public * @author Jerome Bogaerts, <*****@*****.**> * @return mixed */ public function __construct() { // Default values for source and target languages are en-US. $this->setSourceLanguage(tao_helpers_translation_Utils::getDefaultLanguage()); $this->setTargetLanguage(tao_helpers_translation_Utils::getDefaultLanguage()); }
/** * @param $f * @param $translatableProperties * @return tao_helpers_translation_POFile * @throws tao_helpers_translation_TranslationException */ protected function extractPoFileFromRDF($f, $translatableProperties) { $modelExtractor = new tao_helpers_translation_POExtractor(array($f)); $modelExtractor->setTranslatableProperties($translatableProperties); $modelExtractor->extract(); $translationFile = new tao_helpers_translation_POFile(); $translationFile->setSourceLanguage(tao_helpers_translation_Utils::getDefaultLanguage()); $translationFile->setTargetLanguage($this->options['language']); $translationFile->addTranslationUnits($modelExtractor->getTranslationUnits()); $translationFile->setExtensionId($this->options['extension']); $this->preparePOFile($translationFile); return $translationFile; }
/** * Short description of method read * * @access public * @author Jerome Bogaerts, <*****@*****.**> * @return mixed */ public function read() { $translationUnits = array(); try { $translationFile = $this->getTranslationFile(); } catch (tao_helpers_translation_TranslationException $e) { $translationFile = new tao_helpers_translation_RDFTranslationFile(); } $this->setTranslationFile($translationFile); $inputFile = $this->getFilePath(); if (file_exists($inputFile)) { if (is_file($inputFile)) { if (is_readable($inputFile)) { try { $doc = new DOMDocument('1.0', 'UTF-8'); $doc->load($inputFile); $xpath = new DOMXPath($doc); $rdfNS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; $xmlNS = 'http://www.w3.org/XML/1998/namespace'; $xpath->registerNamespace('rdf', $rdfNS); $rootNodes = $xpath->query('//rdf:RDF'); if ($rootNodes->length == 1) { // Try to get annotations from the root node. $sibling = $rootNodes->item(0)->previousSibling; while ($sibling !== null) { if ($sibling instanceof DOMNode && $sibling->nodeType == XML_COMMENT_NODE) { $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations($sibling->data); $translationFile->setAnnotations($annotations); if (isset($annotations['sourceLanguage'])) { $translationFile->setSourceLanguage($annotations['sourceLanguage']); } if (isset($annotations['targetLanguage'])) { $translationFile->setTargetLanguage($annotations['targetLanguage']); } break; } $sibling = $sibling->previousSibling; } $descriptions = $xpath->query('//rdf:Description'); foreach ($descriptions as $description) { if ($description->hasAttributeNS($rdfNS, 'about')) { $subject = $description->getAttributeNS($rdfNS, 'about'); // Let's retrieve properties. foreach ($description->childNodes as $property) { if ($property->nodeType == XML_ELEMENT_NODE) { // Retrieve namespace uri of this node. if ($property->namespaceURI != null) { $predicate = $property->namespaceURI . $property->localName; // Retrieve an hypothetic target language. $lang = tao_helpers_translation_Utils::getDefaultLanguage(); if ($property->hasAttributeNS($xmlNS, 'lang')) { $lang = $property->getAttributeNS($xmlNS, 'lang'); } $object = $property->nodeValue; $tu = new tao_helpers_translation_RDFTranslationUnit(''); $tu->setTargetLanguage($lang); $tu->setTarget($object); $tu->setSubject($subject); $tu->setPredicate($predicate); // Try to get annotations. $sibling = $property->previousSibling; while ($sibling !== null) { if ($sibling instanceof DOMNode && $sibling->nodeType == XML_COMMENT_NODE) { // We should have the annotations we are looking for. $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations($sibling->data); $tu->setAnnotations($annotations); // Set the found sources and sourcelanguages if found. if (isset($annotations['source'])) { $tu->setSource($annotations['source']); } } $sibling = $sibling->previousSibling; } $translationUnits[] = $tu; } } } } } $this->getTranslationFile()->addTranslationUnits($translationUnits); } else { throw new tao_helpers_translation_TranslationException("'{$inputFile}' has no rdf:RDF root node or more than one rdf:RDF root node."); } } catch (DOMException $e) { throw new tao_helpers_translation_TranslationException("'{$inputFile}' cannot be parsed."); } } else { throw new tao_helpers_translation_TranslationException("'{$inputFile}' cannot be read. Check your system permissions."); } } else { throw new tao_helpers_translation_TranslationException("'{$inputFile}' is not a file."); } } else { throw new tao_helpers_translation_TranslationException("The file '{$inputFile}' does not exist."); } }
/** * * @author Lionel Lecaque, lionel@taotesting.com */ public function generateClientBundles($checkPreviousBundlue = false) { $returnValue = array(); $extensions = common_ext_ExtensionsManager::singleton()->getInstalledExtensions(); // lookup for languages into tao $languages = tao_helpers_translation_Utils::getAvailableLanguages(); $path = ROOT_PATH . 'tao/views/locales/'; $generated = 0; $generate = true; foreach ($languages as $langCode) { try { $bundle = new TranslationBundle($langCode, $extensions); if ($checkPreviousBundlue) { $currenBundle = $path . $langCode . '.json'; if (file_exists($currenBundle)) { $bundleData = json_decode(file_get_contents($currenBundle), true); if ($bundleData['serial'] === $bundle->getSerial()) { $generate = false; } } if ($generate) { $file = $bundle->generateTo($path, false); } } else { $file = $bundle->generateTo($path); } if ($file) { $generated++; $returnValue[] = $file; } else { if ($generate) { common_Logger::e('Failure generating message.js for lang ' . $langCode); } else { common_Logger::d('Actual File is more recent, skip ' . $langCode); } } } catch (common_excpetion_Error $e) { common_Logger::e('Failure: ' . $e->getMessage()); } } common_Logger::i($generated . ' translation bundles have been (re)generated'); return $returnValue; }