Fix error reporting
[tails.git] / refresh-translations
blobb837e530d982c011ecc8feea262c79313f8ac024
1 #! /bin/bash
3 set -e
4 set -u
6 PERL_PROGS="/usr/local/bin/tails-security-check \
7 /usr/local/lib/tails-htp-notify-user \
8 $(find \
9 config/chroot_local-includes/usr/src/iuk/bin \
10 -type f \
11 | perl -p -E 's{^config/chroot_local-includes}{}' \
12 ) \
13 $(find \
14 config/chroot_local-includes/usr/src/iuk/lib \
15 config/chroot_local-includes/usr/src/perl5lib/lib \
16 -type f -name '*.pm' \
17 | perl -p -E 's{^config/chroot_local-includes}{}' \
19 PYTHON_PROGS="\
20 /usr/local/bin/electrum \
21 /usr/local/bin/replace-su-with-sudo \
22 /usr/local/bin/tails-about \
23 /usr/local/bin/tails-additional-software-config \
24 /usr/local/bin/tails-screen-locker \
25 /usr/local/bin/tails-upgrade-frontend-wrapper \
26 /usr/lib/python3/dist-packages/additional_software.py \
27 /usr/lib/python3/dist-packages/tails_installer/creator.py \
28 /usr/lib/python3/dist-packages/tails_installer/gui.py \
29 /usr/lib/python3/dist-packages/tails_installer/passphrase_dialog.py \
30 /usr/lib/python3/dist-packages/tails_installer/source.py \
31 /usr/lib/python3/dist-packages/tails_installer/utils.py \
32 /usr/lib/python3/dist-packages/tailsgreeter/settings/persistence.py \
33 /usr/lib/python3/dist-packages/tailsgreeter/ui/add_settings_dialog.py \
34 /usr/lib/python3/dist-packages/tailsgreeter/ui/additional_settings.py \
35 /usr/lib/python3/dist-packages/tailsgreeter/ui/main_window.py \
36 /usr/lib/python3/dist-packages/tailsgreeter/ui/persistent_storage.py \
37 /usr/lib/python3/dist-packages/tailsgreeter/ui/region_settings.py \
38 /usr/lib/python3/dist-packages/tca/config.py \
39 /usr/lib/python3/dist-packages/tca/ui/main_window.py \
40 /usr/lib/python3/dist-packages/tps/configuration/binding.py \
41 /usr/lib/python3/dist-packages/tps/device.py \
42 /usr/lib/python3/dist-packages/tps_frontend/application.py \
43 /usr/lib/python3/dist-packages/tps_frontend/change_passphrase_dialog.py \
44 /usr/lib/python3/dist-packages/tps_frontend/error_dialog.py \
45 /usr/lib/python3/dist-packages/tps_frontend/feature.py \
46 /usr/lib/python3/dist-packages/tps_frontend/passphrase_strength_hint.py \
47 /usr/lib/python3/dist-packages/tps_frontend/window.py \
48 /usr/lib/python3/dist-packages/unlock_veracrypt_volumes/volume.py \
49 /usr/lib/python3/dist-packages/unlock_veracrypt_volumes/volume_list.py \
50 /usr/lib/python3/dist-packages/unlock_veracrypt_volumes/volume_manager.py \
51 /usr/lib/python3/dist-packages/whisperBack/exceptions.py \
52 /usr/lib/python3/dist-packages/whisperBack/gui.py \
53 /usr/lib/python3/dist-packages/whisperBack/whisperback.py \
54 /usr/local/lib/additional-software/asp-handle-package-changes \
55 /usr/local/lib/additional-software/asp-install \
56 /usr/local/lib/tails-additional-software-notify \
57 /usr/local/lib/tails-virt-notify-user"
58 SHELL_PROGS=" \
59 /usr/local/bin/keepassxc \
60 /usr/local/bin/tca \
61 /usr/local/lib/tails-spoof-mac \
62 /usr/local/lib/tails-report-disk-errors \
63 /usr/local/bin/thunderbird \
64 /usr/bin/tor-browser \
65 /usr/local/sbin/unsafe-browser"
66 JAVASCRIPT_PROGS=" \
67 /usr/share/gnome-shell/extensions/status-menu-helper@tails.boum.org/extension.js \
68 /usr/share/gnome-shell/extensions/torstatus@tails.boum.org/extension.js \
70 GLADE_UI="\
71 /usr/share/tails/additional-software/configuration-window.ui \
74 LOCALE_BASEDIR=config/chroot_local-includes/usr/share/locale
76 ### External libraries
78 . config/chroot_local-includes/usr/local/lib/tails-shell-library/common.sh
79 . config/chroot_local-includes/usr/local/lib/tails-shell-library/po.sh
81 ### Functions
83 normalize_pot () {
84 sed --regexp-extended \
85 -i 's@^"Content-Type: text/plain; charset=CHARSET\\n"@"Content-Type: text/plain; charset=UTF-8\\n"@' \
86 "${@}"
89 prog_potfile () {
90 prog=$1
92 progpath="config/chroot_local-includes$prog"
93 case $prog in
94 /usr/share/gnome-shell/extensions/status-menu-helper@tails.boum.org/extension.js)
95 domain=status-menu-helper-extension.js
97 /usr/share/gnome-shell/extensions/torstatus@tails.boum.org/extension.js)
98 domain=torstatus-extension.js
100 /usr/lib/python3/dist-packages/tps/*)
101 domain="tps-$(basename "$prog")"
103 /usr/lib/python3/dist-packages/tps_frontend/*)
104 domain="tps-frontend-$(basename "$prog")"
106 /usr/lib/python3/dist-packages/unlock_veracrypt_volumes/*)
107 domain="unlock-veracrypt-volumes-$(basename "$prog")"
109 /usr/lib/python3/dist-packages/tailsgreeter/*)
110 domain="greeter-$(basename "$prog")"
112 /usr/lib/python*/dist-packages/tails_installer/*)
113 domain="installer-$(basename "$prog")"
115 /usr/lib/python*/dist-packages/tca/*)
116 domain="tca-$(basename "$prog")"
118 /usr/local/bin/tca)
119 domain="tca-bin"
121 /usr/lib/python3/dist-packages/whisperBack/*)
122 domain="whisperBack-$(basename "$prog")"
125 domain=$(basename "$prog")
127 esac
128 echo "tmp/pot/${domain}.pot"
131 create_pot () {
132 prog=$1
133 proglang=$2
134 progpath="config/chroot_local-includes${prog}"
135 if [ -e "${progpath}" ]; then
136 pot="$(prog_potfile "${prog}")"
137 mkdir -p "$(dirname "${pot}")"
138 case "$proglang" in
139 Perl)
140 # XXX: once we can assume libintl-perl 1.28+,
141 # generate this list of options with:
142 # perl -MLocale::TextDomain -E 'print Locale::TextDomain->options'
143 xgettext_args='--keyword'
144 xgettext_args="$xgettext_args --keyword=__"
145 xgettext_args="$xgettext_args --keyword='\$__'"
146 xgettext_args="$xgettext_args --keyword=__x"
147 xgettext_args="$xgettext_args --keyword=__n:1,2"
148 xgettext_args="$xgettext_args --keyword=__nx:1,2"
149 xgettext_args="$xgettext_args --keyword=__xn:1,2"
150 xgettext_args="$xgettext_args --keyword=__p:1c,2"
151 xgettext_args="$xgettext_args --keyword=__np:1c,2,3"
152 xgettext_args="$xgettext_args --keyword=__npx:1c,2,3"
153 xgettext_args="$xgettext_args --keyword=N__"
154 xgettext_args="$xgettext_args --keyword=N__n:1,2"
155 xgettext_args="$xgettext_args --keyword=N__p:1c,2"
156 xgettext_args="$xgettext_args --keyword=N__np:1c,2,3"
157 xgettext_args="$xgettext_args --keyword=%__'"
160 xgettext_args=
162 esac
163 # shellcheck disable=SC2086
164 xgettext --language="${proglang}" --from-code=UTF-8 \
165 $xgettext_args \
166 --add-comments="Translators:" -o "${pot}" "${progpath}"
167 if [ -f "${pot}" ]; then
168 normalize_pot "${pot}"
170 else
171 echo "error: We are supposed to create a POT file for '${prog}'" \
172 "but '${progpath}' does not exist"
173 exit 1
177 po_file () {
178 locale=$1
180 echo "po/${locale}.po"
183 mo_file () {
184 locale=$1
186 echo "${LOCALE_BASEDIR}/${locale}/LC_MESSAGES/tails.mo"
189 refresh_mo () {
190 for locale in "$@" ; do
191 po=$(po_file "$locale")
192 mo=$(mo_file "$locale")
193 mkdir -p "$(dirname "$mo")"
194 if ! msgfmt --check -o "${mo}" "${po}"; then
195 echo "Errors for locale '${locale}': restoring old file"
196 if [ -n "$(git ls-files "${po}")" ]; then
197 git restore "${po}"
198 msgfmt --check -o "${mo}" "${po}"
199 else
200 rm "${po}"
203 done
206 no_left_out_files () {
207 (cd po && intltool-update --maintain)
208 [ ! -e po/missing ] || return 1
209 return 0
212 intltool_update_pot () {
214 cd po
215 cp -a tails.pot tails.pot.orig
216 intltool-update --pot --gettext-package=tails
217 normalize_pot tails.pot tails.pot.orig
219 if [ "${FORCE:-}" = yes ]; then
220 echo "Force-updating 'tails.pot'."
221 rm tails.pot.orig
222 elif diff_without_pot_creation_date_and_comments -q tails.pot.orig tails.pot; then
223 echo "Only header or comment changes in tails.pot: keeping the old one"
224 mv tails.pot.orig tails.pot
225 else
226 echo "Real changes in tails.pot: switching to the updated one"
227 rm tails.pot.orig
232 intltool_merge_desktop () {
233 extract_from_file_between_markers po/POTFILES.in \
234 '^# Files updated by intltool-update --desktop-style' '^$' \
235 | while read -r infile ; do
236 intltool-merge --quiet --desktop-style --utf8 \
237 po "$infile" "${infile%.in}"
238 done
241 intltool_merge_xml () {
242 extract_from_file_between_markers po/POTFILES.in \
243 '^# Files updated by intltool-update --xml-style' '^$' \
244 | while read -r infile ; do
245 intltool-merge --quiet --xml-style --utf8 \
246 po "$infile" "${infile%.in}"
247 done
250 ### Main
252 FORCE=no
253 KEEP_TMP_POT=no
254 while [ -n "${1:-}" ]; do
255 case "${1:-}" in
256 '--force')
257 FORCE=yes
259 '--keep-tmp-pot')
260 KEEP_TMP_POT=yes
263 echo "Unknown option: ${1}"
264 exit 1
266 esac
267 shift
268 done
270 # Schedule clean up
271 # shellcheck disable=SC2064
272 trap "rm -fr po/*.new po/*.orig ; [ '$KEEP_TMP_POT' = yes ] || rm -fr tmp/pot" EXIT
273 trap "echo refresh-translations: failed" ERR
275 # Update POT files
276 mkdir -p tmp/pot
277 for prog in $PERL_PROGS ; do create_pot "$prog" Perl ; done
278 for prog in $PYTHON_PROGS ; do create_pot "$prog" Python ; done
279 for prog in $SHELL_PROGS ; do create_pot "$prog" Shell ; done
280 for prog in $JAVASCRIPT_PROGS ; do create_pot "$prog" JavaScript ; done
281 for prog in $GLADE_UI ; do create_pot "$prog" Glade ; done
282 intltool_update_pot
284 # If left out files are detected, intltool-update --maintain writes
285 # them to po/missing.
286 if ! no_left_out_files; then
287 echo "E: These files should be listed in POTFILES.in or POTFILES.skip:" >&2
288 cat po/missing
289 exit 3
292 # Update PO files
293 # shellcheck disable=SC2046
294 intltool_update_po $(po_languages)
296 # Update files that are actually used at runtime
297 # shellcheck disable=SC2046
298 refresh_mo $(po_languages)
299 intltool_merge_desktop
300 intltool_merge_xml