forked from ihhub/fheroes2
-
Notifications
You must be signed in to change notification settings - Fork 0
112 lines (109 loc) · 3.94 KB
/
translation_update.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
name: Translation update
on:
workflow_call:
permissions:
contents: write
pull-requests: write
jobs:
translation:
name: Translation update
if: ${{ github.repository == 'ihhub/fheroes2' && github.event_name == 'push' }}
runs-on: ubuntu-latest
timeout-minutes: 10
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
sudo apt-get -y update
sudo apt-get -y install gettext
- name: Setup Git
run: |
git config user.name "GitHub Actions Bot"
git config user.email "[email protected]"
- name: Create PR branch
run: |
PR_BRANCH=translation-update-$(uuidgen)
git switch -c "$PR_BRANCH"
echo "PR_BRANCH=$PR_BRANCH" >> "$GITHUB_ENV"
- name: Generate POT
run: |
make -C src/dist/fheroes2 -j "$(nproc)" pot
- name: Merge PO with POT
run: |
make -C files/lang -j "$(nproc)" merge
- name: Generate statistics
run: |
mkdir -p docs/json
for PO_FILE in files/lang/*.po; do
STATISTICS=$(msgfmt --statistics --output-file=/dev/null -- "$PO_FILE" 2>&1)
echo "$PO_FILE: $STATISTICS"
if [[ "$STATISTICS" =~ ([0-9]+)\ translated\ messages? ]]; then
TRANSLATED=${BASH_REMATCH[1]}
else
TRANSLATED=0
fi
if [[ "$STATISTICS" =~ ([0-9]+)\ fuzzy\ translations? ]]; then
FUZZY=${BASH_REMATCH[1]}
else
FUZZY=0
fi
if [[ "$STATISTICS" =~ ([0-9]+)\ untranslated\ messages? ]]; then
UNTRANSLATED=${BASH_REMATCH[1]}
else
UNTRANSLATED=0
fi
OVERALL=$((TRANSLATED + FUZZY + UNTRANSLATED))
if ((OVERALL == 0)); then
PERCENT=0
else
PERCENT=$((TRANSLATED * 100 / OVERALL))
fi
LANGUAGE=$(basename "${PO_FILE%.po}")
if [[ "$(head -n1 -- "$PO_FILE")" =~ ^\#\ (.+)\ translation ]]; then
LABEL=${BASH_REMATCH[1]}
else
LABEL=$LANGUAGE
fi
if ((PERCENT < 75)); then
COLOR="red"
elif ((PERCENT < 95)); then
COLOR="yellow"
else
COLOR="green"
fi
jq -n -c --arg lbl "$LABEL" --arg msg "$PERCENT%" --arg clr "$COLOR" "{ schemaVersion: 1, label: \$lbl, message: \$msg, color: \$clr }" > "docs/json/lang_$LANGUAGE.json"
done
- name: Commit changes
run: |
git diff --name-only -z -- files/lang/*.po \
| xargs -r0 bash -c 'set -e; for NAME in "$@"; do if [[ -n "$(git diff "-I^\"POT-Creation-Date:[^\"]*\"$" -- "$NAME")" ]]; then git add -- "$NAME"; fi; done' dummy
git add -- docs/json/lang_*.json
if git commit -m "Update translation files"; then git push origin HEAD; echo "CREATE_PR=YES" >> "$GITHUB_ENV"; fi
- name: Create PR
if: ${{ env.CREATE_PR == 'YES' }}
uses: actions/github-script@v7
with:
script: |
const pr = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Update translation files",
head: "${{ env.PR_BRANCH }}",
base: "${{ github.ref }}",
});
const assigneesPromise = github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.data.number,
assignees: ["${{ github.actor }}"],
});
const labelsPromise = github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.data.number,
labels: ["improvement", "translation"],
});
await Promise.all([assigneesPromise, labelsPromise]);