From a7e4ea8168f6766378156b46eb50e01d51377e24 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 7 Oct 2021 20:59:40 +0200 Subject: [PATCH] Add update_messages & compile_messages CLI commands --- includes/cli.php | 137 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 133 insertions(+), 4 deletions(-) diff --git a/includes/cli.php b/includes/cli.php index 8cf50a2..e8e8b8a 100644 --- a/includes/cli.php +++ b/includes/cli.php @@ -431,9 +431,138 @@ function cli_extract_messages($command_args) { logging('FATAL', _("Fail to merge messages using msgcat.")); } add_cli_command( - 'extract_messages', - 'cli_extract_messages', - ___("Extract messages that need to be translated"), + 'extract_messages', + 'cli_extract_messages', + ___("Extract messages that need to be translated"), null, - ___("This command could be used to generate/update lang/messages.pot file.") + ___("This command could be used to generate/update lang/messages.pot file.") +); + + +function cli_update_messages($command_args) { + global $root_dir_path, $root_lang_dir, $smarty_templates_dir; + + $pot_file = "$root_lang_dir/messages.pot"; + if (!is_file($pot_file)) + logging( + 'FATAL', sprintf( + _("POT file not found (%s). Please run extract_messages first."), + $pot_file + ) + ); + + if ($dh = opendir($root_lang_dir)) { + $error = False; + while (($file = readdir($dh)) !== false) { + if (!is_dir($root_lang_dir . '/' . $file) || in_array($file, array('.', '..')) || is_link($root_lang_dir . '/' . $file)) + continue; + + logging('DEBUG', sprintf(_("Lang directory '%s' found"), $file)); + + // Check LC_MESSAGES directory exists + $lang = $file; + $lang_dir = $root_lang_dir . '/' . $file . '/LC_MESSAGES' ; + if (!is_dir($lang_dir)) { + logging('DEBUG', sprintf( + _("LC_MESSAGES directory not found in lang '%s' directory, ignore it."), + $lang) + ); + continue; + } + + // Test .PO file is present + $po_file = $lang_dir . '/' . TEXT_DOMAIN . '.po'; + if (!is_file($po_file)) { + logging('DEBUG', sprintf( + _("PO file not found in lang '%s' directory, ignore it."), + $lang) + ); + continue; + } + + // Update messages in PO file from POT file using msgmerge + $result = run_external_command( + array("msgmerge", "-q", "-U", $po_file, $pot_file) + ); + if (!is_array($result) || $result[0] != 0) { + logging('ERROR', sprintf( + _("Fail to update messages in %s PO file using msgmerge (%s)."), + $lang, $po_file) + ); + $error = True; + } + } + closedir($dh); + return !$error; + } + + logging('FATAL', sprintf(_("Fail to open root lang directory (%s)."), $root_dir_path)); +} +add_cli_command( + 'update_messages', + 'cli_update_messages', + ___("Update messages in existing translation PO lang files"), + null, + ___("This command could be used to update PO files in lang/*/LC_MESSAGES directories.") +); + + +function cli_compile_messages($command_args) { + global $root_dir_path, $root_lang_dir, $smarty_templates_dir; + + if ($dh = opendir($root_lang_dir)) { + $error = False; + while (($file = readdir($dh)) !== false) { + if (!is_dir($root_lang_dir . '/' . $file) || in_array($file, array('.', '..')) || is_link($root_lang_dir . '/' . $file)) + continue; + + logging('DEBUG', sprintf(_("Lang directory '%s' found"), $file)); + + // Check LC_MESSAGES directory exists + $lang = $file; + $lang_dir = $root_lang_dir . '/' . $file . '/LC_MESSAGES' ; + if (!is_dir($lang_dir)) { + logging('DEBUG', sprintf( + _("LC_MESSAGES directory not found in lang '%s' directory, ignore it."), + $lang) + ); + continue; + } + + // Test .PO file is present + $po_file = $lang_dir . '/' . TEXT_DOMAIN . '.po'; + if (!is_file($po_file)) { + logging('DEBUG', sprintf( + _("PO file not found in lang '%s' directory, ignore it."), + $lang) + ); + continue; + } + + $mo_file = preg_replace('/\.po$/', '.mo', $po_file); + + // Compile messages from PO file to MO file using msgfmt + $result = run_external_command( + array("msgfmt", "-o", $mo_file, $po_file) + ); + if (!is_array($result) || $result[0] != 0) { + logging('ERROR', sprintf( + _("Fail to compile messages from %s PO file as MO file using msgfmt (%s)."), + $lang, $po_file) + ); + $error = True; + } + } + closedir($dh); + return !$error; + } + + logging('FATAL', sprintf(_("Fail to open root lang directory (%s)."), $root_dir_path)); +} +add_cli_command( + 'compile_messages', + 'cli_compile_messages', + ___("Compile messages from existing translation PO lang files to corresponding MO files"), + null, + ___("This command could be used to compile PO files in lang/*/LC_MESSAGES directories to MO files.") );