|
| 1 | +name: i18n validation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - '**' |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-i18n: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + env: |
| 14 | + LOCALES: "zh-cn en zh-hant" |
| 15 | + GITHUB_EVENT_NAME: ${{ github.event_name }} |
| 16 | + GITHUB_BASE_REF: ${{ github.event.pull_request.base.ref || '' }} |
| 17 | + GITHUB_BEFORE: ${{ github.event.before || '' }} |
| 18 | + GITHUB_SHA: ${{ github.sha }} |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Validate added localized markdown files |
| 26 | + run: | |
| 27 | + set -euo pipefail |
| 28 | + echo "Locales: $LOCALES" |
| 29 | + # compute diff range |
| 30 | + if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then |
| 31 | + if [ -z "${GITHUB_BASE_REF}" ]; then |
| 32 | + echo "GITHUB_BASE_REF is empty for this pull_request event. Exiting." |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + echo "PR event, base ref: ${GITHUB_BASE_REF}" |
| 36 | + git fetch origin "${GITHUB_BASE_REF}" --quiet || true |
| 37 | + DIFF_RANGE="origin/${GITHUB_BASE_REF}...HEAD" |
| 38 | + else |
| 39 | + echo "Push event" |
| 40 | + if [ -z "${GITHUB_BEFORE}" ] || [ -z "${GITHUB_SHA}" ]; then |
| 41 | + echo "Missing before/sha info for push event. Exiting." |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + DIFF_RANGE="${GITHUB_BEFORE}..${GITHUB_SHA}" |
| 45 | + fi |
| 46 | + echo "Diff range: $DIFF_RANGE" |
| 47 | +
|
| 48 | + # Get list of added files (status A) |
| 49 | + mapfile -t ADDED < <(git diff --name-status $DIFF_RANGE | awk '$1=="A" {print $2}') |
| 50 | +
|
| 51 | + if [ ${#ADDED[@]} -eq 0 ]; then |
| 52 | + echo "No added files in this range. Nothing to check." |
| 53 | + exit 0 |
| 54 | + fi |
| 55 | +
|
| 56 | + echo "Added files:" |
| 57 | + printf ' - %s\n' "${ADDED[@]}" |
| 58 | +
|
| 59 | + # Build map: key = relative path under locale (e.g. "guide/foo.md"), value = list of locales added for that key |
| 60 | + declare -A seen |
| 61 | + IFS=' ' read -r -a LOCALES_ARR <<< "$LOCALES" |
| 62 | + ERR=0 |
| 63 | +
|
| 64 | + for f in "${ADDED[@]}"; do |
| 65 | + # only consider markdown files under the 3 locale dirs |
| 66 | + if [[ "$f" =~ ^(zh-cn|en|zh-hant)/.+\.md$ ]]; then |
| 67 | + locale="${f%%/*}" |
| 68 | + name="${f#*/}" |
| 69 | + # trim possible leading ./ |
| 70 | + name="${name#./}" |
| 71 | + seen["$name"]="${seen["$name"]} ${locale}" |
| 72 | + fi |
| 73 | + done |
| 74 | +
|
| 75 | + if [ ${#seen[@]} -eq 0 ]; then |
| 76 | + echo "No new localized markdown files added under zh-cn/en/zh-hant. Nothing to check." |
| 77 | + exit 0 |
| 78 | + fi |
| 79 | +
|
| 80 | + echo "Checking that for each new localized file all locales are added in this same commit/PR..." |
| 81 | + for key in "${!seen[@]}"; do |
| 82 | + present="${seen[$key]}" |
| 83 | + # normalize to array |
| 84 | + read -r -a present_arr <<< "$present" |
| 85 | + missing=() |
| 86 | + for loc in "${LOCALES_ARR[@]}"; do |
| 87 | + found=false |
| 88 | + for p in "${present_arr[@]}"; do |
| 89 | + if [ "$p" = "$loc" ]; then found=true; break; fi |
| 90 | + done |
| 91 | + if ! $found; then missing+=("$loc"); fi |
| 92 | + done |
| 93 | + if [ ${#missing[@]} -ne 0 ]; then |
| 94 | + echo "ERROR: New file '$key' added in locales:${present} but missing in locales: ${missing[*]}" |
| 95 | + ERR=1 |
| 96 | + else |
| 97 | + echo "OK: '$key' present in all locales (${present})" |
| 98 | + fi |
| 99 | + done |
| 100 | +
|
| 101 | + if [ "$ERR" -ne 0 ]; then |
| 102 | + echo "" |
| 103 | + echo "One or more localized md files are missing their counterparts. Please add the missing files in the same commit/PR." |
| 104 | + echo "" |
| 105 | + echo "Expected for each new file under zh-cn/en/zh-hant to have the same relative path and filename." |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | +
|
| 109 | + echo "All checks passed." |
0 commit comments