Merge branch '20384-thunderbird-temporarily-disable-pdfjs' into 'stable'
[tails.git] / import-translations
blobbb6989ae82f42783a75dea522af616e636bfee3c
1 #!/bin/sh
3 set -e
4 set -u
6 TAILS_PO_DIR='po'
7 SCRIPT_DIR=$(readlink -f "$(dirname "$0")")
8 TOR_TRANSLATION_REMOTE='https://gitlab.torproject.org/tpo/translation.git'
9 TOR_TRANSLATION_DIR="$SCRIPT_DIR/tmp/tor-translation"
10 GIT_IN_TOR_TRANSLATION_DIR="git \
11 --work-tree=\"$TOR_TRANSLATION_DIR\" \
12 --git-dir=\"$TOR_TRANSLATION_DIR/.git\""
13 MINIMUM_PERCENT=25
15 ### External libraries
16 . "$SCRIPT_DIR/config/chroot_local-includes/usr/local/lib/tails-shell-library/po.sh"
18 # Defaults
19 LANG_DOT_PO_LAYOUT=yes
21 # Detect which project is in current folder,
22 # and set parameters accordingly
23 # shellcheck disable=SC2016
24 if [ -f 'po/tails.pot' ]; then
25 POTFILE='po/tails.pot'
26 BRANCH='tails-misc'
27 AFTER_IMPORT='intltool_update_po $(po_languages)'
28 elif [ -f 'po/onioncircuits.pot' ]; then
29 POTFILE='po/onioncircuits.pot'
30 LANG_DOT_PO_LAYOUT=no
31 BRANCH='tails-onioncircuits_release'
32 AFTER_IMPORT='./setup.py build_i18n && ( cd po && for po in *.po ; do msgmerge --update "$po" onioncircuits.pot ; done )'
33 else
34 echo "Current folder is not a project known to this script!"
35 exit 1
38 TOTAL=$(msgattrib --no-obsolete "$POTFILE" | count_msgids)
40 # Clone or update the translation repository
41 if [ -d "$TOR_TRANSLATION_DIR" ]; then
42 eval "$GIT_IN_TOR_TRANSLATION_DIR remote set-url origin ${TOR_TRANSLATION_REMOTE}"
43 eval "$GIT_IN_TOR_TRANSLATION_DIR fetch origin"
44 else
45 mkdir -p "$SCRIPT_DIR/tmp"
46 git clone "$TOR_TRANSLATION_REMOTE" "$TOR_TRANSLATION_DIR"
49 # Checkout the correct branch
50 eval "$GIT_IN_TOR_TRANSLATION_DIR checkout \"$BRANCH\""
51 eval "$GIT_IN_TOR_TRANSLATION_DIR reset --hard \"origin/$BRANCH\""
53 # Ensure we only keep PO files that are still present in the
54 # branch we import from.
55 find "$TAILS_PO_DIR" -name '*.po' -delete
57 po_import_filter() {
58 po_file="$1"
59 lang="$2"
61 # Always import the default locale, whose .mo file is needed
62 # at least by tailsgreeter.config.set_current_language.
63 if [ "$lang" = "en" ] || [ "$lang" = "en_US" ]; then
64 return 0
67 translated_strings=$(count_translated_strings < "$po_file")
68 if [ $((100*translated_strings/TOTAL)) -lt "$MINIMUM_PERCENT" ]; then
69 echo "Skipping $lang, that has less than $MINIMUM_PERCENT translated strings."
70 return 1
73 return 0
76 # Import PO files that have at least $MINIMUM_PERCENT % of strings translated
77 if [ "$LANG_DOT_PO_LAYOUT" = yes ] ; then
78 find "$TOR_TRANSLATION_DIR" -name '*.po' | sort | while read -r po_file; do
79 lang=$(basename "$po_file" | tr - _ | sed 's/\.po$//')
80 po_import_filter "$po_file" "$lang" || continue
81 echo "Importing translation for $lang..."
82 cp "$po_file" "$TAILS_PO_DIR"
83 done
84 else
85 find "$TOR_TRANSLATION_DIR" -name '*.pot' | sort | while read -r po_file; do
86 lang=$(basename "$(dirname "$po_file" | tr - _ | sed 's/\.pot$//')")
87 po_import_filter "$po_file" "$lang" || continue
88 echo "Importing translation for $lang..."
89 cp "$po_file" "$TAILS_PO_DIR/${lang}.po"
90 done
93 # Update PO files
94 if [ -n "${AFTER_IMPORT:-}" ]; then
95 eval "$AFTER_IMPORT"