generate_lang_file CLI command: add -K/--keep-unused parameter

This commit is contained in:
Benjamin Renard 2021-10-08 17:17:17 +02:00
parent 91b2b4e1ab
commit c0d874c68d

View file

@ -238,7 +238,8 @@ if (php_sapi_name() != "cli") return true;
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 $LSlang_cli_logger, $available_onlys, $available_withouts, $data, $translations, $interactive, 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) // Initialize logger (if not already initialized by another CLI command)
if (!isset($LSlang_cli_logger)) 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' $interactive_exit = False; // Exit flag set when user type 'q'
$output = False; $output = False;
$additionalfileformat = False; $additionalfileformat = False;
$keep_unused = False;
$lang = null; $lang = null;
$encoding = null; $encoding = null;
$available_formats = array('php', 'pot'); $available_formats = array('php', 'pot');
@ -363,6 +365,11 @@ function cli_generate_lang_file($command_args) {
$debug = true; $debug = true;
break; break;
case '--keep-unused':
case '-K':
$keep_unused = true;
break;
default: default:
$path = relative2absolute_path($command_args[$i]); $path = relative2absolute_path($command_args[$i]);
if (is_file($path)) if (is_file($path))
@ -814,7 +821,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 $LSlang_cli_logger, $additionalfileformat, $data, $copyoriginalvalue; global $LSlang_cli_logger, $additionalfileformat, $data, $copyoriginalvalue, $keep_unused, $translations;
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");
@ -837,6 +844,30 @@ function cli_generate_lang_file($command_args) {
} }
} }
// Handle keep unused mode
if ($keep_unused) {
$unused_msgs = array();
foreach ($translations as $msg => $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"); if (!$additionalfileformat) fwrite($fd, "\n);\n");
} }
@ -948,6 +979,7 @@ function cli_generate_lang_file_args_autocompleter($comp_words, $comp_word_num,
'-o', '--output', '-o', '--output',
'-f', '--format', '-f', '--format',
'-I', '--include-upstream', '-I', '--include-upstream',
'-K', '--keep-unused',
) )
); );
return LScli :: autocomplete_opts($opts, $comp_word); return LScli :: autocomplete_opts($opts, $comp_word);
@ -976,6 +1008,7 @@ LScli :: add_command(
" -o/--output Output file (default: stdout)", " -o/--output Output file (default: stdout)",
" -f/--format Output file format : php or pot", " -f/--format Output file format : php or pot",
" (default: php)", " (default: php)",
" -K/--keep-unused Keep unused translations in resulting file",
), ),
false, // This command does not need LDAP connection false, // This command does not need LDAP connection
'cli_generate_lang_file_args_autocompleter' 'cli_generate_lang_file_args_autocompleter'