public function ExportFile($strTemplateFile, $strTranslatedFile) { $intTime = time(); $arrSourceKey = $this->FileAsArray($strTemplateFile); $intElapsedTime = time() - $intTime; if ($intElapsedTime > 0) { // NarroLogger::LogDebug(sprintf('Preprocessing %s took %d seconds.', $this->objFile->FileName, $intElapsedTime)); } // NarroLogger::LogDebug(sprintf('Found %d contexts in file %s.', count($arrSourceKey), $this->objFile->FileName)); if (is_array($arrSourceKey)) { $arrSourceKey = $this->GetAccessKeys($arrSourceKey); $arrTranslation = $this->GetTranslations($arrSourceKey); $hndTranslationFile = fopen($strTranslatedFile, 'w'); if ($this->objProject->GetPreferenceValueByName('Export translators and reviewers in the file header as a comment') == 'Yes') { $arrUsers = array(); foreach ($this->objFile->GetTranslatorArray($this->objTargetLanguage->LanguageId) as $objUser) { $arrUsers[] = sprintf("# %s <%s>", $objUser->RealName, $objUser->Email); } if (count($arrUsers)) { fwrite($hndTranslationFile, sprintf("# Translator(s):\n#\n%s\n#\n", join("\n", $arrUsers))); } $arrUsers = array(); foreach ($this->objFile->GetReviewerArray($this->objTargetLanguage->LanguageId) as $objUser) { $arrUsers[] = sprintf("# %s <%s>", $objUser->RealName, $objUser->Email); } if (count($arrUsers)) { fwrite($hndTranslationFile, sprintf("# Reviewer(s):\n#\n%s\n#\n", join("\n", $arrUsers))); } } if ($this->objFile->Header) { fwrite($hndTranslationFile, $this->objFile->Header); } foreach ($arrSourceKey as $strContext => $objEntity) { if (isset($arrTranslation[$strContext])) { fwrite($hndTranslationFile, $objEntity->BeforeValue . $arrTranslation[$strContext] . $objEntity->AfterValue); } else { fwrite($hndTranslationFile, $objEntity->BeforeValue . $objEntity->Value . $objEntity->AfterValue); } } fclose($hndTranslationFile); NarroUtils::Chmod($strTranslatedFile, 0666); return true; } else { NarroLogger::LogWarn(sprintf('Found a empty template (%s), copying the original', $strTemplateFile)); copy($strTemplateFile, $strTranslatedFile); NarroUtils::Chmod($strTranslatedFile, 0666); return false; } }
public function ExportFile($strTemplateFile, $strTranslatedFile) { $arrSourceKey = $this->FileAsArray($strTemplateFile); $intElapsedTime = time() - $intTime; if ($intElapsedTime > 0) { // NarroLogger::LogDebug(sprintf('Preprocessing %s took %d seconds.', $this->objFile->FileName, $intElapsedTime)); } // NarroLogger::LogDebug(sprintf('Found %d contexts in file %s.', count($arrSourceKey), $this->objFile->FileName)); if (is_array($arrSourceKey)) { $arrTranslationObjects = NarroContextInfo::QueryArray(QQ::AndCondition(QQ::Equal(QQN::NarroContextInfo()->Context->FileId, $this->objFile->FileId), QQ::Equal(QQN::NarroContextInfo()->LanguageId, $this->objTargetLanguage->LanguageId), QQ::Equal(QQN::NarroContextInfo()->Context->Active, 1))); foreach ($arrTranslationObjects as $objNarroContextInfo) { $strTranslation = $this->GetExportedSuggestion($objNarroContextInfo); if (isset($arrSourceKey[$objNarroContextInfo->Context->Context])) { $arrSourceKey[$objNarroContextInfo->Context->Context]->Value = $strTranslation; } else { if ($this->blnSkipUntranslated == false) { $arrSourceKey[$objNarroContextInfo->Context->Context]->Value = $objNarroContextInfo->Context->Text->TextValue; } else { unset($arrTranslation[$objNarroContextInfo->Context->Context]); } } } $hndTranslationFile = fopen($strTranslatedFile, 'w'); foreach ($arrSourceKey as $strContext => $objEntity) { /* @var $objEntity NarroFileEntity */ fwrite($hndTranslationFile, $objEntity->Key . "\n"); fwrite($hndTranslationFile, $objEntity->Comment . "\n"); fwrite($hndTranslationFile, $objEntity->Value . "\n\n"); } fclose($hndTranslationFile); NarroUtils::Chmod($strTranslatedFile, 0666); return true; } else { NarroLogger::LogWarn(sprintf('Found a empty template (%s), copying the original', $strTemplateFile)); copy($strTemplateFile, $strTranslatedFile); NarroUtils::Chmod($strTranslatedFile, 0666); return false; } }
private function CreateNarroTemplate($intProjectId) { $strPoFile = __DOCROOT__ . __SUBDIRECTORY__ . '/locale/' . $this->objSourceLanguage->LanguageCode . '/narro.po'; NarroLogger::LogInfo(sprintf('Building a narro gettext template in %s.', $strPoFile)); $arrPermissions = NarroPermission::QueryArray(QQ::All(), QQ::Clause(QQ::OrderBy(QQN::NarroPermission()->PermissionName))); NarroLogger::LogInfo(sprintf('Found %d permission names to localize.', count($arrPermissions))); $arrRoles = NarroRole::QueryArray(QQ::All(), QQ::Clause(QQ::OrderBy(QQN::NarroRole()->RoleName))); NarroLogger::LogInfo(sprintf('Found %d role names to localize.', count($arrRoles))); $allFiles = NarroUtils::ListDirectory(realpath(dirname(__FILE__) . '/../../..'), null, '/.*\\/drafts\\/.*|.*\\/data\\/.*|.*\\/examples\\/.*|.*\\/qcubed_generated\\/.*/'); NarroLogger::LogInfo(sprintf('Found %d php files to search for localizable messages.', count($allFiles))); foreach ($allFiles as $strFileName) { if (pathinfo($strFileName, PATHINFO_EXTENSION) != 'php') { continue; } $strFile = file_get_contents($strFileName); $strShortPath = str_ireplace(realpath(__DOCROOT__ . __SUBDIRECTORY__) . '/', '', $strFileName); if (strpos($strShortPath, 'data') === 0) { continue; } if (strpos($strShortPath, 'includes/qcubed_generated') === 0) { continue; } $strFile = str_replace("\\'", "&&&escapedsimplequote&&&", $strFile); $strFile = str_replace('\\"', "&&&escapeddoublequote&&&", $strFile); if ($strFile) { preg_match_all('/([^a-zA-Z]t|NarroApp::Translate|QApplication::Translate|__t)\\s*\\(\\s*[\']([^\']{2,})[\']\\s*\\)/', $strFile, $arrMatches); if (isset($arrMatches[2])) { foreach ($arrMatches[2] as $intMatchNo => $strText) { if (trim($strText) != '') { $strText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $strText); $arrMessages[md5($strText)]['text'] = $strText; $arrMessages[md5($strText)]['files'][$strShortPath] = $strShortPath; $strSearchText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $arrMatches[0][$intMatchNo]); preg_match_all('/^.*' . preg_quote($strSearchText, '/') . '.*$/m', $strFile, $arrFullMatches); $arrMessages[md5($strText)]['context'] = '#. '; foreach ($arrFullMatches[0] as $strFullMatch) { if (trim($strFullMatch)) { $arrMessages[md5($strText)]['context'] .= trim($strFullMatch) . "\n"; } } } } } preg_match_all('/([^a-zA-Z]t|NarroApp::Translate|QApplication::Translate|__t)\\s*\\(\\s*[\\"]([^\\"]{2,})[\\"]\\s*\\)/', $strFile, $arrMatches); if (isset($arrMatches[2])) { foreach ($arrMatches[2] as $intMatchNo => $strText) { if (trim($strText) != '') { $strText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $strText); $arrMessages[md5($strText)]['text'] = $strText; $arrMessages[md5($strText)]['files'][$strShortPath] = $strShortPath; $strSearchText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $arrMatches[0][$intMatchNo]); preg_match_all('/^.*' . preg_quote($strSearchText, '/') . '.*$/m', $strFile, $arrFullMatches); $arrMessages[md5($strText)]['context'] = '#. '; foreach ($arrFullMatches[0] as $strFullMatch) { if (trim($strFullMatch)) { $arrMessages[md5($strText)]['context'] .= trim($strFullMatch) . "\n"; } } } } } preg_match_all('/([^a-zA-Z]t|NarroApp::Translate|QApplication::Translate|__t)\\s*\\(\\s*[\']([^\']{2,})[\']\\s*,\\s*[\']([^\']{2,})[\']\\s*,\\s*([^\\)]+)\\s*\\)/', $strFile, $arrMatches); if (isset($arrMatches[2])) { foreach ($arrMatches[2] as $intMatchNo => $strText) { if (trim($strText) != '') { $strText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $strText); $arrMessages[md5($strText)]['text'] = $strText; $arrMessages[md5($strText)]['files'][$strShortPath] = $strShortPath; $arrMessages[md5($strText)]['plural'] = $arrMatches[3][$intMatchNo]; $strSearchText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $arrMatches[0][$intMatchNo]); preg_match_all('/^.*' . preg_quote($strSearchText, '/') . '.*$/m', $strFile, $arrFullMatches); $arrMessages[md5($strText)]['context'] = '#. '; foreach ($arrFullMatches[0] as $strFullMatch) { if (trim($strFullMatch)) { $arrMessages[md5($strText)]['context'] .= trim($strFullMatch) . "\n"; } } } } } preg_match_all('/([^a-zA-Z]t|NarroApp::Translate|QApplication::Translate|__t)\\s*\\(\\s*[\\"]([^\\"]{2,})[\\"]\\s*,\\s*[\\"]([^\\"]{2,})[\\"]\\s*,\\s*([^\\)]+)\\s*\\)/', $strFile, $arrMatches); if (isset($arrMatches[2])) { foreach ($arrMatches[2] as $intMatchNo => $strText) { if (trim($strText) != '') { $strText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $strText); $arrMessages[md5($strText)]['text'] = $strText; $arrMessages[md5($strText)]['files'][$strShortPath] = $strShortPath; $arrMessages[md5($strText)]['plural'] = $arrMatches[3][$intMatchNo]; $strSearchText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $arrMatches[0][$intMatchNo]); preg_match_all('/^.*' . preg_quote($strSearchText, '/') . '.*$/m', $strFile, $arrFullMatches); $arrMessages[md5($strText)]['context'] = '#. '; foreach ($arrFullMatches[0] as $strFullMatch) { if (trim($strFullMatch)) { $arrMessages[md5($strText)]['context'] .= trim($strFullMatch) . "\n"; } } } } } preg_match_all('/NarroApp::RegisterPreference\\(\\s*\'([^\']+)\'\\s*,\\s*\'[^\']+\'\\s*,\\s*\'([^\']+)\'\\s*,\\s*/', $strFile, $arrMatches); if (isset($arrMatches[1])) { foreach ($arrMatches[1] as $intMatchNo => $strText) { if (trim($strText) != '') { $strText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $strText); $arrMessages[md5($strText)]['text'] = $strText; $arrMessages[md5($strText)]['files'][$strShortPath] = $strShortPath; $strSearchText = $arrMatches[0][$intMatchNo]; preg_match_all('/^.*' . preg_quote($strSearchText, '/') . '.*$/m', $strFile, $arrFullMatches); $arrMessages[md5($strText)]['context'] = "#. Preference name\n"; foreach ($arrFullMatches[0] as $strLine) { if (isset($strLine) && trim($strLine)) { $arrMessages[md5($strText)]['context'] .= trim($strLine) . "\n"; } } } } foreach ($arrMatches[2] as $intMatchNo => $strText) { if (trim($strText) != '') { $strText = str_replace(array("&&&escapedsimplequote&&&", "&&&escapeddoublequote&&&"), array("'", '\\"'), $strText); $arrMessages[md5($strText)]['text'] = $strText; $arrMessages[md5($strText)]['files'][$strShortPath] = $strShortPath; $strSearchText = $arrMatches[0][$intMatchNo]; preg_match_all('/^.*' . preg_quote($strSearchText, '/') . '.*$/m', $strFile, $arrFullMatches); $arrMessages[md5($strText)]['context'] = "#. Preference description\n"; foreach ($arrFullMatches[0] as $strLine) { if (isset($strLine) && trim($strLine)) { $arrMessages[md5($strText)]['context'] .= trim($strLine) . "\n"; } } } } } if (preg_match_all('/t\\(\\$[a-zA-Z]+\\-\\>LanguageName/', $strFile, $arrMatches)) { if (!isset($arrLanguages)) { $arrLanguages = NarroLanguage::QueryArray(QQ::All(), QQ::Clause(QQ::OrderBy(QQN::NarroLanguage()->LanguageName))); } $strLangContext = '#. '; foreach ($arrMatches as $intMatchNo => $arrVal) { $strSearchText = $arrMatches[0][$intMatchNo]; preg_match_all('/^.*' . preg_quote($strSearchText, '/') . '.*$/m', $strFile, $arrFullMatches); foreach ($arrFullMatches[0] as $strLine) { if (isset($strLine) && trim($strLine)) { $strLangContext .= trim($strLine) . "\n"; } } } if (is_array($arrLanguages)) { foreach ($arrLanguages as $objLanguage) { $arrMessages[md5($objLanguage->LanguageName)]['text'] = $objLanguage->LanguageName; $arrMessages[md5($objLanguage->LanguageName)]['files'][$strShortPath] = $strShortPath; $arrMessages[md5($objLanguage->LanguageName)]['context'] = $strLangContext; } } } if (preg_match_all('/t\\(\\$[a-zA-Z]+\\-\\>RoleName/', $strFile, $arrMatches)) { $strLangContext = '#. '; foreach ($arrMatches as $intMatchNo => $arrVal) { $strSearchText = $arrMatches[0][$intMatchNo]; preg_match_all('/^.*' . preg_quote($strSearchText, '/') . '.*$/m', $strFile, $arrFullMatches); foreach ($arrFullMatches[0] as $strLine) { if (isset($strLine) && trim($strLine)) { $strLangContext .= trim($strLine) . "\n"; } } } if (is_array($arrRoles)) { foreach ($arrRoles as $objRole) { $arrMessages[md5($objRole->RoleName)]['text'] = $objRole->RoleName; $arrMessages[md5($objRole->RoleName)]['files'][$strShortPath] = $strShortPath; $arrMessages[md5($objRole->RoleName)]['context'] = $strLangContext; } } } if (preg_match_all('/t\\(\\$[a-zA-Z]+\\-\\>PermissionName/', $strFile, $arrMatches)) { $strLangContext = '#. '; foreach ($arrMatches as $intMatchNo => $arrVal) { $strSearchText = $arrMatches[0][$intMatchNo]; preg_match_all('/^.*' . preg_quote($strSearchText, '/') . '.*$/m', $strFile, $arrFullMatches); foreach ($arrFullMatches[0] as $strLine) { if (isset($strLine) && trim($strLine)) { $strLangContext .= trim($strLine) . "\n"; } } } if (is_array($arrPermissions)) { foreach ($arrPermissions as $objPermission) { $arrMessages[md5($objPermission->PermissionName)]['text'] = $objPermission->PermissionName; $arrMessages[md5($objPermission->PermissionName)]['files'][$strShortPath] = $strShortPath; $arrMessages[md5($objPermission->PermissionName)]['context'] = $strLangContext; } } } } } $strPoHeader = '#, fuzzy' . "\n" . 'msgid ""' . "\n" . 'msgstr ""' . "\n" . '"Project-Id-Version: Narro ' . NARRO_VERSION . "\n" . '"Report-Msgid-Bugs-To: alexxed@gmail.com\\n"' . "\n" . '"POT-Creation-Date: ' . date('Y-d-m H:iO') . '\\n"' . "\n" . '"PO-Revision-Date: ' . date('Y-d-m H:iO') . '\\n"' . "\n" . '"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"' . "\n" . '"Language-Team: LANGUAGE <*****@*****.**>\\n"' . "\n" . '"MIME-Version: 1.0\\n"' . "\n" . '"Content-Type: text/plain; charset=UTF-8\\n"' . "\n" . '"Content-Transfer-Encoding: 8bit\\n"' . "\n" . '"Plural-Forms: nplurals=2; plural=n != 1;\\n"' . "\n" . '"X-Generator: Narro\\n"' . "\n"; $hndFile = fopen($strPoFile, 'w'); if (!$hndFile) { NarroLogger::LogError(sprintf('Error while opening the po file "%s" for writing.', $strPoFile)); } fputs($hndFile, $strPoHeader); // Obtain a list of columns foreach ($arrMessages as $key => $row) { $texts[$key] = $row['text']; } //array_multisort($texts, SORT_ASC, SORT_STRING, $arrMessages); foreach ($arrMessages as $intKey => $arrMsgData) { if (isset($arrMsgData['plural'])) { fputs($hndFile, sprintf("#: %s\nmsgid \"%s\"\nmsgid_plural \"%s\"\nmsgstr[0] \"\"\nmsgstr[1] \"\"\n\n", join(' ', array_values($arrMsgData['files'])), str_replace(array('"', "\n"), array('\\"', '\\n'), $arrMsgData['text']), str_replace(array('"', "\n"), array('\\"', '\\n'), $arrMsgData['plural']))); } else { if (!isset($arrMsgData['files'])) { print_r($arrMsgData); } else { fputs($hndFile, sprintf("%s\n#: %s\nmsgid \"%s\"\nmsgstr \"\"\n\n", isset($arrMsgData['context']) ? str_replace("\n", "\n#. ", trim($arrMsgData['context'])) . '' : '', join(' ', array_values($arrMsgData['files'])), str_replace(array('"', "\n"), array('\\"', '\\n'), $arrMsgData['text']))); } } } fclose($hndFile); NarroUtils::Chmod($strPoFile, 0666); NarroLogger::LogInfo('Wrote a new Narro template file in ' . $strPoFile); }