NEW Editing translation GUI become easier with search translation tool.

This commit is contained in:
Laurent Destailleur 2016-09-04 14:45:43 +02:00
parent e4539be127
commit d24045d831
5 changed files with 171 additions and 80 deletions

View File

@ -23,6 +23,7 @@
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
$langs->load("companies");
@ -36,10 +37,12 @@ if (!$user->admin) accessforbidden();
$id=GETPOST('rowid','int');
$action=GETPOST('action','alpha');
$langcode=GETPOST('langcode','alpha');
$transkey=GETPOST('transkey','alpha');
$transvalue=GETPOST('transvalue','alpha');
$mode = GETPOST('mode')?GETPOST('mode'):'overwrite';
$limit = GETPOST("limit")?GETPOST("limit","int"):$conf->liste_limit;
@ -131,82 +134,44 @@ print load_fiche_titre($langs->trans("Translation"),'','title_setup');
print $langs->trans("TranslationDesc")."<br>\n";
print "<br>\n";
print $langs->trans("CurrentUserLanguage").': <strong>'.$langs->defaultlang.'</strong><br>';
$current_language_code=$langs->defaultlang;
$s=picto_from_langcode($current_language_code);
print $langs->trans("CurrentUserLanguage").': <strong>'.$s.' '.$current_language_code.'</strong><br>';
print '<br>';
print img_info().' '.$langs->trans("SomeTranslationAreUncomplete");
$urlwikitranslatordoc='https://wiki.dolibarr.org/index.php/Translator_documentation';
print ' ('.$langs->trans("SeeAlso").': <a href="'.$urlwikitranslatordoc.'" target="_blank">'.$urlwikitranslatordoc.'</a>)<br>';
print $langs->trans("TranslationOverwriteDesc",$langs->transnoentitiesnoconv("Language"),$langs->transnoentitiesnoconv("Key"),$langs->transnoentitiesnoconv("NewTranslationStringToShow"))."<br>\n";
print '<br>';
$param='mode='.$mode;
$param='&mode='.$mode;
if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
if ($optioncss != '') $param.='&optioncss='.$optioncss;
if ($langcode) $param.='&langcode='.urlencode($langcode);
if ($transkey) $param.='&transkey='.urlencode($transkey);
if ($transvalue) $param.='&transvalue='.urlencode($transvalue);
print '<form action="'.$_SERVER["PHP_SELF"].((empty($user->entity) && $debug)?'?debug=1':'').'" method="POST">';
if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
print '<input type="hidden" name="action" value="list">';
print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
$head=translation_prepare_head();
dol_fiche_head($head, $mode, '', 0, '');
if ($mode == 'searchkey')
{
//print '<br>';
//print load_fiche_titre($langs->trans("TranslationKeySearch"), '', '')."\n";
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<input type="hidden" id="action" name="action" value="search">';
print '<input type="hidden" id="mode" name="mode" value="'.$mode.'">';
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Language").' (en_US, es_MX, ...)</td>';
print '<td>'.$langs->trans("Key").'</td>';
print '<td>'.$langs->trans("TranslationString").'</td>';
if (! empty($conf->multicompany->enabled) && !$user->entity) print '<td>'.$langs->trans("Entity").'</td>';
print '<td align="center"></td>';
print "</tr>\n";
// Line to search new record
$var=false;
print "\n";
print '<tr '.$bc[$var].'><td>';
print $formadmin->select_language(GETPOST('langcodesearch'),'langcodesearch',0,null,$langs->trans("All"),0,0,'',1);
//print '<input type="text" class="flat" size="24" name="langcode" value="'.GETPOST('langcode').'">';
print '</td>'."\n";
print '<td>';
print '<input type="text" class="flat maxwidthonsmartphone" name="transkeysearch" value="">';
print '</td><td>';
print '<input type="text" class="quatrevingtpercent" name="transvaluesearch" value="">';
print '</td>';
// Limit to superadmin
if (! empty($conf->multicompany->enabled) && !$user->entity)
{
print '<td>';
print '<input type="text" class="flat" size="1" name="entitysearch" value="'.$conf->entity.'">';
print '</td>';
print '<td align="center">';
}
else
{
print '<td align="center">';
print '<input type="hidden" name="entitysearch" value="'.$conf->entity.'">';
}
print '<input type="submit" class="button" value="'.$langs->trans("Search").'" name="search">';
print "</td>\n";
print '</tr>';
print '</table>';
print '</form>';
}
if ($mode == 'overwrite')
{
//print load_fiche_titre($langs->trans("TranslationOverwriteKey"), '', '')."\n";
print img_info().' '.$langs->trans("SomeTranslationAreUncomplete");
$urlwikitranslatordoc='https://wiki.dolibarr.org/index.php/Translator_documentation';
print ' ('.$langs->trans("SeeAlso").': <a href="'.$urlwikitranslatordoc.'" target="_blank">'.$urlwikitranslatordoc.'</a>)<br>';
print $langs->trans("TranslationOverwriteDesc",$langs->transnoentitiesnoconv("Language"),$langs->transnoentitiesnoconv("Key"),$langs->transnoentitiesnoconv("NewTranslationStringToShow"))."\n";
print ' ('.$langs->trans("TranslationOverwriteDesc2").').'."<br>\n";
print '<br>';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<input type="hidden" id="action" name="action" value="">';
@ -227,8 +192,7 @@ if ($mode == 'overwrite')
print "\n";
print '<tr '.$bc[$var].'><td>';
print $formadmin->select_language(GETPOST('langcode'),'langcode',0,null,1,0,0,'',1);
//print '<input type="text" class="flat" size="24" name="langcode" value="'.GETPOST('langcode').'">';
print $formadmin->select_language(GETPOST('langcode'), 'langcode', 0, null, 1, 0, 0, 'maxwidthonsmartphone', 1);
print '</td>'."\n";
print '<td>';
print '<input type="text" class="flat maxwidthonsmartphone" name="transkey" value="">';
@ -313,6 +277,128 @@ if ($mode == 'overwrite')
}
if ($mode == 'searchkey')
{
$recordtoshow=array();
$nbempty=0;
/*var_dump($langcode);
var_dump($transkey);
var_dump($transvalue);*/
if (empty($langcode) || $langcode == '-1') $nbempty++;
if (empty($transkey)) $nbempty++;
if (empty($transvalue)) $nbempty++;
if ($action == 'search' && ($nbempty > 1))
{
setEventMessages($langs->trans("WarningAtLeastKeyOrTranslationRequired"), null, 'warnings');
}
else
{
$newlang=new Translate('',$conf);
$newlang->setDefaultLang($langcode);
// Load all translations keys
foreach($conf->file->dol_document_root as $keydir => $searchdir)
{
// Directory of translation files
$dir_lang = $searchdir."/langs/".$langcode;
$dir_lang_osencoded=dol_osencode($dir_lang);
$filearray=dol_dir_list($dir_lang_osencoded,'files',0,'','',$sortfield,(strtolower($sortorder)=='asc'?SORT_ASC:SORT_DESC),1);
foreach($filearray as $file)
{
$tmpfile=preg_replace('/.lang/i', '', basename($file['name']));
$newlang->load($tmpfile, 0, 0, '', 0);
//print 'After loading lang '.$tmpfile.', newlang has '.count($newlang->tab_translate).' records<br>'."\n";
}
}
// Now search into translation array
foreach($newlang->tab_translate as $key => $val)
{
if ($transkey && ! preg_match('/'.preg_quote($transkey).'/', $key)) continue;
if ($transvalue && ! preg_match('/'.preg_quote($transvalue).'/', $val)) continue;
$recordtoshow[$key]=$val;
}
}
//print '<br>';
$nbtotalofrecordswithoutfilters = count($newlang->tab_translate);
$nbtotalofrecords = count($recordtoshow);
$num = $limit + 1;
if (($offset + $num) > $nbtotalofrecords) $num = $limit;
//print 'param='.$param.' $_SERVER["PHP_SELF"]='.$_SERVER["PHP_SELF"].' num='.$num.' page='.$page.' nbtotalofrecords='.$nbtotalofrecords." sortfield=".$sortfield." sortorder=".$sortorder;
$title = $langs->trans("TranslationKeySearch");
if ($nbtotalofrecords > 0) $title.=' ('.$nbtotalofrecords.' / '.$nbtotalofrecordswithoutfilters.')';
print print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, -1 * $nbtotalofrecords, '', 0, '', '', $limit)."\n";
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<input type="hidden" id="action" name="action" value="search">';
print '<input type="hidden" id="mode" name="mode" value="'.$mode.'">';
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print_liste_field_titre($langs->trans("Language").' (en_US, es_MX, ...)',$_SERVER["PHP_SELF"],'lang,transkey','',$param,'',$sortfield,$sortorder).'</td>';
print_liste_field_titre($langs->trans("Key"),$_SERVER["PHP_SELF"],'transkey','',$param,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CurrentTranslationString"),$_SERVER["PHP_SELF"],'transvalue','',$param,'',$sortfield,$sortorder);
if (! empty($conf->multicompany->enabled) && !$user->entity) print_liste_field_titre($langs->trans("Entity"),$_SERVER["PHP_SELF"],'entity,transkey','',$param,'',$sortfield,$sortorder);
print '<td align="center"></td>';
print "</tr>\n";
// Line to search new record
$var=false;
print "\n";
print '<tr '.$bc[$var].'><td>';
$langcode=GETPOST('langcode')?GETPOST('langcode'):$langs->defaultlang;
//print $formadmin->select_language($langcode,'langcode',0,null,$langs->trans("All"),0,0,'',1);
print $formadmin->select_language($langcode,'langcode', 0, null, 0, 0, 0, 'maxwidthonsmartphone', 1);
print '</td>'."\n";
print '<td>';
print '<input type="text" class="flat maxwidthonsmartphone" name="transkey" value="'.$transkey.'">';
print '</td><td>';
print '<input type="text" class="quatrevingtpercent" name="transvalue" value="'.$transvalue.'">';
print '</td>';
// Limit to superadmin
if (! empty($conf->multicompany->enabled) && !$user->entity)
{
print '<td>';
print '<input type="text" class="flat" size="1" name="entitysearch" value="'.$conf->entity.'">';
print '</td>';
print '<td align="center">';
}
else
{
print '<td align="center">';
print '<input type="hidden" name="entitysearch" value="'.$conf->entity.'">';
}
print '<input type="submit" class="button" value="'.$langs->trans("Search").'" name="search">';
print "</td>\n";
print '</tr>';
if ($sortfield == 'transkey' && strtolower($sortorder) == 'asc') ksort($recordtoshow);
if ($sortfield == 'transkey' && strtolower($sortorder) == 'desc') krsort($recordtoshow);
if ($sortfield == 'transvalue' && strtolower($sortorder) == 'asc') asort($recordtoshow);
if ($sortfield == 'transvalue' && strtolower($sortorder) == 'desc') arsort($recordtoshow);
// Show result
$i=0;
foreach($recordtoshow as $key => $val)
{
$i++;
if ($i <= $offset) continue;
if ($i > ($offset + $limit)) break;
print '<tr><td>'.$langcode.'</td><td>'.$key.'</td><td colspan="2">';
print dol_escape_htmltag($val);
print '</td></tr>'."\n";
}
print '</table>';
print '</form>';
}
dol_fiche_end();
print "</form>\n";

