Replace long list by file

The replaces the `strs` list by a corresponding file, which eliminates the need
for looping over the list.

Use sed to transform strings into gettext po format entries.

Format the file with fish_indent and use more expressive variable name for the
file cargo expand writes to.

Performance improvements (in microseconds):
- sort+format rust strings: from 21750 to 11096 (speedup 2.0)
This commit is contained in:
Daniel Rainer
2025-05-12 20:30:09 +02:00
parent ff5ff50183
commit 1df8fbff67

View File

@@ -15,37 +15,40 @@ begin
echo ""
end >$output_file
set -l tmpfile (mktemp)
set -l cargo_expanded_file (mktemp)
# This is a gigantic crime.
# We use cargo-expand to get all our wgettext invocations.
# This might be replaced once we have a tool which properly handles macro expansions.
begin cargo expand --lib; for f in fish fish_indent fish_key_reader; cargo expand --bin $f; end; end >$tmpfile
begin
cargo expand --lib
for f in fish fish_indent fish_key_reader
cargo expand --bin $f
end
end >$cargo_expanded_file
set -l rust_string_file (mktemp)
# Extract any gettext call
set -l strs (grep -A1 wgettext_static_str <$tmpfile |
grep 'widestring::internals::core::primitive::str =' |
string match -rg '"(.*)"' | string match -rv '^%ls$|^$' |
# escaping difference between gettext and cargo-expand: single-quotes
string replace -a "\'" "'")
grep -A1 wgettext_static_str <$cargo_expanded_file |
grep 'widestring::internals::core::primitive::str =' |
string match -rg '"(.*)"' |
string match -rv '^%ls$|^$' |
# escaping difference between gettext and cargo-expand: single-quotes
string replace -a "\'" "'" >$rust_string_file
# Extract any constants
set -a strs (grep -Ev 'BUILD_VERSION:|PACKAGE_NAME' <$tmpfile |
grep -E 'const [A-Z_]*: &str = "(.*)"' |
sed -E -e 's/^.*const [A-Z_]*: &str = "(.*)".*$/\1/' -e "s_\\\'_'_g")
grep -Ev 'BUILD_VERSION:|PACKAGE_NAME' <$cargo_expanded_file |
grep -E 'const [A-Z_]*: &str = "(.*)"' |
sed -E -e 's/^.*const [A-Z_]*: &str = "(.*)".*$/\1/' -e "s_\\\'_'_g" >>$rust_string_file
rm $tmpfile
rm $cargo_expanded_file
# Sort the extracted strings and remove duplicates.
# This is optional.
set -l strs (string join \n -- $strs | sort -u)
# Then, transform them into the po format
sort -u $rust_string_file |
sed -E 's/^(.*)$/msgid "\1"\nmsgstr ""\n/' >>$output_file
# We construct messages.pot ourselves instead of forcing this into msgmerge or whatever.
# The escaping so far works out okay.
for str in $strs
echo "msgid \"$str\""
echo 'msgstr ""'
echo ""
end >>$output_file
rm $rust_string_file
function extract_fish_script_messages --argument-names regex
@@ -72,7 +75,9 @@ function extract_fish_script_messages --argument-names regex
string unescape |
string replace --all '\\' '\\\\' |
string replace --all '"' '\\"'
end | sort -u | string replace --regex '^(.*)$' 'msgid "$1"'\n'msgstr ""'\n >>$output_file
end |
sort -u |
sed -E 's/^(.*)$/msgid "\1"\nmsgstr ""\n/' >>$output_file
end
# This regex handles explicit requests to translate a message. These are more important to translate