|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -euo pipefail |
3 | 3 |
|
4 | | -cd $(dirname "$0")/.. |
| 4 | +cd "$(dirname "$0")/.." |
5 | 5 |
|
6 | | -function update_lang() { |
7 | | - file=${1} |
| 6 | +usage() { |
| 7 | + echo "Usage: ${0} <command>" |
| 8 | + echo "" |
| 9 | + echo "Commands:" |
| 10 | + echo " all Regenerate base.pot and update all languages" |
| 11 | + echo " <lang> Regenerate base.pot and update a single language" |
| 12 | + echo " check Run translation validation checks" |
| 13 | + echo " -h, --help Show this help" |
| 14 | +} |
| 15 | + |
| 16 | +generate_pot() { |
| 17 | + find . -type f -iname '*.py' | sort \ |
| 18 | + | xargs xgettext --no-location --omit-header --keyword='tr' \ |
| 19 | + -d base -o locales/base.pot |
| 20 | +} |
8 | 21 |
|
| 22 | +update_lang() { |
| 23 | + local file=${1} |
9 | 24 | echo "Updating: ${file}" |
| 25 | + local path |
10 | 26 | path=$(dirname "${file}") |
11 | 27 | msgmerge --quiet --no-location --width 512 --backup none --update "${file}" locales/base.pot |
12 | 28 | msgfmt -o "${path}/base.mo" "${file}" |
13 | 29 | } |
14 | 30 |
|
15 | | - |
16 | | -function generate_all() { |
| 31 | +cmd_generate_all() { |
| 32 | + generate_pot |
17 | 33 | for file in $(find locales/ -name "base.po"); do |
18 | 34 | update_lang "${file}" |
19 | 35 | done |
20 | 36 | } |
21 | 37 |
|
22 | | -function generate_single_lang() { |
23 | | - lang_file="locales/${1}/LC_MESSAGES/base.po" |
24 | | - |
| 38 | +cmd_generate_single() { |
| 39 | + local lang_file="locales/${1}/LC_MESSAGES/base.po" |
25 | 40 | if [ ! -f "${lang_file}" ]; then |
26 | 41 | echo "Language files not found: ${lang_file}" |
27 | 42 | exit 1 |
28 | 43 | fi |
29 | | - |
| 44 | + generate_pot |
30 | 45 | update_lang "${lang_file}" |
31 | 46 | } |
32 | 47 |
|
| 48 | +cmd_check_po_syntax() { |
| 49 | + echo "Checking .po syntax..." |
| 50 | + local failed=0 |
| 51 | + while IFS= read -r po; do |
| 52 | + if ! msgfmt --check --output-file=/dev/null "$po" 2>&1; then |
| 53 | + echo "FAIL: $po" |
| 54 | + failed=1 |
| 55 | + fi |
| 56 | + done < <(find locales/ -name '*.po') |
| 57 | + if [ "$failed" -eq 1 ]; then |
| 58 | + echo "ERROR: some .po files have syntax errors" >&2 |
| 59 | + return 1 |
| 60 | + fi |
| 61 | + echo "All .po files passed syntax check." |
| 62 | +} |
| 63 | + |
| 64 | +cmd_check_no_tr_fstring() { |
| 65 | + echo "Checking for tr(f-string) anti-pattern..." |
| 66 | + if grep -rnE "tr\(\s*f['\"]" . --include='*.py'; then |
| 67 | + echo "ERROR: use tr('...{}').format(...) instead of tr(f'...')" >&2 |
| 68 | + return 1 |
| 69 | + fi |
| 70 | + echo "No tr(f-string) anti-pattern found." |
| 71 | +} |
| 72 | + |
| 73 | +cmd_check_pot_freshness() { |
| 74 | + # msgcmp (not diff) because base.pot carries legacy stale entries from |
| 75 | + # --join-existing; diff would always fail until a full cleanup is done. |
| 76 | + echo "Checking base.pot for missing strings..." |
| 77 | + find . -type f -iname '*.py' | sort \ |
| 78 | + | xargs xgettext --no-location --omit-header --keyword='tr' \ |
| 79 | + -d base -o /tmp/generated.pot |
| 80 | + if ! msgcmp --use-untranslated locales/base.pot /tmp/generated.pot; then |
| 81 | + echo "ERROR: base.pot is missing strings - run: locales_generator.sh all" >&2 |
| 82 | + return 1 |
| 83 | + fi |
| 84 | + echo "base.pot contains all translatable strings." |
| 85 | +} |
| 86 | + |
| 87 | +cmd_check() { |
| 88 | + local failed=0 |
| 89 | + cmd_check_po_syntax || failed=1 |
| 90 | + cmd_check_no_tr_fstring || failed=1 |
| 91 | + cmd_check_pot_freshness || failed=1 |
| 92 | + if [ "$failed" -eq 1 ]; then |
| 93 | + echo "Some translation checks failed." >&2 |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + echo "All translation checks passed." |
| 97 | +} |
33 | 98 |
|
34 | 99 | if [ $# -eq 0 ]; then |
35 | | - echo "Usage: ${0} <language_abbr>" |
36 | | - echo "Special case 'all' for <language_abbr> builds all languages." |
| 100 | + usage |
37 | 101 | exit 1 |
38 | 102 | fi |
39 | 103 |
|
40 | | -lang=${1} |
41 | | - |
42 | | -# Update the base file containing all translatable strings |
43 | | -find . -type f -iname "*.py" | xargs xgettext --join-existing --no-location --omit-header --keyword='tr' -d base -o locales/base.pot |
44 | | - |
45 | | -case "${lang}" in |
46 | | - "all") generate_all;; |
47 | | - *) generate_single_lang "${lang}" |
| 104 | +case "${1}" in |
| 105 | + check) cmd_check ;; |
| 106 | + all) cmd_generate_all ;; |
| 107 | + -h|--help) usage ;; |
| 108 | + *) cmd_generate_single "${1}" ;; |
48 | 109 | esac |
0 commit comments