1+ name : crowdin
2+
3+ on :
4+ pull_request :
5+ types :
6+ - opened
7+ - reopened
8+ - synchronize
9+ - labeled
10+
11+ permissions :
12+ contents : write
13+ pull-requests : read
14+
15+ jobs :
16+ format :
17+ name : Format Translations
18+ if : |
19+ contains(github.event.pull_request.labels.*.name, 'translations') &&
20+ startsWith(github.event.pull_request.head.ref, 'l10n_') &&
21+ github.event.pull_request.head.repo.full_name == github.repository
22+ runs-on : ubuntu-latest
23+
24+ steps :
25+ - name : Checkout PR branch
26+ uses : actions/checkout@v4
27+ with :
28+ ref : ${{ github.event.pull_request.head.sha }}
29+ fetch-depth : 0
30+
31+ - name : Setup Node
32+ uses : actions/setup-node@v4
33+ with :
34+ node-version : 20
35+
36+ - name : Get files
37+ id : files
38+ run : |
39+ git fetch origin "${{ github.event.pull_request.base.ref }}"
40+
41+ git diff \
42+ --name-only \
43+ "origin/${{ github.event.pull_request.base.ref }}...HEAD" \
44+ -- '*.json' > files.txt
45+
46+ cat files.txt
47+
48+ - name : Format translation files
49+ run : |
50+ cat > fix.js <<'EOF'
51+ const fs = require('fs');
52+
53+ function clean(value) {
54+ if (typeof value === 'string') {
55+ return value === '' ? undefined : value;
56+ }
57+
58+ if (Array.isArray(value)) {
59+ const arr = value.map(clean).filter(v => v !== undefined);
60+ return arr.length ? arr : undefined;
61+ }
62+
63+ if (value && typeof value === 'object') {
64+ const result = {};
65+
66+ for (const [key, val] of Object.entries(value)) {
67+ const cleaned = clean(val);
68+
69+ if (cleaned !== undefined) {
70+ result[key] = cleaned;
71+ }
72+ }
73+
74+ return Object.keys(result).length ? result : undefined;
75+ }
76+
77+ return value;
78+ }
79+
80+ const files = fs.readFileSync('files.txt', 'utf8')
81+ .split('\n')
82+ .map(f => f.trim())
83+ .filter(Boolean);
84+
85+ for (const file of files) {
86+ try {
87+ const json = JSON.parse(fs.readFileSync(file, 'utf8'));
88+ const cleaned = clean(json) ?? {};
89+
90+ fs.writeFileSync(file, JSON.stringify(cleaned), 'utf8');
91+
92+ console.log(`Processed ${file}`);
93+ } catch (err) {
94+ console.warn(`Skipping ${file}: ${err.message}`);
95+ }
96+ }
97+ EOF
98+
99+ node fix.js
100+
101+ - name : Commit changes
102+ run : |
103+ if git diff --quiet; then
104+ echo "No changes."
105+ exit 0
106+ fi
107+
108+ git config user.name "github-actions[bot]"
109+ git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
110+
111+ git add $(cat files.txt)
112+ git commit -m "Format Crowdin translation exports"
113+ git push
0 commit comments