View File

@ -58,7 +58,7 @@ class FormAdmin
* @param int $showcode Add language code into label
* @return string Return HTML select string with list of languages
*/
function select_language($selected='',$htmlname='lang_id',$showauto=0,$filter=null,$showempty='',$showwarning=0,$disabled=0,$morecss='',$showcode=0)
function select_language($selected='', $htmlname='lang_id', $showauto=0, $filter=null, $showempty='', $showwarning=0, $disabled=0, $morecss='', $showcode=0)
{
global $langs;

View File

@ -155,14 +155,15 @@ class Translate
* @param integer $alt 0 (try xx_ZZ then 1), 1 (try xx_XX then 2), 2 (try en_US)
* @param int $stopafterdirection Stop when the DIRECTION tag is found (optimize speed)
* @param int $forcelangdir To force a different lang directory
* @param int $loadfromfileonly 1=Do not load overwritten translation from file or old conf.
* @return int <0 if KO, 0 if already loaded or loading not required, >0 if OK
*/
function load($domain,$alt=0,$stopafterdirection=0,$forcelangdir='')
function load($domain,$alt=0,$stopafterdirection=0,$forcelangdir='',$loadfromfileonly=0)
{
global $conf,$db;
// Load $this->tab_translate[] from database
if (count($this->tab_translate) == 0) $this->loadFromDatabase($db); // Nothing was loaded yet, so we load database.
if (empty($loadfromfileonly) && count($this->tab_translate) == 0) $this->loadFromDatabase($db); // Nothing was loaded yet, so we load database.
// Check parameters
if (empty($domain))
@ -336,19 +337,20 @@ class Translate
// This part is deprecated and replaced with table llx_overwrite_trans
// Kept for backward compatibility.
$overwritekey='MAIN_OVERWRITE_TRANS_'.$this->defaultlang;
if (! empty($conf->global->$overwritekey)) // Overwrite translation with key1:newstring1,key2:newstring2
{
// Overwrite translation with param MAIN_OVERWRITE_TRANS_xx_XX
$tmparray=explode(',', $conf->global->$overwritekey);
foreach($tmparray as $tmp)
if (empty($loadfromfileonly))
{
$overwritekey='MAIN_OVERWRITE_TRANS_'.$this->defaultlang;
if (! empty($conf->global->$overwritekey)) // Overwrite translation with key1:newstring1,key2:newstring2
{
$tmparray2=explode(':',$tmp);
if (! empty($tmparray2[1])) $this->tab_translate[$tmparray2[0]]=$tmparray2[1];
// Overwrite translation with param MAIN_OVERWRITE_TRANS_xx_XX
$tmparray=explode(',', $conf->global->$overwritekey);
foreach($tmparray as $tmp)
{
$tmparray2=explode(':',$tmp);
if (! empty($tmparray2[1])) $this->tab_translate[$tmparray2[0]]=$tmparray2[1];
}
}
}
}
// Check to be sure that SeparatorDecimal differs from SeparatorThousand
if (! empty($this->tab_translate["SeparatorDecimal"]) && ! empty($this->tab_translate["SeparatorThousand"])

View File

@ -3041,7 +3041,7 @@ function load_fiche_titre($titre, $mesg='', $picto='title_generic.png', $pictois
* @param string $sortorder Order to sort ('' by default)
* @param string $center Strin gin the middle ('' by default). We often find here string $massaction comming from $form->selectMassAction()
* @param int $num Number of records found by select with limit+1
* @param int $totalnboflines Total number of records/lines for all pages (if known). Use a negative value to no show number.
* @param int $totalnboflines Total number of records/lines for all pages (if known). Use a negative value to not show number.
* @param string $picto Icon to use before title (should be a 32x32 transparent png file)
* @param int $pictoisfullpath 1=Icon name is a full absolute url of image
* @param string $morehtml More html to show

View File

@ -1041,8 +1041,11 @@ TranslationSetup=Setup of translation
TranslationKeySearch=Search a translation key or string
TranslationOverwriteKey=Overwrite a translation string
TranslationDesc=How to set displayed application language :<br>* Systemwide: menu <strong>Home - Setup - Display</strong><br>* Per user: <strong>User display setup</strong> tab of user card (click on username at the top of the screen).
TranslationOverwriteDesc=You can also override strings filling the following table. Choose your language from "%s" dropdown, insert the key string found in the lang file (langs/xx_XX/somefile.lang) into "%s" and your new translation into "%s".
TranslationOverwriteDesc=You can also override strings filling the following table. Choose your language from "%s" dropdown, insert the translation key string into "%s" and your new translation into "%s"
TranslationOverwriteDesc2=You can use the other tab to help you know translation key to use
TranslationString=Translation string
CurrentTranslationString=Current translation string
WarningAtLeastKeyOrTranslationRequired=A search criteria is required at least for key or translation string
NewTranslationStringToShow=New translation string to show
TotalNumberOfActivatedModules=Total number of activated feature modules: <b>%s</b> / <b>%s</b>
YouMustEnableOneModule=You must at least enable 1 module