From bc171e6092ee76f4722ad28acc6785cd2a90bff7 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Fri, 26 May 2023 11:04:01 +0200 Subject: [PATCH] generate_ldapsaisie_pot: Add -I/--internal paramater to use internal message extractor for PHP files instead of xgettext Note: just for tests, not recommended. --- src/includes/class/class.LSlang.php | 84 +++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/src/includes/class/class.LSlang.php b/src/includes/class/class.LSlang.php index afcc580a..92209129 100644 --- a/src/includes/class/class.LSlang.php +++ b/src/includes/class/class.LSlang.php @@ -847,7 +847,7 @@ function _cli_output_pot($fd) { } } -$available_onlys = array("config", "templates", "addons", "auth_methods", "includes"); +$available_onlys = array("config", "templates", "addons", "auth_methods", "includes", "php"); $available_withouts = array_merge($available_onlys, array("select-list")); @@ -916,7 +916,7 @@ function cli_generate_lang_file($command_args) { LScli :: usage(_("You could specify only one -O/--only parameter.")); $only = strtolower($command_args[$i]); if (!in_array($only, $available_onlys)) - LScli :: usage(_("Invalid -O/--only parameter. Must be one of the following values: %s.'"), implode("','", $available_onlys)); + LScli :: usage(_("Invalid -O/--only parameter. Must be one of the following values: '%s'."), implode("', '", $available_onlys)); elseif ($withouts) LScli :: usage(_("You could not use -W/--without parameter combined with -O/--only parameter.")); break; @@ -1179,6 +1179,12 @@ function cli_generate_lang_file($command_args) { _cli_find_and_parse_php_file(LS_ROOT_DIR.'/'.LS_LOCAL_DIR.LS_CONF_DIR.'/LSauth', '/^config\.(.+)\.php$$/'); } + // Manage all PHP files + if ($only == 'php') { + $LSlang_cli_logger -> info("Looking for string to translate in PHP files"); + _cli_find_and_parse_php_file(LS_ROOT_DIR, null, true, array(LS_ROOT_DIR.'/'.LS_LOCAL_DIR)); + } + // Sort resulting strings ksort($data); @@ -1324,6 +1330,22 @@ ___( **/ function cli_generate_ldapsaisie_pot($command_args) { global $LSlang_cli_logger; + + // Parameters + $use_internal_parser = false; + + for ($i=0; $i < count($command_args); $i++) { + switch ($command_args[$i]) { + case '--internal': + case '-I': + $use_internal_parser = true; + break; + + default: + LScli :: usage(_("%s: Invalid parameter."), $command_args[$i]); + } + } + // Initialize logger (if not already initialized by another CLI command) if (!isset($LSlang_cli_logger)) $LSlang_cli_logger = LSlog :: get_logger('generate_ldapsaisie_pot'); @@ -1351,25 +1373,41 @@ function cli_generate_ldapsaisie_pot($command_args) { return false; } - // Extract messages from LdapSaisie PHP files using xgettext - $result = LScli :: run_external_command( - array( - "xgettext", - "--from-code utf-8", - "--language=PHP", - "-o", LS_I18N_DIR_PATH."/ldapsaisie-main.pot", // Output - "--omit-header", // No POT header - "--keyword=__", // Handle custom __() translation function - "--keyword=___", // Handle custom ___() translation function - "--files=-" // Read files to parse from STDIN - ), - $php_files[1], // Pass PHP files list via STDIN - true, // Escape parameters - LS_ROOT_DIR // Run in LdapSaisie root directory - ); - if (!is_array($result) || $result[0] != 0) - $LSlang_cli_logger -> fatal("Fail to extract messages from PHP files using xgettext."); - + // Extract messages from LdapSaisie PHP files + if ($use_internal_parser) { + // Using internal parser + $result = LScli :: run_command( + 'generate_lang_file', + array ( + "-o", LS_I18N_DIR_PATH."/ldapsaisie-main.pot", + "-f", "pot", + "--only", "php", + ), + false // do not exit + ); + if (!$result) + $LSlang_cli_logger -> fatal("Fail to extract messages from PHP files using generate_lang_file command."); + } + else { + // Using xgettext + $result = LScli :: run_external_command( + array( + "xgettext", + "--from-code utf-8", + "--language=PHP", + "-o", LS_I18N_DIR_PATH."/ldapsaisie-main.pot", // Output + "--omit-header", // No POT header + "--keyword=__", // Handle custom __() translation function + "--keyword=___", // Handle custom ___() translation function + "--files=-" // Read files to parse from STDIN + ), + $php_files[1], // Pass PHP files list via STDIN + true, // Escape parameters + LS_ROOT_DIR // Run in LdapSaisie root directory + ); + if (!is_array($result) || $result[0] != 0) + $LSlang_cli_logger -> fatal("Fail to extract messages from PHP files using xgettext."); + } // Extract other messages from LdapSaisie templates files $result = LScli :: run_command( @@ -1410,6 +1448,10 @@ LScli :: add_command( ___(" => contains messages from templates files"), " - ".LS_I18N_DIR_PATH."/ldapsaisie.pot", ___(" => contains all messages"), + null, +___( +"Note: Use -I / --internal parameter to use internal parser instead of +xgettext for PHP files."), ), false // This command does not need LDAP connection );