From 40a17c3524bdd41332934b62def5c9858eb5d5bd Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Fri, 29 Jan 2021 15:23:22 +0100 Subject: [PATCH] LSlang: improve generate_lang_file command Improve detection in PHP files, add detection in LSaddons/LSauthMethod config files and includes PHP files. Add -I/--include-upstream parameter (and use it for generate_ldapsaisie_pot command) --- src/includes/class/class.LSlang.php | 186 +++++++++++++++++----------- 1 file changed, 113 insertions(+), 73 deletions(-) diff --git a/src/includes/class/class.LSlang.php b/src/includes/class/class.LSlang.php index fae7e6d5..c33ca368 100644 --- a/src/includes/class/class.LSlang.php +++ b/src/includes/class/class.LSlang.php @@ -232,7 +232,7 @@ if (php_sapi_name() != "cli") return true; **/ global $LSlang_cli_logger, $available_onlys, $available_withouts; - $available_onlys = array("config", "templates", "addons"); + $available_onlys = array("config", "templates", "addons", "auth_methods", "includes"); $available_withouts = array_merge($available_onlys, array("select-list")); function cli_generate_lang_file($command_args) { // Use global variables to share it with sub-functions @@ -251,6 +251,7 @@ function cli_generate_lang_file($command_args) { // Parameters $only = null; $withouts = array(); + $include_upstream = false; $copyoriginalvalue = False; $interactive = False; $interactive_exit = False; // Exit flag set when user type 'q' @@ -308,6 +309,11 @@ function cli_generate_lang_file($command_args) { LScli :: usage("You could not use only -O/--only parameter combined with -W/--without parameter."); break; + case '-I': + case '--include-upstream': + $include_upstream=True; + break; + case '--copy-original-value': case '-c': $copyoriginalvalue=True; @@ -687,86 +693,117 @@ function cli_generate_lang_file($command_args) { } } $LSlang_cli_logger -> info("Looking for string to translate in templates file"); - find_and_parse_template_file(LS_ROOT_DIR.'/'.LS_TEMPLATES_DIR); + if ($include_upstream) find_and_parse_template_file(LS_ROOT_DIR.'/'.LS_TEMPLATES_DIR); find_and_parse_template_file(LS_ROOT_DIR.'/'.LS_LOCAL_DIR.LS_TEMPLATES_DIR); } + /* + * Manage custom PHP code/config files + */ + function parse_php_file($file) { + global $LSlang_cli_logger; + $LSlang_cli_logger -> debug("Looking for string to translate in '$file' PHP file"); + $count = 0; + $quote=''; + $res=''; + foreach(file($file) as $line) { + $count++; + $LSlang_cli_logger -> trace("Handle line #$count of '$file' PHP file"); + $offset=0; + while ($pos = strpos($line,'__(',$offset)) { + $LSlang_cli_logger -> trace("$file:$count: detect keyword at position #$pos ('$line')"); + for ($i=$pos+3;$i debug("Looking for string to translate in '$file' LSaddon file"); - $count = 0; - foreach(file($file) as $line) { - $count++; - $offset=0; - while ($pos = strpos($line,'__(',$offset)) { - $quote=''; - $res=''; - for ($i=$pos+3;$i info("Looking for string to translate in LSaddons PHP code"); - find_and_parse_addon_file(LS_ROOT_DIR.'/'.LS_ADDONS_DIR); - find_and_parse_addon_file(LS_ROOT_DIR.'/'.LS_LOCAL_DIR.LS_ADDONS_DIR); + if ($include_upstream) find_and_parse_php_file(LS_ROOT_DIR.'/'.LS_ADDONS_DIR, '/^LSaddons\.(.+)\.php$/'); + find_and_parse_php_file(LS_ROOT_DIR.'/'.LS_LOCAL_DIR.LS_ADDONS_DIR, '/^LSaddons\.(.+)\.php$/'); + $LSlang_cli_logger -> info("Looking for string to translate in LSaddons configuration files"); + if ($include_upstream) find_and_parse_php_file(LS_ROOT_DIR.'/'.LS_CONF_DIR.'/LSaddons', '/^config\.LSaddons\.(.+)\.php$$/'); + find_and_parse_php_file(LS_ROOT_DIR.'/'.LS_LOCAL_DIR.LS_CONF_DIR.'/LSaddons', '/^config\.LSaddons\.(.+)\.php$$/'); + } + + /* + * Manage auth methods files + */ + if (!in_array('auth_methods', $withouts) && (!$only || $only == 'auth_methods')) { + $LSlang_cli_logger -> info("Looking for string to translate in LSauthMethods configuration files"); + if ($include_upstream) find_and_parse_php_file(LS_ROOT_DIR.'/'.LS_CONF_DIR.'/LSauth', '/^config\.(.+)\.php$$/'); + find_and_parse_php_file(LS_ROOT_DIR.'/'.LS_LOCAL_DIR.LS_CONF_DIR.'/LSauth', '/^config\.(.+)\.php$$/'); } // Sort resulting strings @@ -909,6 +946,7 @@ function cli_generate_lang_file_args_autocompleter($comp_words, $comp_word_num, '-l', '--lang', '-o', '--output', '-f', '--format', + '-I', '--include-upstream', ) ); return LScli :: autocomplete_opts($opts, $comp_word); @@ -926,6 +964,7 @@ LScli :: add_command( " -O/--only Only handle specified messages. Must be one", " of the following values :", " - ".implode("\n - ", $available_onlys), + " -I/--include-upstream Include upstream code to message lookup", " -c/--copy-original-value Copy original value as translated value when", " no translated value exists", " -i/--interactive Interactive mode : ask user to enter", @@ -1000,6 +1039,7 @@ function cli_generate_ldapsaisie_pot($command_args) { "-o", LS_I18N_DIR_PATH."/ldapsaisie-templates.pot", "-f", "pot", "--only", "templates", + "--include-upstream", ), false // do not exit );