#!/usr/bin/env bash

# See https://zulip.readthedocs.io/en/latest/translating/internationalization.html
# for background on this subsystem.

set -e
set -x

./manage.py makemessages --all
tx pull -a -f --mode=translator --minimum-perc=5 "$@"
# For readability, we prefer UTF-8, not ascii, in these JSON files.
find ./locale \
    -regextype sed \
    -regex '^\./locale/.*/\(mobile\|translations\).json$' \
    -exec ./tools/i18n/unescape-contents {} \;

# Normalize to Unicode Normalization Form C.
files="$(find locale -type f -name '*.json' -o -name '*.po')"
mapfile -t files <<<"$files"
for file in "${files[@]}"; do
    uconv -x any-nfc "$file" | sponge -- "$file"
done

./tools/i18n/update-for-legacy-translations

./manage.py compilemessages --ignore='*'
./tools/i18n/process-mobile-i18n
