/** * Short description of method write * * @access public * @author firstname and lastname of author, <*****@*****.**> * @return mixed */ public function write() { $buffer = ''; $file = $this->getTranslationFile(); // Add PO Headers. $buffer .= 'msgid ""' . "\n"; $buffer .= 'msgstr ""' . "\n"; // If the TranslationFile is a specific POFile instance, we add PO Headers // to the output. if (get_class($this->getTranslationFile()) == 'tao_helpers_translation_POFile') { foreach ($file->getHeaders() as $name => $value) { $buffer .= '"' . $name . ': ' . $value . '\\n"' . "\n"; } } // Write all Translation Units. $buffer .= "\n"; foreach ($this->getTranslationFile()->getTranslationUnits() as $tu) { $c = tao_helpers_translation_POUtils::sanitize($tu->getContext(), true); $s = tao_helpers_translation_POUtils::sanitize($tu->getSource(), true); $t = tao_helpers_translation_POUtils::sanitize($tu->getTarget(), true); $a = tao_helpers_translation_POUtils::serializeAnnotations($tu->getAnnotations()); if (!empty($a)) { $buffer .= "{$a}\n"; } if ($c) { $buffer .= "msgctxt \"{$c}\"\n"; } $buffer .= "msgid \"{$s}\"\n"; $buffer .= "msgstr \"{$t}\"\n"; $buffer .= "\n"; } return file_put_contents($this->getFilePath(), $buffer); }
/** * Short description of method read * * @access public * @author firstname and lastname of author, <*****@*****.**> * @throws tao_helpers_translation_TranslationException * @return mixed */ public function read() { $file = $this->getFilePath(); if (!file_exists($file)) { throw new tao_helpers_translation_TranslationException("The translation file '{$file}' does not exist."); } // Create the translation file. $tf = new tao_helpers_translation_POFile(); $fc = implode('', file($file)); $matched = preg_match_all('/((?:#[\\.:,\\|]{0,1}\\s+(?:.*?)\\n)*)' . '(msgctxt\\s+(?:"(?:[^"]|\\\\")*?"\\s*)+)?' . '(msgid\\s+(?:"(?:[^"]|\\\\")*?"\\s*)+)\\s+' . '(msgstr\\s+(?:"(?:[^"]|\\\\")*?(?<!\\\\)"\\s*)+)/', $fc, $matches); preg_match('/sourceLanguage: (.*?)\\n/s', $fc, $sourceLanguage); preg_match('/targetLanguage: (.*?)\\n/s', $fc, $targetLanguage); if (count($sourceLanguage)) { $tf->setSourceLanguage(substr($sourceLanguage[1], 0, 5)); } if (count($targetLanguage)) { $tf->setTargetLanguage(substr($targetLanguage[1], 0, 5)); } if ($matched) { for ($i = 0; $i < $matched; $i++) { $annotations = $matches[1][$i]; $msgctxt = preg_replace('/\\s*msgctxt\\s*"(.*)"\\s*/s', '\\1', $matches[2][$i]); $msgid = preg_replace('/\\s*msgid\\s*"(.*)"\\s*/s', '\\1', $matches[3][$i]); $msgstr = preg_replace('/\\s*msgstr\\s*"(.*)"\\s*/s', '\\1', $matches[4][$i]); // Do not include meta data as a translation unit.. if ($msgid !== '') { // Sanitze the strings. $msgid = tao_helpers_translation_POUtils::sanitize($msgid); $msgstr = tao_helpers_translation_POUtils::sanitize($msgstr); $msgctxt = tao_helpers_translation_POUtils::sanitize($msgctxt); $tu = new tao_helpers_translation_POTranslationUnit(); // Set up source & target. $tu->setSource($msgid); if ($msgstr !== '') { $tu->setTarget($msgstr); } if ($msgctxt) { $tu->setContext($msgctxt); } // Deal with annotations $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($annotations); foreach ($annotations as $name => $value) { $tu->addAnnotation($name, $value); } $tf->addTranslationUnit($tu); } } } $this->setTranslationFile($tf); }
/** * Short description of method getTranslationsInFile * * @access private * @author firstname and lastname of author, <*****@*****.**> * @param string filePath * @return mixed */ private function getTranslationsInFile($filePath) { // File extension ? $extOk = false; foreach ($this->getFileTypes() as $exp) { if (@preg_match("/\\.{$exp}\$/", $filePath)) { $extOk = true; break; } } if ($extOk) { // We read the file. $lines = file($filePath); foreach ($lines as $line) { $strings = array(); $patternMatch1 = array(); $patternMatch2 = array(); preg_match_all("/__\\(['\"](.*?)['\"]\\)/u", $line, $patternMatch1); preg_match_all("/\\{\\{__ ['\"](.*?)['\"]\\}\\}/u", $line, $patternMatch2); if (!empty($patternMatch1[1])) { $strings = $patternMatch1[1]; } if (!empty($patternMatch2[1])) { $strings = array_merge($strings, $patternMatch2[1]); } //preg_match_all("/__(\(| )+['\"](.*?)['\"](\))?/u", $line, $string); //lookup for __('to translate') or __ 'to translate' // preg_match_all("/__(\(| )+(('(.*?)')|(\"(.*?)\"))(\))?/u", $line, $string); foreach ($strings as $s) { $tu = new tao_helpers_translation_TranslationUnit(); $tu->setSource(tao_helpers_translation_POUtils::sanitize($s)); $tus = $this->getTranslationUnits(); $found = false; // We must add the string as a new TranslationUnit only // if a similiar source does not exist. foreach ($tus as $t) { if ($tu->getSource() == $t->getSource()) { $found = true; break; } } if (!$found) { array_push($tus, $tu); $this->setTranslationUnits($tus); } } } } }
/** * Short description of method getTranslationsInFile * * @access private * @author firstname and lastname of author, <*****@*****.**> * @param string $filePath */ private function getTranslationsInFile($filePath) { // File extension ? $extOk = in_array(\Jig\Utils\FsUtils::getFileExtension($filePath), $this->getFileTypes()); if ($extOk) { foreach ($this->getBannedFileType() as $bannedExt) { $extOk &= substr_compare($filePath, $bannedExt, strlen($filePath) - strlen($bannedExt), strlen($bannedExt)) !== 0; } } if ($extOk) { // We read the file. $lines = file($filePath); foreach ($lines as $line) { $strings = $this->getTranslationPhrases($line); //preg_match_all("/__(\(| )+['\"](.*?)['\"](\))?/u", $line, $string); //lookup for __('to translate') or __ 'to translate' // preg_match_all("/__(\(| )+(('(.*?)')|(\"(.*?)\"))(\))?/u", $line, $string); foreach ($strings as $s) { $tu = new tao_helpers_translation_TranslationUnit(); $tu->setSource(tao_helpers_translation_POUtils::sanitize($s)); $tus = $this->getTranslationUnits(); $found = false; // We must add the string as a new TranslationUnit only // if a similiar source does not exist. foreach ($tus as $t) { if ($tu->getSource() == $t->getSource()) { $found = true; break; } } if (!$found) { $tus[] = $tu; $this->setTranslationUnits($tus); } } } } }