Add update_messages & compile_messages CLI commands

This commit is contained in:
Benjamin Renard 2021-10-07 20:59:40 +02:00
parent 6684efab2f
commit a7e4ea8168

View file

@ -437,3 +437,132 @@ add_cli_command(
null,
___("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.")
);