2017-05-28 05:13:18 +00:00
|
|
|
#!/usr/bin/env fish
|
|
|
|
#
|
2019-09-21 14:24:11 +00:00
|
|
|
# Tool to generate messages.pot
|
|
|
|
|
2024-03-05 16:24:17 +00:00
|
|
|
# Create temporary directory for these operations. OS X `mktemp` is somewhat restricted, so this block
|
|
|
|
# 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)
|
|
|
|
or exit 1
|
|
|
|
|
|
|
|
# This is a gigantic crime.
|
|
|
|
# xgettext still does not support rust *at all*, so we use cargo-expand to get all our wgettext invocations.
|
|
|
|
set -l expanded (cargo expand --lib; for f in fish{,_indent,_key_reader}; cargo expand --bin $f; end)
|
|
|
|
|
|
|
|
# Extract any gettext call
|
|
|
|
set -l strs (printf '%s\n' $expanded | grep -A1 wgettext_static_str |
|
|
|
|
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 "\'" "'" | sort -u)
|
2017-05-28 05:13:18 +00:00
|
|
|
|
2024-03-05 16:24:17 +00:00
|
|
|
# Extract any constants
|
|
|
|
set -a strs (string match -rv 'BUILD_VERSION:|PACKAGE_NAME' -- $expanded |
|
|
|
|
string match -rg 'const [A-Z_]*: &str = "(.*)"' | string replace -a "\'" "'")
|
|
|
|
|
|
|
|
# We construct messages.pot ourselves instead of forcing this into msgmerge or whatever.
|
|
|
|
# The escaping so far works out okay.
|
|
|
|
for str in $strs
|
|
|
|
# grep -P needed for string escape to be compatible (PCRE-style),
|
|
|
|
# -H gives the filename, -n the line number.
|
|
|
|
# If you want to run this on non-GNU grep: Don't.
|
|
|
|
echo "#:" (grep -PHn -r -- (string escape --style=regex -- $str) src/ |
|
|
|
|
head -n1 | string replace -r ':\s.*' '')
|
|
|
|
echo "msgid \"$str\""
|
|
|
|
echo 'msgstr ""'
|
|
|
|
end >messages.pot
|
2019-09-21 14:24:11 +00:00
|
|
|
|
2017-05-28 05:13:18 +00:00
|
|
|
# This regex handles descriptions for `complete` and `function` statements. These messages are not
|
|
|
|
# particularly important to translate. Hence the "implicit" label.
|
2020-05-15 05:56:06 +00:00
|
|
|
set -l implicit_regex '(?:^| +)(?:complete|function).*? (?:-d|--description) (([\'"]).+?(?<!\\\\)\\2).*'
|
2017-05-28 05:13:18 +00:00
|
|
|
|
|
|
|
# This regex handles explicit requests to translate a message. These are more important to translate
|
|
|
|
# than messages which should be implicitly translated.
|
2020-05-15 05:56:06 +00:00
|
|
|
set -l explicit_regex '.*\( *_ (([\'"]).+?(?<!\\\\)\\2) *\).*'
|
2017-05-28 05:13:18 +00:00
|
|
|
|
2020-11-13 03:14:18 +00:00
|
|
|
mkdir -p $tmpdir/implicit/share/completions $tmpdir/implicit/share/functions
|
|
|
|
mkdir -p $tmpdir/explicit/share/completions $tmpdir/explicit/share/functions
|
2017-05-28 05:13:18 +00:00
|
|
|
|
|
|
|
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")`.
|
2024-03-05 16:24:17 +00:00
|
|
|
string replace --filter --regex $explicit_regex '$1' <$f | string unescape \
|
|
|
|
| string replace --all '"' '\\"' | string replace -r '(.*)' 'N_ "$1"' >$tmpdir/explicit/$f
|
2017-05-28 05:13:18 +00:00
|
|
|
|
|
|
|
# Handle `complete` / `function` description messages. The `| fish` is subtle. It basically
|
2019-11-25 11:03:25 +00:00
|
|
|
# avoids the need to use `source` with a command substitution that could affect the current
|
2017-05-28 05:13:18 +00:00
|
|
|
# shell.
|
2024-03-05 16:24:17 +00:00
|
|
|
string replace --filter --regex $implicit_regex '$1' <$f | string unescape \
|
|
|
|
| string replace --all '"' '\\"' | string replace -r '(.*)' 'N_ "$1"' >$tmpdir/implicit/$f
|
2017-05-28 05:13:18 +00:00
|
|
|
end
|
2019-09-21 14:24:11 +00:00
|
|
|
|
2024-03-05 16:24:17 +00:00
|
|
|
xgettext -j -k -kN_ -LShell --from-code=UTF-8 -cDescription --no-wrap -o messages.pot $tmpdir/{ex,im}plicit/share/*/*.fish
|
2019-09-21 14:24:11 +00:00
|
|
|
|
2024-03-10 15:40:34 +00:00
|
|
|
# Remove the tmpdir from the location to avoid churn
|
|
|
|
sed -i 's_^#: /.*/share/_#: share/_' messages.pot
|
|
|
|
|
2020-11-13 03:14:18 +00:00
|
|
|
rm -r $tmpdir
|