Skip to content

Commit bc3be2e

Browse files
authored
ci(orb): add i18n (#215)
1 parent aa7e222 commit bc3be2e

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/jobs/i18n.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
description: >
2+
Create translation files.
3+
4+
executor: default
5+
6+
steps:
7+
- checkout_with_workspace
8+
- run:
9+
name: Check if commit author is bot
10+
command: |
11+
COMMIT_AUTHOR=$(git log -1 --pretty=format:'%ae')
12+
if [ "$COMMIT_AUTHOR" = "$GITHUB_COMMITER_EMAIL" ]; then
13+
echo "Commit was made by bot ($(git rev-parse --short HEAD)), skipping translation update"
14+
circleci step halt
15+
fi
16+
- run:
17+
name: Install WP CLI
18+
command: |
19+
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
20+
chmod +x wp-cli.phar
21+
sudo mv wp-cli.phar /usr/local/bin/wp
22+
- run:
23+
name: Build plugin files, so dist dir is available
24+
command: |
25+
npm run build || true
26+
- run:
27+
name: Create POT translation files
28+
command: |
29+
# Theme is excluded, more in https://github.com/Automattic/newspack-theme/pull/2458
30+
if [ "$CIRCLE_PROJECT_REPONAME" = "newspack-theme" ]; then
31+
wp i18n make-pot . languages/${CIRCLE_PROJECT_REPONAME}.pot --exclude='src' --domain=${CIRCLE_PROJECT_REPONAME}
32+
else
33+
wp i18n make-pot . languages/${CIRCLE_PROJECT_REPONAME}.pot --domain=${CIRCLE_PROJECT_REPONAME}
34+
fi
35+
- run:
36+
name: Create JSON translation files
37+
command: |
38+
sudo apt-get update && sudo apt-get install -y gettext
39+
cd languages
40+
41+
# Create PO files from POT if they don't exist
42+
if [ ! -f "${CIRCLE_PROJECT_REPONAME}-en_US.po" ]; then
43+
wp i18n update-po ${CIRCLE_PROJECT_REPONAME}.pot .
44+
fi
45+
46+
for po in ${CIRCLE_PROJECT_REPONAME}-*.po; do
47+
if [ -f "$po" ]; then
48+
# Update translations according to the new POT file
49+
msgmerge $po $CIRCLE_PROJECT_REPONAME.pot -o $po.out
50+
mv $po.out $po
51+
msgfmt $po -o $(basename $po .po).mo
52+
# no-purge since we need the JS translations for the next run
53+
wp i18n make-json --no-purge $po .
54+
fi
55+
done
56+
- run:
57+
name: Commit translation files
58+
command: |
59+
LINES_CHANGED=$(git diff --numstat languages/ | awk '{sum += $1 + $2} END {print sum}')
60+
# If no existing files were changed, check for new files
61+
if [ -z "$LINES_CHANGED" ] || [ "$LINES_CHANGED" -eq 0 ]; then
62+
LINES_CHANGED=$(git ls-files --others --exclude-standard languages/ | xargs wc -l 2>/dev/null | tail -1 | awk '{print $1}')
63+
fi
64+
LINES_CHANGED=${LINES_CHANGED:-0}
65+
echo "Lines changed in languages/: $LINES_CHANGED"
66+
if [ "$LINES_CHANGED" -gt 3 ]; then
67+
git config user.email "$GITHUB_COMMITER_EMAIL"
68+
git config user.name "$GIT_COMMITTER_NAME"
69+
git remote set-url origin https://$GITHUB_TOKEN@github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME.git
70+
git add languages/
71+
git commit -m "chore: update translation files [skip ci]"
72+
git push origin $CIRCLE_BRANCH
73+
fi

0 commit comments

Comments
 (0)