public function updateLanguagePlugin($plugin, $idLanguage) { if (!file_exists(PATH_PLUGINS . $plugin)) { throw new Exception('The plugin ' . $plugin . ' not exist'); die; } if (!file_exists(PATH_PLUGINS . $plugin . PATH_SEP . 'translations' . PATH_SEP . 'translations.php')) { throw new Exception('Translations.php not exist in plugin ' . $plugin); } if (!file_exists(PATH_PLUGINS . $plugin . PATH_SEP . 'translations' . PATH_SEP . $plugin . '.' . $idLanguage . '.po')) { throw new Exception('The file ' . $plugin . '.' . $idLanguage . '.po not exists'); die; } $languageFile = PATH_PLUGINS . $plugin . PATH_SEP . 'translations' . PATH_SEP . $plugin . '.' . $idLanguage . '.po'; try { G::LoadSystem('i18n_po'); $POFile = new i18n_PO($languageFile); $POFile->readInit(); $POHeaders = $POFile->getHeaders(); $oTranslation = new Translation(); $countItems = 0; $countItemsSuccess = 0; $errorMsg = ''; while ($rowTranslation = $POFile->getTranslation()) { $countItems++; if (!isset($POFile->translatorComments[0]) || !isset($POFile->translatorComments[1]) || !isset($POFile->references[0])) { throw new Exception('The .po file doesn\'t have valid directives for Processmaker!'); } foreach ($POFile->translatorComments as $a => $aux) { $identifier = ''; $context = ''; $aux = trim($aux); if ($aux == 'TRANSLATION') { $identifier = $aux; } else { $var = explode('/', $aux); if ($var[0] == 'LABEL') { $context = $aux; } if ($var[0] == 'JAVASCRIPT') { $context = $aux; } } if ($identifier == '' && $context == '') { $context = $aux; } if (preg_match('/^([\\w-]+)\\/([\\w-]+\\/*[\\w-]*\\.xml\\?)/', $aux, $match)) { $identifier = $aux; } else { if (preg_match('/^([\\w-]+)\\/([\\w-]+\\/*[\\w-]*\\.xml$)/', $aux, $match)) { $context = $aux; } } } $reference = $POFile->references[0]; if ($identifier != 'TRANSLATION') { $xmlForm = $context; //erik: expresion to prevent and hable correctly dropdown values like -1, -2 etc. preg_match('/^([\\w_]+)\\s-\\s([\\w_]+)\\s*-*\\s*([\\w\\W]*)$/', $reference, $match); if (!file_exists(PATH_PLUGINS . $plugin . PATH_SEP . $xmlForm)) { $errorMsg .= 'file doesn\'t exist: ' . PATH_PLUGINS . $plugin . $xmlForm . "\n"; continue; } if (count($match) < 4) { $near = isset($rowTranslation['msgid']) ? $rowTranslation['msgid'] : (isset($rowTranslation['msgstr']) ? $rowTranslation['msgstr'] : ''); $errorMsg .= "Invalid Translation reference: \"{$reference}\", near -> " . strip_tags($near) . "\n"; continue; } G::LoadSystem('dynaformhandler'); $dynaform = new dynaFormHandler(PATH_PLUGINS . $plugin . PATH_SEP . $xmlForm); $fieldName = $match[2]; $codes = explode('-', $reference); if (sizeof($codes) == 2) { //is a normal node $dynaform->addChilds($fieldName, array($idLanguage => stripcslashes(str_replace(chr(10), '', $rowTranslation['msgstr'])))); } elseif (sizeof($codes) > 2) { //is a node child for a language node $name = $match[3] == "''" ? '' : $match[3]; $childNode = array(array('name' => 'option', 'value' => $rowTranslation['msgstr'], 'attributes' => array('name' => $name))); $dynaform->addChilds($fieldName, array($idLanguage => null), $childNode); } $countItemsSuccess++; } } $trn = new Translation(); $trn->generateFileTranslationPlugin($plugin, $idLanguage); $trn->addTranslationEnvironmentPlugins($plugin, $idLanguage, $POHeaders, $countItemsSuccess); $languageID = isset($languageID) ? $languageID : $idLanguage; //fill the results $results = new stdClass(); $results->recordsCount = $countItems; $results->recordsCountSuccess = $countItemsSuccess; $results->lang = $languageID; $results->headers = $POHeaders; $results->errMsg = $errorMsg; return $results; } catch (Exception $oError) { throw $oError; } }
public function import($sLanguageFile, $updateXml = true, $updateDB = true) { try { G::LoadSystem('i18n_po'); $POFile = new i18n_PO($sLanguageFile); $POFile->readInit(); $POHeaders = $POFile->getHeaders(); /*getting the PO Language definition*/ $langName = $POHeaders['X-Poedit-Language']; //find the lang id $language = new Language(); $langRecord = $language->findByLanName($langName); if (!isset($langRecord['LAN_ID'])) { //if the language doesn't exist abort throw new Exception('The .po file has a invalid X-Poedit-Language definition!'); } $languageID = $langRecord['LAN_ID']; /*getting the PO Language definition*/ $countryName = $POHeaders['X-Poedit-Country']; if ($countryName != '.') { $isoCountry = new IsoCountry(); $countryRecord = $isoCountry->findByIcName($countryName); if (!isset($countryRecord['IC_UID'])) { //if the language doesn't exist abort throw new Exception('The .po file has a invalid X-Poedit-Country definition!'); } $countryID = $countryRecord['IC_UID']; //define locale $LOCALE = "{$languageID}-{$countryID}"; } else { $LOCALE = $languageID; } $oTranslation = new Translation(); $countItems = 0; $countItemsSuccess = 0; $errorMsg = ''; while ($rowTranslation = $POFile->getTranslation()) { $countItems++; if (!isset($POFile->translatorComments[0]) || !isset($POFile->translatorComments[1]) || !isset($POFile->references[0])) { throw new Exception('The .po file doesn\'t have valid directives for Processmaker!'); } foreach ($POFile->translatorComments as $a => $aux) { $aux = trim($aux); if ($aux == 'TRANSLATION') { $identifier = $aux; } else { $var = explode('/', $aux); if ($var[0] == 'LABEL') { $context = $aux; } if ($var[0] == 'JAVASCRIPT') { $context = $aux; } } if (preg_match('/^([\\w-]+)\\/([\\w-]+\\/*[\\w-]*\\.xml\\?)/', $aux, $match)) { $identifier = $aux; } else { if (preg_match('/^([\\w-]+)\\/([\\w-]+\\/*[\\w-]*\\.xml$)/', $aux, $match)) { $context = $aux; } } } $reference = $POFile->references[0]; // it is a Sql insert on TRANSLATIONS TAble if ($identifier == 'TRANSLATION') { if ($updateDB) { list($category, $id) = explode('/', $context); $result = $oTranslation->addTranslation($category, $id, $LOCALE, trim(stripcslashes(str_replace(chr(10), '', $rowTranslation['msgstr'])))); if ($result['codError'] == 0) { $countItemsSuccess++; } else { $errorMsg .= $id . ': ' . $result['message'] . "\n"; } } } else { if ($updateXml) { $xmlForm = $context; //erik: expresion to prevent and hable correctly dropdown values like -1, -2 etc. preg_match('/^([\\w_]+)\\s-\\s([\\w_]+)\\s*-*\\s*([\\w\\W]*)$/', $reference, $match); if (!file_exists(PATH_XMLFORM . $xmlForm)) { $errorMsg .= 'file doesn\'t exist: ' . PATH_XMLFORM . $xmlForm . "\n"; continue; } if (count($match) < 4) { $near = isset($rowTranslation['msgid']) ? $rowTranslation['msgid'] : (isset($rowTranslation['msgstr']) ? $rowTranslation['msgstr'] : ''); $errorMsg .= "Invalid Translation reference: \"{$reference}\", near -> " . $near . "\n"; continue; } G::LoadSystem('dynaformhandler'); $dynaform = new dynaFormHandler(PATH_XMLFORM . $xmlForm); $fieldName = $match[2]; $codes = explode('-', $reference); if (sizeof($codes) == 2) { //is a normal node $dynaform->addChilds($fieldName, array($LOCALE => stripcslashes(str_replace(chr(10), '', $rowTranslation['msgstr'])))); } else { if (sizeof($codes) > 2) { //is a node child for a language node $name = $match[3] == "''" ? '' : $match[3]; $childNode = array(array('name' => 'option', 'value' => $rowTranslation['msgstr'], 'attributes' => array('name' => $name))); $dynaform->addChilds($fieldName, array($LOCALE => NULL), $childNode); } } $countItemsSuccess++; } } } $oLanguage = new Language(); $oLanguage->update(array('LAN_ID' => $languageID, 'LAN_ENABLED' => '1')); $trn = new Translation(); $trn->generateFileTranslation($LOCALE); $trn->addTranslationEnvironment($LOCALE, $POHeaders, $countItemsSuccess); $content = new Content(); $content->regenerateContent($languageID); //fill the results $results = new stdClass(); $results->recordsCount = $countItems; $results->recordsCountSuccess = $countItemsSuccess; $results->lang = $languageID; $results->headers = $POHeaders; $results->errMsg = $errorMsg; return $results; } catch (Exception $oError) { throw $oError; } }