-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathi18n.sh
More file actions
executable file
·85 lines (76 loc) · 2.13 KB
/
i18n.sh
File metadata and controls
executable file
·85 lines (76 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env bash
# Translation helper for the Perry docs.
#
# Usage:
# ./docs/i18n.sh extract Regenerate po/messages.pot from English source
# ./docs/i18n.sh sync Merge .pot into every po/<lang>.po
# ./docs/i18n.sh add <lang> Create po/<lang>.po (e.g. ./docs/i18n.sh add zh-CN)
# ./docs/i18n.sh build <lang> Build a single language into book/<lang>/
# ./docs/i18n.sh build-all Build English + every po/<lang>.po into book/
set -euo pipefail
cd "$(dirname "$0")"
cmd=${1:-help}
require() {
command -v "$1" >/dev/null 2>&1 || { echo "missing tool: $1" >&2; exit 1; }
}
extract() {
require mdbook
require mdbook-xgettext
rm -rf /tmp/perry-i18n-extract
MDBOOK_OUTPUT__XGETTEXT__POT_FILE=messages.pot \
mdbook build -d /tmp/perry-i18n-extract >/dev/null
mkdir -p po
cp /tmp/perry-i18n-extract/xgettext/messages.pot po/messages.pot
rm -rf /tmp/perry-i18n-extract
echo "wrote po/messages.pot"
}
sync_all() {
require msgmerge
for f in po/*.po; do
[ -e "$f" ] || continue
msgmerge --quiet --update --backup=none "$f" po/messages.pot
echo "synced $f"
done
}
add_lang() {
require msginit
local lang=${1:?usage: i18n.sh add <lang>}
local out="po/${lang}.po"
if [ -e "$out" ]; then
echo "$out already exists" >&2
exit 1
fi
[ -e po/messages.pot ] || extract
msginit -i po/messages.pot -l "${lang}.UTF-8" -o "$out" --no-translator
echo "created $out — translate msgstr entries and submit a PR"
}
build_one() {
require mdbook
require mdbook-gettext
local lang=${1:?usage: i18n.sh build <lang>}
if [ "$lang" = "en" ]; then
mdbook build -d book
else
[ -e "po/${lang}.po" ] || { echo "po/${lang}.po not found" >&2; exit 1; }
MDBOOK_BOOK__LANGUAGE="$lang" mdbook build -d "book/${lang}"
fi
}
build_all() {
build_one en
for f in po/*.po; do
[ -e "$f" ] || continue
local lang
lang=$(basename "$f" .po)
build_one "$lang"
done
}
case "$cmd" in
extract) extract ;;
sync) sync_all ;;
add) add_lang "${2:-}" ;;
build) build_one "${2:-}" ;;
build-all) build_all ;;
*)
sed -n '2,9p' "$0"
exit 1 ;;
esac