|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +pot_file="i18n/opengothicstarter.pot" |
| 5 | + |
| 6 | +if ! command -v xgettext >/dev/null 2>&1; then |
| 7 | + echo "xgettext is required to validate ${pot_file}." >&2 |
| 8 | + exit 1 |
| 9 | +fi |
| 10 | + |
| 11 | +if ! command -v msgcat >/dev/null 2>&1; then |
| 12 | + echo "msgcat is required to validate ${pot_file}." >&2 |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +if [ ! -f "${pot_file}" ]; then |
| 17 | + echo "Expected POT file not found: ${pot_file}" >&2 |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +mapfile -t source_files < <(find src -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.h' -o -name '*.hh' -o -name '*.hpp' -o -name '*.hxx' \) | sort) |
| 22 | + |
| 23 | +if [ "${#source_files[@]}" -eq 0 ]; then |
| 24 | + echo "No source files found under src/." |
| 25 | + exit 0 |
| 26 | +fi |
| 27 | + |
| 28 | +expected_tmp=$(mktemp) |
| 29 | +current_tmp=$(mktemp) |
| 30 | +expected_normalized_tmp=$(mktemp) |
| 31 | +current_normalized_tmp=$(mktemp) |
| 32 | +cleanup() { |
| 33 | + rm -f "${expected_tmp}" "${current_tmp}" "${expected_normalized_tmp}" "${current_normalized_tmp}" |
| 34 | +} |
| 35 | +trap cleanup EXIT |
| 36 | + |
| 37 | +xgettext \ |
| 38 | + --from-code=UTF-8 \ |
| 39 | + --language=C++ \ |
| 40 | + --keyword=_ \ |
| 41 | + --omit-header \ |
| 42 | + --no-location \ |
| 43 | + -o "${expected_tmp}" \ |
| 44 | + "${source_files[@]}" |
| 45 | + |
| 46 | +msgcat --sort-output --no-location -o "${expected_tmp}" "${expected_tmp}" |
| 47 | +msgcat --sort-output --no-location -o "${current_tmp}" "${pot_file}" |
| 48 | + |
| 49 | +strip_header_entry() { |
| 50 | + local input_file="$1" |
| 51 | + local output_file="$2" |
| 52 | + awk ' |
| 53 | + BEGIN { skipping_preamble = 1; skipping_header = 0; } |
| 54 | + { |
| 55 | + if (skipping_preamble) { |
| 56 | + if ($0 ~ /^#/ || $0 == "") { |
| 57 | + next; |
| 58 | + } |
| 59 | +
|
| 60 | + if ($0 ~ /^msgid ""$/) { |
| 61 | + skipping_preamble = 0; |
| 62 | + skipping_header = 1; |
| 63 | + next; |
| 64 | + } |
| 65 | +
|
| 66 | + skipping_preamble = 0; |
| 67 | + } |
| 68 | +
|
| 69 | + if (skipping_header) { |
| 70 | + if ($0 == "") { |
| 71 | + skipping_header = 0; |
| 72 | + } |
| 73 | + next; |
| 74 | + } |
| 75 | +
|
| 76 | + if ($0 ~ /^#/) { |
| 77 | + next; |
| 78 | + } |
| 79 | +
|
| 80 | + if ($0 ~ /^msgid ""$/) { |
| 81 | + skipping_header = 1; |
| 82 | + next; |
| 83 | + } |
| 84 | +
|
| 85 | + print; |
| 86 | + }' "${input_file}" > "${output_file}" |
| 87 | +} |
| 88 | + |
| 89 | +strip_header_entry "${expected_tmp}" "${expected_normalized_tmp}" |
| 90 | +strip_header_entry "${current_tmp}" "${current_normalized_tmp}" |
| 91 | + |
| 92 | +if ! diff -u "${current_normalized_tmp}" "${expected_normalized_tmp}" >/dev/null; then |
| 93 | + echo "${pot_file} is out of sync with translatable source strings." >&2 |
| 94 | + echo "Regenerate ${pot_file} from current sources before committing." >&2 |
| 95 | + diff -u "${current_normalized_tmp}" "${expected_normalized_tmp}" || true |
| 96 | + exit 1 |
| 97 | +fi |
| 98 | + |
| 99 | +echo "POT template is in sync with source strings." |
0 commit comments