Add --use-existing-template argument

This is intended to allow translation updates in contexts where building within
the `fish_xgettext.fish` script is undesirable.

Specifically, this allows checking for PO file updates in the tests run by
`test_driver.py`. Because these use a tmpdir for `$HOME`, building within such a
test requires installing the entire Rust toolchain and doing a clean build,
which is a waste of resources.
With this argument, it is possible to build the template before running the
tests and passing the file path into the script.
This commit is contained in:
Daniel Rainer
2025-06-08 00:27:33 +02:00
parent 1e571263a0
commit 85fb937a4d
2 changed files with 33 additions and 8 deletions

View File

@@ -2,6 +2,10 @@
#
# Tool to generate gettext messages template file.
# Writes to stdout.
# Intended to be called from `update_translations.fish`.
argparse use-existing-template= -- $argv
or exit $status
begin
# Write header. This is required by msguniq.
@@ -15,17 +19,25 @@ begin
echo ""
end
set -l rust_extraction_file (mktemp)
set -g repo_root (status dirname)/..
# We need to build to ensure that the proc macro for extracting strings runs.
FISH_GETTEXT_EXTRACTION_FILE=$rust_extraction_file cargo check
or exit 1
set -l rust_extraction_file
if set -l --query _flag_use_existing_template
set rust_extraction_file $_flag_use_existing_template
else
set rust_extraction_file (mktemp)
# We need to build to ensure that the proc macro for extracting strings runs.
FISH_GETTEXT_EXTRACTION_FILE=$rust_extraction_file cargo check
or exit 1
end
# Get rid of duplicates and sort.
msguniq --no-wrap --strict --sort-output $rust_extraction_file
or exit 1
rm $rust_extraction_file
if not set -l --query _flag_use_existing_template
rm $rust_extraction_file
end
function extract_fish_script_messages --argument-names regex
@@ -54,7 +66,7 @@ begin
sed -E -e 's_\\\\_\\\\\\\\_g' -e 's_"_\\\\"_g' -e 's_^(.*)$_msgid "\1"\nmsgstr ""\n_'
end
set -g share_dir (status dirname)/../share
set -g share_dir $repo_root/share
# This regex handles explicit requests to translate a message. These are more important to translate
# than messages which should be implicitly translated.

View File

@@ -21,6 +21,15 @@
# - Specify `--dry-run` to see if any updates to the PO files would by applied by this script.
# If this flag is specified, the script will exit with an error if there are outstanding
# changes, and will display the diff. Do not specify other flags if `--dry-run` is specified.
#
# Specify `--use-existing-template=FILE` to prevent running cargo for extracting an up-to-date
# version of the localized strings. This flag is intended for testing setups which make it
# inconvenient to run cargo here, but run it in an earlier step to ensure up-to-date values.
# This argument is passed on to the `fish_xgettext.fish` script and has no other uses.
# `FILE` must be the path to a gettext template file generated from our compilation process.
# It can be obtained by running:
# set -l FILE (mktemp)
# FISH_GETTEXT_EXTRACTION_FILE=$FILE cargo check
# The sort utility is locale-sensitive.
# Ensure that sorting output is consistent by setting LC_ALL here.
@@ -44,7 +53,7 @@ function cleanup_exit
exit $exit_status
end
argparse --exclusive 'no-mo,only-mo,dry-run' no-mo only-mo dry-run -- $argv
argparse --exclusive 'no-mo,only-mo,dry-run' no-mo only-mo dry-run use-existing-template= -- $argv
or exit $status
# Make sure that the template file is not included in $po_files.
@@ -84,7 +93,11 @@ if set -l --query _flag_only_mo
end
if set -l --query extract
$build_tools/fish_xgettext.fish >$template_file
set -l xgettext_args
if set -l --query _flag_use_existing_template
set xgettext_args --use-existing-template=$_flag_use_existing_template
end
$build_tools/fish_xgettext.fish $xgettext_args >$template_file
or exit 1
end