LSlang : use LSlog_logger

This commit is contained in:
Benjamin Renard 2020-05-08 13:23:45 +02:00
parent 6db3543ec0
commit 7425555b3d

View file

@ -20,7 +20,9 @@
******************************************************************************/ ******************************************************************************/
class LSlang { LSsession :: loadLSclass('LSlog_staticLoggerClass');
class LSlang extends LSlog_staticLoggerClass {
// Current lang and encoding // Current lang and encoding
@ -76,7 +78,7 @@ class LSlang {
// Check // Check
if (self :: localeExist($lang, $encoding)) { if (self :: localeExist($lang, $encoding)) {
LSlog :: debug("LSlang :: setLocale() : Use local '$lang.$encoding'"); self :: log("DEBUG", "setLocale() : Use local '$lang.$encoding'");
if ($encoding) { if ($encoding) {
$lang .= '.'.$encoding; $lang .= '.'.$encoding;
} }
@ -85,26 +87,26 @@ class LSlang {
// Set the locale // Set the locale
if (setlocale(LC_ALL, $lang) === false) if (setlocale(LC_ALL, $lang) === false)
LSlog :: error("An error occured setting locale to '$lang'"); self :: log("ERROR", "An error occured setting locale to '$lang'");
// Configure and set the text domain // Configure and set the text domain
$fullpath = bindtextdomain(LS_TEXT_DOMAIN, LS_I18N_DIR_PATH); $fullpath = bindtextdomain(LS_TEXT_DOMAIN, LS_I18N_DIR_PATH);
LSlog :: debug("LSlang :: setLocale(): Text domain fullpath is '$fullpath'."); self :: log("DEBUG", "setLocale(): Text domain fullpath is '$fullpath'.");
LSlog :: debug("LSlang :: setLocale(): Text domain is : ".textdomain(LS_TEXT_DOMAIN)); self :: log("DEBUG", "setLocale(): Text domain is : ".textdomain(LS_TEXT_DOMAIN));
// Include local translation file // Include local translation file
$lang_file = LS_I18N_DIR.'/'.$lang.'/lang.php'; $lang_file = LS_I18N_DIR.'/'.$lang.'/lang.php';
if (LSsession :: includeFile($lang_file, false, false)) if (LSsession :: includeFile($lang_file, false, false))
LSlog :: debug("LSlang :: setLocale(): lang file '$lang_file' loaded."); self :: log("DEBUG", "setLocale(): lang file '$lang_file' loaded.");
else else
LSlog :: debug("LSlang :: setLocale(): no lang file found ($lang_file)."); self :: log("DEBUG", "setLocale(): no lang file found ($lang_file).");
// Include other local translation file(s) // Include other local translation file(s)
foreach(array(LS_I18N_DIR_PATH.'/'.$lang, LS_LOCAL_DIR.'/'.LS_I18N_DIR.'/'.$lang) as $lang_dir) { foreach(array(LS_I18N_DIR_PATH.'/'.$lang, LS_LOCAL_DIR.'/'.LS_I18N_DIR.'/'.$lang) as $lang_dir) {
if (is_dir($lang_dir)) { if (is_dir($lang_dir)) {
foreach (listFiles($lang_dir, '/^lang.+\.php$/') as $file) { foreach (listFiles($lang_dir, '/^lang.+\.php$/') as $file) {
$path = "$lang_dir/$file"; $path = "$lang_dir/$file";
LSlog :: debug("LSlang :: setLocale(): Local '$lang.$encoding' : load translation file '$path'"); self :: log("DEBUG", "setLocale(): Local '$lang.$encoding' : load translation file '$path'");
include($path); include($path);
} }
} }
@ -112,7 +114,7 @@ class LSlang {
} }
else { else {
if ($encoding && $lang) $lang .= '.'.$encoding; if ($encoding && $lang) $lang .= '.'.$encoding;
LSlog :: error("The local '$lang' does not exists, use default one."); self :: log("ERROR", "The local '$lang' does not exists, use default one.");
} }
} }
@ -211,14 +213,19 @@ if (php_sapi_name() != "cli") return;
* *
* @retval boolean True on succes, false otherwise * @retval boolean True on succes, false otherwise
**/ **/
global $available_onlys, $available_withouts; global $LSlang_cli_logger, $available_onlys, $available_withouts;
$available_onlys = array("config", "templates", "addons"); $available_onlys = array("config", "templates", "addons");
$available_withouts = array_merge($available_onlys, array("select-list")); $available_withouts = array_merge($available_onlys, array("select-list"));
function cli_generate_lang_file($command_args) { function cli_generate_lang_file($command_args) {
// Use global variables to share it with sub-functions // Use global variables to share it with sub-functions
global $available_onlys, $available_withouts, $data, $translations, $interactive, global $LSlang_cli_logger, $available_onlys, $available_withouts, $data, $translations, $interactive,
$copyoriginalvalue, $format, $curdir, $additionalfileformat, $copyoriginalvalue, $lang; $copyoriginalvalue, $format, $curdir, $additionalfileformat, $copyoriginalvalue, $lang;
// Initialize logger (if not already initialized by another CLI command)
if (!isset($LSlang_cli_logger))
$LSlang_cli_logger = LSlog :: get_logger('generate_lang_file');
// Store existing translations // Store existing translations
$translations = array(); $translations = array();
// Store output translations // Store output translations
@ -340,15 +347,9 @@ function cli_generate_lang_file($command_args) {
} }
} }
function debug($msg) {
LSlog :: debug("generate_lang_file() : $msg");
}
function add($msg, $context=null) { function add($msg, $context=null) {
global $lang, $data, $translations, $interactive, $copyoriginalvalue, $format; global $LSlang_cli_logger, $lang, $data, $translations, $interactive, $copyoriginalvalue, $format;
debug("add($msg, $context)"); $LSlang_cli_logger -> debug("add($msg, $context)");
if ($msg == '') if ($msg == '')
return; return;
if (!is_null($lang) && _($msg) != "$msg") if (!is_null($lang) && _($msg) != "$msg")
@ -400,11 +401,12 @@ function cli_generate_lang_file($command_args) {
} }
function addFromLSconfig($pattern, $value='value', $excludes=array()) { function addFromLSconfig($pattern, $value='value', $excludes=array()) {
debug("addFromLSconfig($pattern, array(".implode(',', $excludes)."))"); global $LSlang_cli_logger;
$LSlang_cli_logger -> debug("addFromLSconfig($pattern, array(".implode(',', $excludes)."))");
$keys = LSconfig :: getMatchingKeys($pattern); $keys = LSconfig :: getMatchingKeys($pattern);
debug("addFromLSconfig : ".count($keys)." matching key(s)"); $LSlang_cli_logger -> debug("addFromLSconfig : ".count($keys)." matching key(s)");
foreach ($keys as $key => $value) { foreach ($keys as $key => $value) {
debug("addFromLSconfig : $key = $value"); $LSlang_cli_logger -> debug("addFromLSconfig : $key = $value");
if ($value == 'key') { if ($value == 'key') {
// Get the last key parts as value and all other as key // Get the last key parts as value and all other as key
$key_parts = explode('.', $key); $key_parts = explode('.', $key);
@ -418,7 +420,7 @@ function cli_generate_lang_file($command_args) {
// Load translation files // Load translation files
foreach($load_files as $path) { foreach($load_files as $path) {
debug("Load $path lang file"); $LSlang_cli_logger -> debug("Load $path lang file");
@include($path); @include($path);
foreach($GLOBALS['LSlang'] as $msg => $trans) { foreach($GLOBALS['LSlang'] as $msg => $trans) {
$translations[$msg]=$trans; $translations[$msg]=$trans;
@ -465,7 +467,7 @@ function cli_generate_lang_file($command_args) {
} }
debug('LSobjects list : '.implode(', ', $objects)); $LSlang_cli_logger -> debug('LSobjects list : '.implode(', ', $objects));
// LSobject // LSobject
foreach($objects as $obj) { foreach($objects as $obj) {
@ -626,18 +628,19 @@ function cli_generate_lang_file($command_args) {
*/ */
if (!in_array('templates', $withouts) && (!$only || $only == 'templates')) { if (!in_array('templates', $withouts) && (!$only || $only == 'templates')) {
function parse_template_file($file) { function parse_template_file($file) {
debug("parse_template_file($file) : start ..."); global $LSlang_cli_logger;
$LSlang_cli_logger -> debug("parse_template_file($file) : start ...");
$count = 0; $count = 0;
foreach(file($file) as $line) { foreach(file($file) as $line) {
$count ++; $count ++;
if (preg_match_all('/\{ *tr +msg=["\']([^\}]+)["\'] *\}/',$line,$matches)) { if (preg_match_all('/\{ *tr +msg=["\']([^\}]+)["\'] *\}/',$line,$matches)) {
foreach($matches[1] as $t) { foreach($matches[1] as $t) {
debug(" - \"$t\" # Line $count"); $LSlang_cli_logger -> debug(" - \"$t\" # Line $count");
add($t, absolute2relative_path($file).":$count"); add($t, absolute2relative_path($file).":$count");
} }
} }
} }
debug("parse_template_file($file) : done."); $LSlang_cli_logger -> debug("parse_template_file($file) : done.");
} }
function find_and_parse_template_file($dir) { function find_and_parse_template_file($dir) {
@ -666,7 +669,8 @@ function cli_generate_lang_file($command_args) {
if (!in_array('addons', $withouts) && (!$only || $only == 'addons')) { if (!in_array('addons', $withouts) && (!$only || $only == 'addons')) {
function parse_addon_file($file) { function parse_addon_file($file) {
debug("parse_addon_file($file)"); global $LSlang_cli_logger;
$LSlang_cli_logger -> debug("parse_addon_file($file)");
$count = 0; $count = 0;
foreach(file($file) as $line) { foreach(file($file) as $line) {
$count++; $count++;
@ -743,7 +747,7 @@ function cli_generate_lang_file($command_args) {
* Handle output file format * Handle output file format
*/ */
function output_php($fd) { function output_php($fd) {
global $additionalfileformat, $data, $copyoriginalvalue; global $LSlang_cli_logger, $additionalfileformat, $data, $copyoriginalvalue;
fwrite($fd, "<?php\n\n"); fwrite($fd, "<?php\n\n");
if (!$additionalfileformat) fwrite($fd, "\$GLOBALS['LSlang'] = array (\n"); if (!$additionalfileformat) fwrite($fd, "\$GLOBALS['LSlang'] = array (\n");
@ -775,7 +779,7 @@ function cli_generate_lang_file($command_args) {
} }
function output_pot($fd) { function output_pot($fd) {
global $data, $copyoriginalvalue; global $LSlang_cli_logger, $data, $copyoriginalvalue;
foreach($data as $key => $key_data) { foreach($data as $key => $key_data) {
if ($copyoriginalvalue && $key_data['translation'] == "") { if ($copyoriginalvalue && $key_data['translation'] == "") {
$val = $key; $val = $key;
@ -794,14 +798,14 @@ function cli_generate_lang_file($command_args) {
if ($output) { if ($output) {
$output = relative2absolute_path($output); $output = relative2absolute_path($output);
try { try {
debug("Open output file ($output)"); $LSlang_cli_logger -> debug("Open output file ($output)");
$fd = fopen($output, 'w'); $fd = fopen($output, 'w');
} }
catch(Exception $e) { catch(Exception $e) {
LSlog :: error('Error occured opening output file : '.$e->getMessage(), "\n"); $LSlang_cli_logger -> error('Error occured opening output file : '.$e->getMessage(), "\n");
} }
if (!$fd) { if (!$fd) {
LSlog :: error("Use stdout out instead.\n"); $LSlang_cli_logger -> error("Use stdout out instead.\n");
$fd = STDOUT; $fd = STDOUT;
$output = false; $output = false;
} }
@ -810,7 +814,7 @@ function cli_generate_lang_file($command_args) {
$fd = STDOUT; $fd = STDOUT;
// Generate output // Generate output
debug("Output format : $format"); $LSlang_cli_logger -> debug("Output format : $format");
switch($format) { switch($format) {
case 'pot': case 'pot':
output_pot($fd); output_pot($fd);
@ -823,7 +827,7 @@ function cli_generate_lang_file($command_args) {
// Close output file (is specified) // Close output file (is specified)
if ($output && $fd != STDOUT) { if ($output && $fd != STDOUT) {
debug("Close output file ($output)"); $LSlang_cli_logger -> debug("Close output file ($output)");
fclose($fd); fclose($fd);
} }
@ -863,13 +867,18 @@ LScli :: add_command(
* @retval boolean True on succes, false otherwise * @retval boolean True on succes, false otherwise
**/ **/
function cli_generate_ldapsaisie_pot($command_args) { function cli_generate_ldapsaisie_pot($command_args) {
global $LSlang_cli_logger;
// Initialize logger (if not already initialized by another CLI command)
if (!isset($LSlang_cli_logger))
$LSlang_cli_logger = LSlog :: get_logger('generate_ldapsaisie_pot');
// Clean php file in tmp directory // Clean php file in tmp directory
if (is_dir(LS_TMP_DIR_PATH)) { if (is_dir(LS_TMP_DIR_PATH)) {
foreach(listFiles(LS_TMP_DIR_PATH, '/\.php$/') as $file) { foreach(listFiles(LS_TMP_DIR_PATH, '/\.php$/') as $file) {
$tmp_file = LS_TMP_DIR_PATH.$file; $tmp_file = LS_TMP_DIR_PATH.$file;
LSlog :: debug("Remove temporary file '$tmp_file'"); $LSlang_cli_logger -> debug("Remove temporary file '$tmp_file'");
if (!unlink($tmp_file)) { if (!unlink($tmp_file)) {
LSlog :: fatal("Fail to delete temporary file '$tmp_file'."); $LSlang_cli_logger -> fatal("Fail to delete temporary file '$tmp_file'.");
} }
} }
} }
@ -881,7 +890,7 @@ function cli_generate_ldapsaisie_pot($command_args) {
false // do not escape command args (already done) false // do not escape command args (already done)
); );
if (!is_array($php_files) || $php_files[0] != 0) { if (!is_array($php_files) || $php_files[0] != 0) {
LSlog :: fatal("Fail to list PHP files."); $LSlang_cli_logger -> fatal("Fail to list PHP files.");
} }
// Extract messages from LdapSaisie PHP files using xgettext // Extract messages from LdapSaisie PHP files using xgettext
@ -899,7 +908,7 @@ function cli_generate_ldapsaisie_pot($command_args) {
$php_files[1] // Pass PHP files list via STDIN $php_files[1] // Pass PHP files list via STDIN
); );
if (!is_array($result) || $result[0] != 0) if (!is_array($result) || $result[0] != 0)
LSlog :: fatal("Fail to extract messages from PHP files using xgettext."); $LSlang_cli_logger -> fatal("Fail to extract messages from PHP files using xgettext.");
// Extract other messages from LdapSaisie templates files // Extract other messages from LdapSaisie templates files
@ -913,7 +922,7 @@ function cli_generate_ldapsaisie_pot($command_args) {
false // do not exit false // do not exit
); );
if (!$result) if (!$result)
LSlog :: fatal("Fail to extract messages from template files using generate_lang_file command."); $LSlang_cli_logger -> fatal("Fail to extract messages from template files using generate_lang_file command.");
// Merge previous results in ldapsaisie.pot file using msgcat // Merge previous results in ldapsaisie.pot file using msgcat
$result = LScli :: run_external_command(array( $result = LScli :: run_external_command(array(
@ -923,7 +932,7 @@ function cli_generate_ldapsaisie_pot($command_args) {
"-o", LS_I18N_DIR_PATH."/ldapsaisie.pot", "-o", LS_I18N_DIR_PATH."/ldapsaisie.pot",
)); ));
if (!is_array($result) || $result[0] != 0) if (!is_array($result) || $result[0] != 0)
LSlog :: fatal("Fail to merge messages using msgcat."); $LSlang_cli_logger -> fatal("Fail to merge messages using msgcat.");
return true; return true;
} }