/** * Imports a translation memory into the database. * * @since 0.4 * * @param LTTranslationMemory $tm * @param integer $memoryId */ protected function doTMImport(LTTranslationMemory $tm, $memoryId) { $dbw = wfGetDB(DB_MASTER); // Delete the memory from the db if already there. $dbw->delete('live_translate', array('memory_id' => $memoryId)); $idOffset = ($memoryId - 1) * 100000; $wordId = 0; $dbw->begin(); // Insert the memory in the db. foreach ($tm->getTranslationUnits() as $tu) { if ($GLOBALS['egLTRequireSignificance'] && !$tu->isSignificant()) { continue; } foreach ($tu->getVariants() as $language => $translations) { $primary = 1; foreach ($translations as $translation) { $dbw->insert('live_translate', array('word_id' => $idOffset + $wordId, 'word_language' => $this->cleanLanguage($language), 'word_translation' => $translation, 'word_primary' => $primary, 'memory_id' => $memoryId)); $primary = 0; } } $wordId++; if ($wordId % 500 == 0) { $dbw->commit(); $dbw->begin(); } } $dbw->update('live_translate_memories', array('memory_lang_count' => $tm->getLanguageAmount(), 'memory_tu_count' => $wordId, 'memory_version_hash' => uniqid()), array('memory_id' => $memoryId)); $dbw->commit(); }
/** * (non-PHPdoc) * @see LTTMParser::parse() */ public function parse($text) { $tm = new LTTranslationMemory(); $translationSets = array(); $text = preg_replace('/\\<!--([^(--\\>)]*)--\\>/', '', $text); $lines = explode("\n", $text); while (true) { $languages = array_shift($lines); if (trim($languages) != '') { break; } } $languages = array_map('trim', explode(',', $languages)); foreach ($lines as $line) { if (trim($line) == '') { continue; } $values = array_map('trim', explode(',', $line)); $tu = new LTTMUnit(); // If there is only one value, interpret it as "should never be translated", and add it for all languages. if (count($values) == 1) { foreach ($languages as $language) { // Add the translation (or translations) (value, array) of the word in the language (key). $tu->addVariants(array($language => array_map('trim', explode('|', $values[0])))); } } else { foreach ($languages as $nr => $language) { if (array_key_exists($nr, $values)) { // Add the translation (or translations) (value, array) of the word in the language (key). $tu->addVariants(array($language => array_map('trim', explode('|', $values[$nr])))); } } } $tm->addTranslationUnit($tu); } return $tm; }
/** * Sets the end element handler function for the XML parser parser.end_element_handler. * * @since 0.4 * * @param resource $parser The first parameter, parser, is a reference to the XML parser calling the handler. * @param string $name The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters. */ protected function endElementHandler($parser, $name) { switch (strtolower($name)) { case 'tu': // We are leaving the translation unit node, so add the translation unit to the translation memory. if ($this->tu->hasVariants()) { $this->tm->addTranslationUnit($this->tu); } break; case 'tuv': $this->tu->addVariant($this->currentLanguage, $this->currentTranslation); break; case 'seg': $this->insideSegment = false; break; case 'ut': $this->insideIgnoreNode = false; break; } }