Compare commits

..

3 commits

Author SHA1 Message Date
Benjamin Renard
823006937a
generate_lang_file: ignore LSformat string with simple string substitution 2023-05-17 17:59:58 +02:00
Benjamin Renard
7b7ff7e629
getFData() & getFieldInFormat(): code cleaning 2023-05-17 17:11:50 +02:00
Benjamin Renard
2d108c8b08
Allow translation of LSioFormat labels 2023-05-17 16:26:35 +02:00
4 changed files with 132 additions and 107 deletions

View file

@ -377,6 +377,15 @@ function _cli_add_str_to_translate($msg, $context=null) {
if (!is_null($lang) && _($msg) != "$msg") if (!is_null($lang) && _($msg) != "$msg")
return; return;
// It is a LSformat string with a simple substitution (for instance: "%{test}")?
if (preg_match("/^%[{(][^{(]+[})]$/", $msg)) {
$LSlang_cli_logger -> trace(
"_cli_add_str_to_translate($msg, $context): LSformat string with a simple substitution, ".
"ignore it"
);
return;
}
// Message already exists ? // Message already exists ?
if (array_key_exists($msg, $data)) { if (array_key_exists($msg, $data)) {
if ($context && !in_array($context, $data[$msg]['contexts'])) if ($context && !in_array($context, $data[$msg]['contexts']))
@ -929,6 +938,9 @@ function cli_generate_lang_file($command_args) {
_cli_add_str_to_translate_from_LSconfig("LSobjects.$obj.LSform.layout.*.label"); _cli_add_str_to_translate_from_LSconfig("LSobjects.$obj.LSform.layout.*.label");
_cli_add_str_to_translate_from_LSconfig("LSobjects.$obj.LSform.dataEntryForm.*.label"); _cli_add_str_to_translate_from_LSconfig("LSobjects.$obj.LSform.dataEntryForm.*.label");
// LSioFormat
_cli_add_str_to_translate_from_LSconfig("LSobjects.$obj.ioFormat.*.label");
// LSsearch // LSsearch
_cli_add_str_to_translate_from_LSconfig("LSobjects.$obj.LSsearch.predefinedFilters.*"); _cli_add_str_to_translate_from_LSconfig("LSobjects.$obj.LSsearch.predefinedFilters.*");
_cli_add_str_to_translate_from_LSconfig("LSobjects.$obj.LSsearch.extraDisplayedColumns.*.label"); _cli_add_str_to_translate_from_LSconfig("LSobjects.$obj.LSsearch.extraDisplayedColumns.*.label");

View file

@ -2032,7 +2032,7 @@ class LSldapObject extends LSlog_staticLoggerClass {
$ioFormats = $this -> getConfig('ioFormat'); $ioFormats = $this -> getConfig('ioFormat');
if (is_array($ioFormats)) { if (is_array($ioFormats)) {
foreach($ioFormats as $name => $conf) { foreach($ioFormats as $name => $conf) {
$ret[$name] = _((isset($conf['label'])?$conf['label']:$name)); $ret[$name] = __((isset($conf['label'])?$conf['label']:$name));
} }
} }
return $ret; return $ret;

View file

@ -20,108 +20,119 @@
******************************************************************************/ ******************************************************************************/
/** /*
* Construction d'une chaine formatée * LSFormat : %{[key name][:A][:B][-][! ou _][~][%}}
*
* Cette fonction retourne la valeur d'une chaine formatée selon le format
* et les données passés en paramètre.
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param string $format Format de la chaine
* @param string $format Les données pour composés la chaine
* Ce paramètre peut être un tableau de string, une string,
* une tableau d'objet ou un objet.
* @param string $format Le nom de la methode de/des objet(s) à appeler pour
* obtenir la valeur de remplacement dans la chaine formatée.
*
* @return string La chaine formatée
*/
function getFData($format,$data,$meth=NULL) {
$unique=false;
/*
* Format : %{[key name][:A][:B][! ou _][~][%}}
* *
* Extracted fields * Extracted fields
* - 0 : full string '%{...}' * - 0 : full string '%{...}'
* - 1 : key name * - 1/key : key name
* - 2 : :A * - 2 : :A
* - 3 : A * - 3/substr_a : A
* - 4 : :B * - 4 : :B
* - 5 : B * - 5/substr_b : B
* - 6 : "!" / "_" / "~" / "%" * - 6/modifiers : "!" / "_" / "~" / "%"
*/ */
$expr="/%[{(]([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9]+))?([\!\_~%]*)[})]/"; define(
'GETFDATA_REGEX',
"/%[{(](?P<key>[A-Za-z0-9]+)(\:(?P<substr_a>-?[0-9])+)?(\:(?P<substr_b>[0-9]+))?(?P<modifiers>[\!\_~%]*)[})]/"
);
/**
* Build LSformat string
*
* This function build and return a LSformat string from specified format and provided data.
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param string $format The LSformat string
* @param string|object|array<string|object> $data Data to build the string
* This parameter could be a string, an object or an array of string or object.
* @param string $meth The name to the method of objet(s) to call to get the replacement value for a
* specified key
*
* @return string The builded LSformat string
*/
function getFData($format, $data, $meth=NULL) {
$unique = false;
if(!is_array($format)) { if(!is_array($format)) {
$format=array($format); $format = array($format);
$unique=true; $unique = true;
} }
for($i=0;$i<count($format);$i++) { for($i=0; $i<count($format); $i++) {
if(is_array($data)) { if(is_array($data)) {
if ($meth==NULL) { if (is_null($meth)) {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
if (!isset($data[$ch[1]])) { if (!isset($data[$ch['key']])) {
$val = ''; $val = '';
} }
elseif (is_array($data[$ch[1]])) { elseif (is_array($data[$ch['key']])) {
$val = $data[$ch[1]][0]; $val = $data[$ch['key']][0];
} }
else { else {
$val = $data[$ch[1]]; $val = $data[$ch['key']];
} }
$val=_getFData_extractAndModify($val,$ch); $val = _getFData_extractAndModify($val, $ch);
$format[$i]=str_replace($ch[0],$val,$format[$i]); $format[$i] = str_replace($ch[0], $val, $format[$i]);
} }
} }
else { else {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
if (method_exists($data[$ch[1]],$meth)) { if (method_exists($data[$ch['key']], $meth)) {
$value = $data[$ch[1]] -> $meth(); $value = $data[$ch['key']] -> $meth();
if (is_array($value)) { if (is_array($value)) {
$value = $value[0]; $value = $value[0];
} }
$value=_getFData_extractAndModify($value,$ch); $value = _getFData_extractAndModify($value, $ch);
$format[$i]=str_replace($ch[0],$value,$format[$i]); $format[$i] = str_replace($ch[0], $value, $format[$i]);
} }
else { else {
LSerror :: addErrorCode('fct_getFData_01',array('meth' => $meth,'obj' => $ch[1])); LSerror :: addErrorCode(
'fct_getFData_01',
array('meth' => $meth, 'obj' => $ch['key'])
);
break; break;
} }
} }
} }
} }
elseif (is_object($data)) { elseif (is_object($data)) {
if ($meth==NULL) { if (is_null($meth)) {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
$value = $data -> $ch[1]; $value = $data -> $ch['key'];
if (is_array($value)) { if (is_array($value)) {
$value = $value[0]; $value = $value[0];
} }
$value=_getFData_extractAndModify($value,$ch); $value = _getFData_extractAndModify($value, $ch);
$format[$i]=str_replace($ch[0],$value,$format[$i]); $format[$i] = str_replace($ch[0], $value, $format[$i]);
} }
} }
else { else {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
if (method_exists($data,$meth)) { if (method_exists($data, $meth)) {
$value = $data -> $meth($ch[1]); $value = $data -> $meth($ch['key']);
if (is_array($value)) { if (is_array($value)) {
$value = $value[0]; $value = $value[0];
} }
$value=_getFData_extractAndModify($value,$ch); $value = _getFData_extractAndModify($value, $ch);
$format[$i]=str_replace($ch[0],$value,$format[$i]); $format[$i] = str_replace($ch[0], $value, $format[$i]);
} }
else { else {
LSerror :: addErrorCode(0,getFData(_("Function 'getFData' : The method %{meth} of the object %{obj} doesn't exist."),array('meth' => $meth,'obj' => get_class($data)))); LSerror :: addErrorCode(
0,
getFData(
_("Function 'getFData' : The method %{meth} of the object %{obj} doesn't exist."),
array('meth' => $meth, 'obj' => get_class($data))
)
);
break; break;
} }
} }
} }
} }
else { else {
while (preg_match($expr,$format[$i],$ch)) { while (preg_match(GETFDATA_REGEX, $format[$i], $ch)) {
$val=_getFData_extractAndModify($data,$ch); $val = _getFData_extractAndModify($data, $ch);
$format[$i]=str_replace($ch[0],$val,$format[$i]); $format[$i] = str_replace($ch[0], $val, $format[$i]);
} }
} }
} }
@ -131,100 +142,95 @@ function getFData($format,$data,$meth=NULL) {
return $format; return $format;
} }
function _getFData_extractAndModify($data,$ch) { function _getFData_extractAndModify($data, $ch) {
/* /*
* Format : %{[key name][:A][:B][-][! ou _][~][%}} * Handle substring parameters (substr_a & substr_b)
*
* Extracted fields
* - 0 : full string '%{...}'
* - 1 : key name
* - 2 : :A
* - 3 : A
* - 4 : :B
* - 5 : B
* - 6 : "!" / "_" / "~" / "%"
*/ */
// If A // If A
if($ch[3]!="") { if($ch['substr_a']) {
// If A and B // If A and B
if ($ch[5]!="") { if ($ch['substr_b']) {
// If A and B=0 // If A and B=0
if ($ch[5]==0) { if ($ch['substr_b'] == 0) {
// If A<0 and B=0 // If A<0 and B=0
if ($ch[3]<0) { if ($ch['substr_a'] < 0) {
$s=strlen((string)$data)-(-1*$ch[3]); $s = mb_strlen(strval($data)) - (-1 * $ch['substr_a']);
$l=strlen((string)$data); $l = mb_strlen(strval($data));
} }
// If A >= 0 and B // If A >= 0 and B
else { else {
$s=$ch[3]; $s = $ch['substr_a'];
$l=strlen((string)$data); $l = mb_strlen(strval($data));
} }
} }
// If A and B > 0 // If A and B > 0
elseif ($ch[5]>0) { elseif ($ch['substr_b'] > 0) {
// If A < 0 and B > 0 or A >= 0 and B > 0 // If A < 0 and B > 0 or A >= 0 and B > 0
$s=$ch[3]; $s = $ch['substr_a'];
$l=$ch[5]; $l = $ch['substr_b'];
} }
// If A and B < 0 // If A and B < 0
else { else {
// If A < 0 and B < 0 // If A < 0 and B < 0
if ($ch[3]<0) { if ($ch['substr_a'] < 0) {
$s=$ch[5]; $s = $ch['substr_b'];
$l=false; $l = false;
} }
// If A >= 0 and B < 0 // If A >= 0 and B < 0
else { else {
$s=$ch[3]+$ch[5]; $s = $ch['substr_a'] + $ch['substr_b'];
$l=abs($ch[5]); $l = abs($ch['substr_b']);
} }
} }
} }
// If only A // If only A
else { else {
if ($ch[3]<0) { if ($ch['substr_a'] < 0) {
$s=$ch[3]; $s = $ch['substr_a'];
$l=false; $l = false;
} }
else { else {
$s=0; $s = 0;
$l=$ch[3]; $l = $ch['substr_a'];
} }
} }
if ($l==false) { if ($l == false) {
$val=mb_substr((string)$data,$s); $val = mb_substr(strval($data), $s);
} }
else { else {
$val=mb_substr((string)$data,$s, abs($l)); $val = mb_substr(strval($data), $s, abs($l));
} }
} }
else { else {
try { try {
$val=strval($data); $val = strval($data);
} }
catch (Exception $e) { catch (Exception $e) {
$val=_('[not string value]'); $val = _('[not string value]');
} }
} }
if ($ch[6]) { /*
* Handle modifiers
*/
if ($ch['modifiers']) {
# Without Accent # Without Accent
if (strpos($ch[6], '~')!==false) { if (strpos($ch['modifiers'], '~') !== false) {
$val = withoutAccents($val); $val = withoutAccents($val);
} }
# Upper / Lower case # Upper / Lower case
if (strpos($ch[6], '!')!==false) { if (strpos($ch['modifiers'], '!') !== false) {
$val=mb_strtoupper($val); $val = mb_strtoupper($val);
} }
elseif (strpos($ch[6], '_')!==false) { elseif (strpos($ch['modifiers'], '_') !== false) {
$val=mb_strtolower($val); $val = mb_strtolower($val);
} }
# Escape HTML entities # Escape HTML entities
if (strpos($ch[6], '%')!==false) { if (strpos($ch['modifiers'], '%') !== false) {
$val = htmlentities($val); $val = htmlentities($val);
} }
} }
@ -233,11 +239,10 @@ function _getFData_extractAndModify($data,$ch) {
} }
function getFieldInFormat($format) { function getFieldInFormat($format) {
$fields=array(); $fields = array();
$expr='/%[{(]([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9]+))?(-)?(\!|\_)?(~)?(%)?[})]/'; while (preg_match(GETFDATA_REGEX, $format, $ch)) {
while (preg_match($expr,$format,$ch)) { $fields[] = $ch['key'];
$fields[]=$ch[1]; $format = str_replace($ch[0], '', $format);
$format=str_replace($ch[0],'',$format);
} }
return $fields; return $fields;
} }

View file

@ -109,6 +109,10 @@ $GLOBALS['LSlang'] = array (
"Company" => "Company" =>
"Société", "Société",
# LSobjects.LSpeople.ioFormat.myfullcsv.label
"Complete CSV" =>
"CSV Complet",
# LSobjects.LSpeople.attrs.pwdHistory.html_options.components.time.label # LSobjects.LSpeople.attrs.pwdHistory.html_options.components.time.label
# LSobjects.LSsysaccount.attrs.pwdHistory.html_options.components.time.label # LSobjects.LSsysaccount.attrs.pwdHistory.html_options.components.time.label
"Date added to history" => "Date added to history" =>
@ -693,6 +697,10 @@ $GLOBALS['LSlang'] = array (
"Simple" => "Simple" =>
"Simple", "Simple",
# LSobjects.LSpeople.ioFormat.mycsv.label
"Simple CSV" =>
"CSV Simple",
# LSobjects.LSpeople.attrs.sambaKickoffTime.help_info # LSobjects.LSpeople.attrs.sambaKickoffTime.help_info
"Specifies the time when the user will be locked down and cannot login any longer." => "Specifies the time when the user will be locked down and cannot login any longer." =>
"Détermine la date à partir de laquelle l'utilisateur sera bloqué et ne pourra plus se connecter.", "Détermine la date à partir de laquelle l'utilisateur sera bloqué et ne pourra plus se connecter.",