Skip to content

Commit

Permalink
Debug v21 - Fix trans
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jan 12, 2025
1 parent 89e0630 commit e524939
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
4 changes: 4 additions & 0 deletions dev/tools/codespell/codespell-ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ espace
methode
datee

# Some string found because part of a text the is html entities escaped into file
tre
activ

# Translation keys
addin
amountin
Expand Down
18 changes: 9 additions & 9 deletions htdocs/core/class/translate.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,14 +682,6 @@ public function trans($key, $param1 = '', $param2 = '', $param3 = '', $param4 =
}
}

// We replace some HTML tags by __xx__ to avoid having them encoded by htmlentities because
// we want to keep '"' '<b>' '</b>' '<strong' '</strong>' '<a ' '</a>' '<br>' '< ' '<span' '</span>' that are reliable HTML tags inside translation strings.
$str = str_replace(
array('"', '<b>', '</b>', '<u>', '</u>', '<i', '</i>', '<center>', '</center>', '<strong>', '</strong>', '<a ', '</a>', '<br>', '<span', '</span>', '< ', '>'), // We accept '< ' but not '<'. We can accept however '>'
array('__quot__', '__tagb__', '__tagbend__', '__tagu__', '__taguend__', '__tagi__', '__tagiend__', '__tagcenter__', '__tagcenterend__', '__tagb__', '__tagbend__', '__taga__', '__tagaend__', '__tagbr__', '__tagspan__', '__tagspanend__', '__ltspace__', '__gt__'),
$str
);

if (strpos($key, 'Format') !== 0) {
try {
// @phan-suppress-next-line PhanPluginPrintfVariableFormatString
Expand All @@ -699,13 +691,21 @@ public function trans($key, $param1 = '', $param2 = '', $param3 = '', $param4 =
}
}

// We replace some HTML tags by __xx__ to avoid having them encoded by htmlentities because
// we want to keep '"' '<b>' '</b>' '<u>' '</u>' '<i>' '</i>' '<center> '</center>' '<strong' '</strong>' '<a ' '</a>' '<br>' '<span' '</span>' '< ' that are reliable HTML tags inside translation strings.
$str = str_replace(
array('"', '<b>', '</b>', '<u>', '</u>', '<i>', '</i>', '<center>', '</center>', '<strong>', '</strong>', '<a ', '</a>', '<br>', '<span', '</span>', '< ', '>'), // We accept '< ' but not '<'. We can accept however '>'
array('__quot__', '__tagb__', '__tagbend__', '__tagu__', '__taguend__', '__tagi__', '__tagiend__', '__tagcenter__', '__tagcenterend__', '__tagb__', '__tagbend__', '__taga__', '__tagaend__', '__tagbr__', '__tagspan__', '__tagspanend__', '__ltspace__', '__gt__'),
$str
);

// Encode string into HTML
$str = htmlentities($str, ENT_COMPAT, $this->charset_output); // Do not convert simple quotes in translation (strings in html are embraced by "). Use dol_escape_htmltag around text in HTML content

// Restore reliable HTML tags into original translation string
$str = str_replace(
array('__quot__', '__tagb__', '__tagbend__', '__tagu__', '__taguend__', '__tagi__', '__tagiend__', '__tagcenter__', '__tagcenterend__', '__taga__', '__tagaend__', '__tagbr__', '__tagspan__', '__tagspanend__', '__ltspace__', '__gt__'),
array('"', '<b>', '</b>', '<u>', '</u>', '<i', '</i>', '<center>', '</center>', '<a ', '</a>', '<br>', '<span', '</span>', '< ', '>'),
array('"', '<b>', '</b>', '<u>', '</u>', '<i>', '</i>', '<center>', '</center>', '<a ', '</a>', '<br>', '<span', '</span>', '< ', '>'),
$str
);

Expand Down
35 changes: 35 additions & 0 deletions test/phpunit/LangTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,48 @@ public function langDataProvider(): array
return $langCodes;
}

/**
* testLang
*
* @return void
*/
public function testTransWithHTMLInParam(): void
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;

include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';

$newlang = new Translate('', $conf);
$newlang->setDefaultLang('fr_FR');
$newlang->load("admin");

// ErrorModuleRequirePHPVersion is a string than contains accent é and <b>
// The ->transnoentities() does not escape nothing into entities.
$result = $newlang->transnoentities("ModuleMustBeEnabled", '<b>é</b><span class="red">aaa</span>');
print "result=".$result.PHP_EOL;
$this->assertEquals('Le module <b><b>é</b><span class="red">aaa</span></b> doit être activé', $result, 'Translation transnoentities ko');

// ErrorModuleRequirePHPVersion is a string than contains accent é and <b>
// The ->trans() escapes content into ModuleMustBeEnabled except b, strong, a, i, br and span tags,
// but content of parameters are escaped
$result = $newlang->trans("ModuleMustBeEnabled", '<b>é</b><span class="red">aaa</span>');
print "result=".$result.PHP_EOL;
$this->assertEquals('Le module <b><b>&eacute;</b><span class="red">aaa</span></b> doit &ecirc;tre activ&eacute;', $result, 'Translation trans ko');

return;
}

/**
* testLang
* @dataProvider langDataProvider
*
* @param string $code Language code for which to verify translations
* @return void
* @depends testTransWithHTMLInParam
*/
public function testLang($code): void
{
Expand Down

0 comments on commit e524939

Please sign in to comment.