Templating tr function: provided message could now be formated as LSformat string using extra provided parameters

Example: {tr msg="Hello %(who)!" who="World"}
This commit is contained in:
Benjamin Renard 2023-05-26 09:46:51 +02:00
parent 087b7e3065
commit 65d1d50c3a
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
2 changed files with 14 additions and 11 deletions

View file

@ -499,14 +499,13 @@ function _cli_add_possible_values_from_LSconfig($context, $withouts, $level=0) {
function _cli_parse_template_file($file) {
global $LSlang_cli_logger;
$LSlang_cli_logger -> debug("Looking for string to translate in '$file' template file");
$count = 0;
foreach(file($file) as $line) {
$count ++;
if (preg_match_all('/\{ *tr +msg=["\']([^\}]+)["\'] *\}/',$line,$matches)) {
foreach($matches[1] as $t) {
foreach(file($file) as $line_count => $line) {
$line_count ++;
if (preg_match_all('/\{ *tr +.*msg=(["\'])(?P<msg>[^"}]+)\1.*\}/', $line, $matches)) {
foreach($matches["msg"] as $t) {
$t = preg_replace('/[\'"]\|escape\:.*$/', '', $t);
$LSlang_cli_logger -> trace(" - \"$t\" # Line $count");
_cli_add_str_to_translate($t, _cli_absolute2relative_path($file).":$count");
$LSlang_cli_logger -> trace(" - \"$t\" # Line $line_count");
_cli_add_str_to_translate($t, _cli_absolute2relative_path($file).":$line_count");
}
}
}
@ -520,15 +519,17 @@ function _cli_parse_template_file($file) {
* @return void
*/
function _cli_find_and_parse_template_file($dir) {
global $LSlang_cli_logger;
if (is_dir($dir)) {
$LSlang_cli_logger -> debug("Looking for string to translate in template directory '$dir'");
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file=='.' || $file=='..') continue;
if (is_dir($dir.'/'.$file)) {
_cli_find_and_parse_template_file($dir.'/'.$file);
if (is_dir("$dir/$file")) {
_cli_find_and_parse_template_file("$dir/$file");
}
elseif (is_file($dir."/".$file) && preg_match('/\.tpl$/',$file)) {
_cli_parse_template_file($dir.'/'.$file);
_cli_parse_template_file("$dir/$file");
}
}
closedir($dh);

View file

@ -705,7 +705,9 @@ function LStemplate_smarty_getFData($params) {
}
function LStemplate_smarty_tr($params) {
echo __($params['msg']);
$msg = __($params['msg']);
unset($params['msg']);
echo $params?getFData($msg, $params):$msg;
}
function LStemplate_smarty_img($params) {