From c0d874c68d5f0d07f050cfa4ddea63817631bf0b Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Fri, 8 Oct 2021 17:17:17 +0200 Subject: [PATCH] generate_lang_file CLI command: add -K/--keep-unused parameter --- src/includes/class/class.LSlang.php | 37 +++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/includes/class/class.LSlang.php b/src/includes/class/class.LSlang.php index c369882c..2d73dfdf 100644 --- a/src/includes/class/class.LSlang.php +++ b/src/includes/class/class.LSlang.php @@ -238,7 +238,8 @@ if (php_sapi_name() != "cli") return true; function cli_generate_lang_file($command_args) { // Use global variables to share it with sub-functions global $LSlang_cli_logger, $available_onlys, $available_withouts, $data, $translations, $interactive, - $interactive_exit, $copyoriginalvalue, $format, $curdir, $additionalfileformat, $copyoriginalvalue, $lang; + $interactive_exit, $copyoriginalvalue, $format, $curdir, $additionalfileformat, $copyoriginalvalue, + $keep_unused, $lang; // Initialize logger (if not already initialized by another CLI command) if (!isset($LSlang_cli_logger)) @@ -258,6 +259,7 @@ function cli_generate_lang_file($command_args) { $interactive_exit = False; // Exit flag set when user type 'q' $output = False; $additionalfileformat = False; + $keep_unused = False; $lang = null; $encoding = null; $available_formats = array('php', 'pot'); @@ -363,6 +365,11 @@ function cli_generate_lang_file($command_args) { $debug = true; break; + case '--keep-unused': + case '-K': + $keep_unused = true; + break; + default: $path = relative2absolute_path($command_args[$i]); if (is_file($path)) @@ -814,7 +821,7 @@ function cli_generate_lang_file($command_args) { * Handle output file format */ function output_php($fd) { - global $LSlang_cli_logger, $additionalfileformat, $data, $copyoriginalvalue; + global $LSlang_cli_logger, $additionalfileformat, $data, $copyoriginalvalue, $keep_unused, $translations; fwrite($fd, " $trans) + if (!array_key_exists($msg, $data)) + $unused_msgs[$msg] = $trans; + if ($unused_msgs) { + fwrite($fd, "\n\n"); + fwrite($fd, "######################################################################\n"); + fwrite($fd, "# Unused translations keeped #\n"); + fwrite($fd, "######################################################################\n"); + foreach($unused_msgs as $key => $val) { + $key=str_replace('"','\\"',$key); + $val=str_replace('"','\\"',$val); + if ($additionalfileformat) { + fwrite($fd, "\n\$GLOBALS['LSlang'][\"$key\"] = \"$val\";\n"); + } + else { + fwrite($fd, "\n\"$key\" =>\n \"$val\",\n"); + } + } + } + } + if (!$additionalfileformat) fwrite($fd, "\n);\n"); } @@ -948,6 +979,7 @@ function cli_generate_lang_file_args_autocompleter($comp_words, $comp_word_num, '-o', '--output', '-f', '--format', '-I', '--include-upstream', + '-K', '--keep-unused', ) ); return LScli :: autocomplete_opts($opts, $comp_word); @@ -976,6 +1008,7 @@ LScli :: add_command( " -o/--output Output file (default: stdout)", " -f/--format Output file format : php or pot", " (default: php)", + " -K/--keep-unused Keep unused translations in resulting file", ), false, // This command does not need LDAP connection 'cli_generate_lang_file_args_autocompleter'