Extract function for gettext extraction

Extracting explicit and implicit messages works essentially the same way, which
is also reflected in the code being identical, except for the regex.

Extract the duplicated code into a function.
This commit is contained in:
Daniel Rainer
2025-05-03 15:31:23 +02:00
parent 0d59e89374
commit d5e80d43d9

View File

@@ -6,7 +6,7 @@
# works around that - based on share/functions/funced.fish.
set -q TMPDIR
or set -l TMPDIR /tmp
set -l tmpdir (mktemp -d $TMPDIR/fish.XXXXXX)
set tmpdir (mktemp -d $TMPDIR/fish.XXXXXX)
or exit 1
# This is a gigantic crime.
@@ -36,29 +36,23 @@ for str in $strs
echo 'msgstr ""'
end >messages.pot
function extract_fish_script_messages --argument-names name regex;
mkdir -p $tmpdir/$name/share/completions $tmpdir/$name/share/functions
for f in share/config.fish share/completions/*.fish share/functions/*.fish
string replace --filter --regex $regex '$1' <$f | string unescape \
| string replace --all '"' '\\"' | string replace -r '(.*)' 'N_ "$1"' >$tmpdir/$name/$f
end
end
# This regex handles descriptions for `complete` and `function` statements. These messages are not
# particularly important to translate. Hence the "implicit" label.
set -l implicit_regex '(?:^| +)(?:complete|function).*? (?:-d|--description) (([\'"]).+?(?<!\\\\)\\2).*'
extract_fish_script_messages implicit $implicit_regex
# This regex handles explicit requests to translate a message. These are more important to translate
# than messages which should be implicitly translated.
set -l explicit_regex '.*\( *_ (([\'"]).+?(?<!\\\\)\\2) *\).*'
mkdir -p $tmpdir/implicit/share/completions $tmpdir/implicit/share/functions
mkdir -p $tmpdir/explicit/share/completions $tmpdir/explicit/share/functions
for f in share/config.fish share/completions/*.fish share/functions/*.fish
# Extract explicit attempts to translate a message. That is, those that are of the form
# `(_ "message")`.
string replace --filter --regex $explicit_regex '$1' <$f | string unescape \
| string replace --all '"' '\\"' | string replace -r '(.*)' 'N_ "$1"' >$tmpdir/explicit/$f
# Handle `complete` / `function` description messages. The `| fish` is subtle. It basically
# avoids the need to use `source` with a command substitution that could affect the current
# shell.
string replace --filter --regex $implicit_regex '$1' <$f | string unescape \
| string replace --all '"' '\\"' | string replace -r '(.*)' 'N_ "$1"' >$tmpdir/implicit/$f
end
extract_fish_script_messages explicit $explicit_regex
xgettext -j -k -kN_ -LShell --from-code=UTF-8 -cDescription --no-wrap -o messages.pot $tmpdir/{ex,im}plicit/share/*/*.fish