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
|
2020-07-05 06:24:12 +00:00
|
|
|
# Extended to replace the old Makefile rule which did not port easily to CMake
|
2019-09-21 14:24:11 +00:00
|
|
|
|
2017-05-28 05:13:18 +00:00
|
|
|
# This script was originally motivated to work around a quirk (or bug depending on your viewpoint)
|
|
|
|
# of the xgettext command. See https://lists.gnu.org/archive/html/bug-gettext/2014-11/msg00006.html.
|
|
|
|
# However, it turns out that even if that quirk did not exist we would still need something like
|
|
|
|
# this script to properly extract descriptions. That's because we need to normalize the strings to
|
|
|
|
# a format that xgettext will handle correctly. Also, `xgettext -LShell` doesn't correctly extract
|
|
|
|
# all the strings we want translated. So we extract and normalize all such strings into a format
|
|
|
|
# that `xgettext` can handle.
|
|
|
|
|
2019-09-21 14:24:11 +00:00
|
|
|
# Start with the C++ source
|
|
|
|
xgettext -k -k_ -kN_ -LC++ --no-wrap -o messages.pot src/*.cpp src/*.h
|
|
|
|
|
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
|
|
|
# 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
|
2019-09-21 14:24:11 +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")`.
|
2020-11-13 03:14:18 +00:00
|
|
|
string replace --filter --regex $explicit_regex 'echo $1' <$f | fish >$tmpdir/explicit/$f.tmp 2>/dev/null
|
2017-05-28 05:13:18 +00:00
|
|
|
while read description
|
|
|
|
echo 'N_ "'(string replace --all '"' '\\"' -- $description)'"'
|
2020-11-13 03:14:18 +00:00
|
|
|
end <$tmpdir/explicit/$f.tmp >$tmpdir/explicit/$f
|
|
|
|
rm $tmpdir/explicit/$f.tmp
|
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.
|
2020-11-13 03:14:18 +00:00
|
|
|
string replace --filter --regex $implicit_regex 'echo $1' <$f | fish >$tmpdir/implicit/$f.tmp 2>/dev/null
|
2017-05-28 05:13:18 +00:00
|
|
|
while read description
|
|
|
|
# We don't use `string escape` as shown in the next comment because it produces output that
|
|
|
|
# is not parsed correctly by xgettext. Instead just escape double-quotes and quote the
|
|
|
|
# resulting string.
|
|
|
|
echo 'N_ "'(string replace --all '"' '\\"' -- $description)'"'
|
2020-11-13 03:14:18 +00:00
|
|
|
end <$tmpdir/implicit/$f.tmp >$tmpdir/implicit/$f
|
|
|
|
rm $tmpdir/implicit/$f.tmp
|
2017-05-28 05:13:18 +00:00
|
|
|
end
|
2019-09-21 14:24:11 +00:00
|
|
|
|
2020-11-13 03:14:18 +00:00
|
|
|
xgettext -j -k -kN_ -LShell --from-code=UTF-8 -cDescription --no-wrap -o messages.pot $tmpdir/explicit/share/*/*.fish
|
|
|
|
xgettext -j -k -kN_ -LShell --from-code=UTF-8 -cDescription --no-wrap -o messages.pot $tmpdir/implicit/share/*/*.fish
|
2019-09-21 14:24:11 +00:00
|
|
|
|
2020-11-13 03:14:18 +00:00
|
|
|
rm -r $tmpdir
|