aboutsummaryrefslogtreecommitdiff
path: root/aux/workflow/l10n.sh
blob: cb132a3cc684062dc3945734964013952c348dae (about) (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
set -eu

LANGS=
MAX_JOBS=64
while getopts 'l:L:j:' flag; do
	case "$flag" in
		l)
			LANGS="$OPTARG"
			;;
		L)
			CONTRIBLANGS="$OPTARG"
			;;
		j)
			MAX_JOBS="$OPTARG"
			;;
		*)
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

assert_arg() {
	if [ -z "$1" ]; then
		echo "Missing $2" >&2
		exit 2
	fi
}

assert_arg "${LANGS:-}" '-l LANGS'

PARALLEL_N=0
parallel_run() {
	{
		"$@"
	} &
	PARALLEL_N=$((PARALLEL_N + 1))
	if [ "$PARALLEL_N" = "$MAX_JOBS" ]; then
		wait
		PARALLEL_N=0
	fi
}

po_run() {
	from_f="$1"
	lang="$2"
	to_f="$(echo "$from_f" | sed "s/en\./$lang./")"
	printf 'Generating %s...\n' "$to_f" >&2
	pofile="po/LC_MESSAGES/$from_f/$lang.po"
	mkdir -p "$(dirname "$pofile")"

	case "$from_f" in
		*.en.[1-9].in)
			po4a-updatepo -f man -m "$from_f" -p "$pofile"
			po4a-translate -f man -m "$from_f" \
				-p "$pofile" -l "$to_f" -k 0 -v >&2
			;;
		*.en.html)
			po4a-updatepo -f xhtml -m "$from_f" -p "$pofile"
			po4a-translate -f xhtml -m "$from_f" \
				-p "$pofile" -l "$to_f" -k 0 -v >&2
			;;
		*.en.md)
			touch "$pofile"
			md2po --include-codeblocks --quiet --save \
				--po-filepath "$pofile" < "$from_f"
			po2md --pofiles "$pofile" --save "$to_f" \
				--quiet --wrapwidth 999 < "$from_f"
			;;
		*.en.msg|*.en.txt)
			po4a-updatepo -f text -m "$from_f" -p "$pofile"
			po4a-translate -f text -m "$from_f" \
				-p "$pofile" -l "$to_f" -k 0 -v >&2
			;;
		*)
			echo "Unsupported file format: $from_f" >&2
			exit 2
			;;
	esac
}

for from_f in "$@"; do
	for lang in $LANGS ${CONTRIBLANGS:-}; do
		parallel_run po_run "$from_f" "$lang"
	done
done

EXIT_CODE=0

end="\033[0m"
yellowb="\033[1;33m"
for lang in $LANGS; do
	# shellcheck disable=2044
	for pofile in $(find po/ -type f -name "$lang.po"); do
		if ! LANG=POSIX msgfmt --statistics "$pofile" 2>&1 |
				grep untranslated; then
			continue
		fi
		# shellcheck disable=2059
		printf "\n  ${yellowb}WARNING${end}!" >&2
		printf "\n  Missing translations for %s\n\n" "$pofile" >&2
		EXIT_CODE=1
	done
done

if [ -n "${ASSERT_NO_MISSING_TRANSLATIONS:-}" ]; then
	exit "$EXIT_CODE"
fi