Translations should be available for PMDynaform
This commit is contained in:
dheeyi william
2017-10-17 16:18:26 -04:00
parent 2417f5f15d
commit 2ce253b67f
2 changed files with 126 additions and 96 deletions

View File

@@ -121,34 +121,31 @@ class Language extends BaseLanguage
} }
} }
/* /**
* Import a language file * Import a language file
* *
* @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail> * @param string $languageFile
* @param string $sLanguageFile * @param bool $updateXml
* @param string $bXml * @param bool $updateDB
* @return void * @param bool $generateMafe
* @return Object
* @throws Exception
*/ */
public function import ($sLanguageFile, $updateXml = true, $updateDB = true, $generateMafe = true) public function import($languageFile, $updateXml = true, $updateDB = true, $generateMafe = true)
{ {
try { try {
$translation = new Translation();
//get labels MichelangeloFE
try { try {
$oTranslation = new Translation(); //We get all MichelangeloFE and PMDynaform translatable labels.
$MichelangeloFE = PATH_HOME . "../workflow/public_html/lib/js"; $labels = array_merge(self::getLabelsMafe(), self::getLabelsPMDynaform());
if (file_exists($MichelangeloFE)) { foreach ($labels as $label) {
$labels = self::readLabelsDirectory($MichelangeloFE, true); $translation->addTranslation('LABEL', 'ID_MAFE_' . G::encryptOld($label), 'en', $label);
foreach ($labels as $label) {
$oTranslation->addTranslation('LABEL', 'ID_MAFE_' . G::encryptOld($label), 'en', $label);
}
} }
} catch (Exception $e) { } catch (Exception $e) {
error_log($e->getMessage()); error_log($e->getMessage());
} }
$POFile = new i18n_PO($languageFile);
$POFile = new i18n_PO( $sLanguageFile );
$POFile->readInit(); $POFile->readInit();
$POHeaders = $POFile->getHeaders(); $POHeaders = $POFile->getHeaders();
@@ -156,13 +153,13 @@ class Language extends BaseLanguage
$langName = $POHeaders['X-Poedit-Language']; $langName = $POHeaders['X-Poedit-Language'];
//find the lang id //find the lang id
$language = new Language(); $language = new Language();
$langRecord = $language->findByLanName( $langName ); $langRecord = $language->findByLanName($langName);
if (! isset( $langRecord['LAN_ID'] )) { if (!isset($langRecord['LAN_ID'])) {
$langRecord = $language->findById( $langName ); $langRecord = $language->findById($langName);
if (! isset( $langRecord['LAN_ID'] )) { if (!isset($langRecord['LAN_ID'])) {
//if the language doesn't exist abort //if the language doesn't exist abort
throw new Exception( 'The .po file has a invalid X-Poedit-Language definition!' ); throw new Exception('The .po file has a invalid X-Poedit-Language definition!');
} }
} }
@@ -172,11 +169,11 @@ class Language extends BaseLanguage
$countryName = $POHeaders['X-Poedit-Country']; $countryName = $POHeaders['X-Poedit-Country'];
if ($countryName != '.') { if ($countryName != '.') {
$isoCountry = new IsoCountry(); $isoCountry = new IsoCountry();
$countryRecord = $isoCountry->findByIcName( $countryName ); $countryRecord = $isoCountry->findByIcName($countryName);
if (! isset( $countryRecord['IC_UID'] )) { if (!isset($countryRecord['IC_UID'])) {
//if the language doesn't exist abort //if the language doesn't exist abort
throw new Exception( 'The .po file has a invalid X-Poedit-Country definition!' ); throw new Exception('The .po file has a invalid X-Poedit-Country definition!');
} }
$countryID = $countryRecord['IC_UID']; $countryID = $countryRecord['IC_UID'];
@@ -186,23 +183,22 @@ class Language extends BaseLanguage
$LOCALE = $languageID; $LOCALE = $languageID;
} }
$oTranslation = new Translation();
$countItems = 0; $countItems = 0;
$countItemsSuccess = 0; $countItemsSuccess = 0;
$errorMsg = ''; $errorMsg = '';
while ($rowTranslation = $POFile->getTranslation()) { while ($rowTranslation = $POFile->getTranslation()) {
$countItems ++; $countItems++;
if (! isset( $POFile->translatorComments[0] ) || ! isset( $POFile->translatorComments[1] ) || ! isset( $POFile->references[0] )) { 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!' ); throw new Exception('The .po file doesn\'t have valid directives for Processmaker!');
} }
foreach ($POFile->translatorComments as $a => $aux) { foreach ($POFile->translatorComments as $a => $aux) {
$aux = trim( $aux ); $aux = trim($aux);
if ($aux == 'TRANSLATION') { if ($aux == 'TRANSLATION') {
$identifier = $aux; $identifier = $aux;
} else { } else {
$var = explode( '/', $aux ); $var = explode('/', $aux);
if ($var[0] == 'LABEL') { if ($var[0] == 'LABEL') {
$context = $aux; $context = $aux;
} }
@@ -210,10 +206,10 @@ class Language extends BaseLanguage
$context = $aux; $context = $aux;
} }
} }
if (preg_match( '/^([\w-]+)\/([\w-]+\/*[\w-]*\.xml\?)/', $aux, $match )) { if (preg_match('/^([\w-]+)\/([\w-]+\/*[\w-]*\.xml\?)/', $aux, $match)) {
$identifier = $aux; $identifier = $aux;
} else { } else {
if (preg_match( '/^([\w-]+)\/([\w-]+\/*[\w-]*\.xml$)/', $aux, $match )) { if (preg_match('/^([\w-]+)\/([\w-]+\/*[\w-]*\.xml$)/', $aux, $match)) {
$context = $aux; $context = $aux;
} }
} }
@@ -224,10 +220,10 @@ class Language extends BaseLanguage
// it is a Sql insert on TRANSLATIONS TAble // it is a Sql insert on TRANSLATIONS TAble
if ($identifier == 'TRANSLATION') { if ($identifier == 'TRANSLATION') {
if ($updateDB) { if ($updateDB) {
list ($category, $id) = explode( '/', $context ); list ($category, $id) = explode('/', $context);
$result = $oTranslation->addTranslation( $category, $id, $LOCALE, trim( stripcslashes( str_replace( chr( 10 ), '', $rowTranslation['msgstr'] ) ) ) ); $result = $translation->addTranslation($category, $id, $LOCALE, trim(stripcslashes(str_replace(chr(10), '', $rowTranslation['msgstr']))));
if ($result['codError'] == 0) { if ($result['codError'] == 0) {
$countItemsSuccess ++; $countItemsSuccess++;
} else { } else {
$errorMsg .= $id . ': ' . $result['message'] . "\n"; $errorMsg .= $id . ': ' . $result['message'] . "\n";
} }
@@ -236,52 +232,52 @@ class Language extends BaseLanguage
elseif ($updateXml) { elseif ($updateXml) {
$xmlForm = $context; $xmlForm = $context;
//erik: expresion to prevent and hable correctly dropdown values like -1, -2 etc. //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 ); preg_match('/^([\w_]+)\s-\s([\w_]+)\s*-*\s*([\w\W]*)$/', $reference, $match);
if (! file_exists( PATH_XMLFORM . $xmlForm )) { if (!file_exists(PATH_XMLFORM . $xmlForm)) {
$errorMsg .= 'file doesn\'t exist: ' . PATH_XMLFORM . $xmlForm . "\n"; $errorMsg .= 'file doesn\'t exist: ' . PATH_XMLFORM . $xmlForm . "\n";
continue; continue;
} }
if (count( $match ) < 4) { if (count($match) < 4) {
$near = isset( $rowTranslation['msgid'] ) ? $rowTranslation['msgid'] : (isset( $rowTranslation['msgstr'] ) ? $rowTranslation['msgstr'] : ''); $near = isset($rowTranslation['msgid']) ? $rowTranslation['msgid'] : (isset($rowTranslation['msgstr']) ? $rowTranslation['msgstr'] : '');
$errorMsg .= "Invalid Translation reference: \"$reference\", near -> " . strip_tags($near) . "\n"; $errorMsg .= "Invalid Translation reference: \"$reference\", near -> " . strip_tags($near) . "\n";
continue; continue;
} }
$dynaform = new DynaformHandler( PATH_XMLFORM . $xmlForm ); $dynaform = new DynaformHandler(PATH_XMLFORM . $xmlForm);
$fieldName = $match[2]; $fieldName = $match[2];
$codes = explode( '-', $reference ); $codes = explode('-', $reference);
if (sizeof( $codes ) == 2) { if (sizeof($codes) == 2) {
//is a normal node //is a normal node
$dynaform->addChilds( $fieldName, Array ($LOCALE => stripcslashes( str_replace( chr( 10 ), '', $rowTranslation['msgstr'] ) ) $dynaform->addChilds($fieldName, Array($LOCALE => stripcslashes(str_replace(chr(10), '', $rowTranslation['msgstr']))
) ); ));
} elseif (sizeof( $codes ) > 2) { } elseif (sizeof($codes) > 2) {
//is a node child for a language node //is a node child for a language node
$name = $match[3] == "''" ? '' : $match[3]; $name = $match[3] == "''" ? '' : $match[3];
$childNode = Array (Array ('name' => 'option','value' => $rowTranslation['msgstr'],'attributes' => Array ('name' => $name $childNode = Array(Array('name' => 'option', 'value' => $rowTranslation['msgstr'], 'attributes' => Array('name' => $name
) )
) )
); );
$dynaform->addChilds( $fieldName, Array ($LOCALE => null $dynaform->addChilds($fieldName, Array($LOCALE => null
), $childNode ); ), $childNode);
} }
$countItemsSuccess ++; $countItemsSuccess++;
} }
} }
$oLanguage = new Language(); $language = new Language();
$oLanguage->update( array ('LAN_ID' => $languageID,'LAN_ENABLED' => '1' $language->update(array('LAN_ID' => $languageID, 'LAN_ENABLED' => '1'
) ); ));
if ($updateXml) { if ($updateXml) {
$trn = new Translation(); $trn = new Translation();
$trn->generateFileTranslation( $LOCALE ); $trn->generateFileTranslation($LOCALE);
$trn->addTranslationEnvironment( $LOCALE, $POHeaders, $countItemsSuccess ); $trn->addTranslationEnvironment($LOCALE, $POHeaders, $countItemsSuccess);
} }
if ($generateMafe) { if ($generateMafe) {
@@ -297,26 +293,26 @@ class Language extends BaseLanguage
$results->headers = $POHeaders; $results->headers = $POHeaders;
$results->errMsg = $errorMsg; $results->errMsg = $errorMsg;
G::auditLog("UploadLanguage", "Language: ".$languageID); G::auditLog("UploadLanguage", "Language: " . $languageID);
return $results; return $results;
} catch (Exception $oError) { } catch (Exception $error) {
throw ($oError); throw ($error);
} }
} }
//export /**
* Export language to Download
* @throws Exception
*/
public function export () public function export ()
{ {
//get labels MichelangeloFE $translation = new Translation();
try { try {
$oTranslation = new Translation(); //We get all MichelangeloFE and PMDynaform translatable labels.
$MichelangeloFE = PATH_HOME . "../workflow/public_html/lib/js"; $labels = array_merge(self::getLabelsMafe(), self::getLabelsPMDynaform());
if (file_exists($MichelangeloFE)) { foreach ($labels as $label) {
$labels = self::readLabelsDirectory($MichelangeloFE, true); $translation->addTranslation('LABEL', 'ID_MAFE_' . G::encryptOld($label), 'en', $label);
foreach ($labels as $label) {
$oTranslation->addTranslation('LABEL', 'ID_MAFE_' . G::encryptOld($label), 'en', $label);
}
} }
} catch (Exception $e) { } catch (Exception $e) {
error_log($e->getMessage()); error_log($e->getMessage());
@@ -412,8 +408,7 @@ class Language extends BaseLanguage
// IF the translations id "TRN_ID" has invalid characteres or has not accepted categories // IF the translations id "TRN_ID" has invalid characteres or has not accepted categories
if ($sTestResult[0] !== $trnCategory || ($trnCategory != 'LABEL' && $trnCategory != 'JAVASCRIPT')) { if ($sTestResult[0] !== $trnCategory || ($trnCategory != 'LABEL' && $trnCategory != 'JAVASCRIPT')) {
$oTranslation = new Translation(); $translation->remove( $aRow1['TRN_CATEGORY'], $aRow1['TRN_ID'], 'en' ); //remove not accepted translations
$oTranslation->remove( $aRow1['TRN_CATEGORY'], $aRow1['TRN_ID'], 'en' ); //remove not accepted translations
continue; //jump to next iteration continue; //jump to next iteration
} }
@@ -561,6 +556,67 @@ class Language extends BaseLanguage
G::auditLog("ExportLanguage", "Language: ".$_GET['LOCALE']); G::auditLog("ExportLanguage", "Language: ".$_GET['LOCALE']);
G::streamFile( $sPOFile, true ); G::streamFile( $sPOFile, true );
} }
/**
* Get Labels MAFE (Michelangelo Project)
* @return array
*/
public static function getLabelsMafe()
{
$labels = [];
$buildhash = PATH_HTML . "lib/buildhash";
if (!file_exists($buildhash)) {
throw new RuntimeException("Unable to generate labels for MAFE!.\nMissing file '{$buildhash}'.");
}
$buildhash = file_get_contents($buildhash);
$michelangeloFE = PATH_HTML . "lib/js";
$pathFileMafe = array_pop(glob($michelangeloFE . '/' . '*' . $buildhash . '*', GLOB_BRACE));
if (file_exists($pathFileMafe) && is_readable($pathFileMafe)) {
$labels = self::readLabelsDirectory($pathFileMafe, true);
}
return $labels;
}
/**
* Get Labels PMDynaform (PMDynaform Project)
* @return array
*/
public static function getLabelsPMDynaform()
{
$labels = [];
$pathFilePMDynaform = PATH_HTML . "lib/pmdynaform/build/js/PMDynaform.js";
if (file_exists($pathFilePMDynaform) && is_readable($pathFilePMDynaform)) {
$labels = self::readLabelsDirectory($pathFilePMDynaform, true);
}
return $labels;
}
/**
* We read all labels
* @param $path
* @param bool $unique
* @return array
*/
public static function readLabelsDirectory($path, $unique = false)
{
$labels = [];
if (is_file($path)) {
$info = pathinfo($path);
if (strtolower($info["extension"]) === "js") {
$file = file_get_contents($path);
//search string 'xx\'xx\'xx'.translate()
$labels = array_merge($labels, self::readLabelsFile($file, "'"));
//search string "xx\"xx\"xx".translate()
$labels = array_merge($labels, self::readLabelsFile($file, "\""));
}
}
if ($unique) {
$labels = array_unique($labels);
}
return $labels;
}
public function updateLanguagePlugin ($plugin, $idLanguage) public function updateLanguagePlugin ($plugin, $idLanguage)
{ {
if (!file_exists(PATH_PLUGINS . $plugin)) { if (!file_exists(PATH_PLUGINS . $plugin)) {
@@ -835,33 +891,6 @@ class Language extends BaseLanguage
} //end foreach } //end foreach
} }
} }
public static function readLabelsDirectory($path, $unique = false)
{
$labels = array();
$items = opendir($path);
while (false !== ($item = readdir($items))) {
$a = $path . "/" . $item;
if ($item !== "." && $item !== ".." && is_dir($a)) {
$labels = array_merge($labels, self::readLabelsDirectory($a, false));
}
if (is_file($a)) {
$info = pathinfo($a);
if ($info["extension"] === "js" || $info["extension"] === "JS") {
$file = file_get_contents($a);
//search string 'xx\'xx\'xx'.translate()
$labels = array_merge($labels, self::readLabelsFile($file, "'"));
//search string "xx\"xx\"xx".translate()
$labels = array_merge($labels, self::readLabelsFile($file, "\""));
}
}
}
if ($unique) {
$labels = array_unique($labels);
}
closedir($items);
return $labels;
}
public static function readLabelsFile($file, $sep) public static function readLabelsFile($file, $sep)
{ {

View File

@@ -12,6 +12,7 @@
<link rel="stylesheet" href="/css/{sys_skin}-pm3.css"> <link rel="stylesheet" href="/css/{sys_skin}-pm3.css">
<link rel="stylesheet" href="/lib/pmdynaform/libs/font-awesome-4.6.3/css/font-awesome.min.css"> <link rel="stylesheet" href="/lib/pmdynaform/libs/font-awesome-4.6.3/css/font-awesome.min.css">
<script type="text/javascript" src="/translations/translationsMafe.js"></script>
<script type="text/javascript" src="/lib/pmdynaform/libs/moment/moment.min.js"></script> <script type="text/javascript" src="/lib/pmdynaform/libs/moment/moment.min.js"></script>
<script type="text/javascript" src="/lib/pmdynaform/libs/jquery/jquery-1.11.js"></script> <script type="text/javascript" src="/lib/pmdynaform/libs/jquery/jquery-1.11.js"></script>
<script type="text/javascript" src="/lib/pmdynaform/libs/bootstrap-3.1.1/js/bootstrap.min.js"></script> <script type="text/javascript" src="/lib/pmdynaform/libs/bootstrap-3.1.1/js/bootstrap.min.js"></script>