|
| 1 | +name: Apply Community Translations |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + translations_json: |
| 7 | + description: 'JSON array of translation edits' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + contributor: |
| 11 | + description: 'GitHub username of contributor' |
| 12 | + required: true |
| 13 | + type: string |
| 14 | + contributor_name: |
| 15 | + description: 'Display name of contributor' |
| 16 | + required: false |
| 17 | + type: string |
| 18 | + |
| 19 | +jobs: |
| 20 | + apply-translations: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: write |
| 24 | + pull-requests: write |
| 25 | + steps: |
| 26 | + - name: Checkout repository |
| 27 | + uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - name: Install XML & JSON parsing tools |
| 30 | + run: sudo apt-get update && sudo apt-get install -y xmlstarlet jq |
| 31 | + |
| 32 | + - name: Create branch and apply string edits |
| 33 | + run: | |
| 34 | + echo '${{ inputs.translations_json }}' > /tmp/edits.json |
| 35 | + BRANCH="translations/${{ inputs.contributor }}-$(date +%s)" |
| 36 | + git checkout -b "$BRANCH" |
| 37 | +
|
| 38 | + jq -c '.[]' /tmp/edits.json | while read -r edit; do |
| 39 | + KEY=$(echo "$edit" | jq -r '.key') |
| 40 | + LOCALE=$(echo "$edit" | jq -r '.locale') |
| 41 | + VALUE=$(echo "$edit" | jq -r '.value') |
| 42 | +
|
| 43 | + if [ "$LOCALE" = "en" ]; then |
| 44 | + FILE="app/src/main/res/values/strings.xml" |
| 45 | + else |
| 46 | + FILE="app/src/main/res/values-${LOCALE}/strings.xml" |
| 47 | + if [ ! -f "$FILE" ]; then |
| 48 | + FILE=$(find app/src/main/res -name "strings.xml" -path "*values-${LOCALE}*" | head -1) |
| 49 | + fi |
| 50 | + fi |
| 51 | +
|
| 52 | + if [ -f "$FILE" ]; then |
| 53 | + xmlstarlet ed -L \ |
| 54 | + -u "//resources/string[@name='$KEY']" \ |
| 55 | + -v "$VALUE" \ |
| 56 | + "$FILE" || true |
| 57 | + fi |
| 58 | + done |
| 59 | +
|
| 60 | + git config user.email "actions@github.com" |
| 61 | + git config user.name "GitHub Actions [In-App Translator]" |
| 62 | + git add app/src/main/res/ |
| 63 | + git commit -m "🌍 Community translations by @${{ inputs.contributor }}" || echo "No changes to commit" |
| 64 | + git push origin "$BRANCH" |
| 65 | + echo "BRANCH_NAME=$BRANCH" >> $GITHUB_ENV |
| 66 | +
|
| 67 | + - name: Create Pull Request |
| 68 | + env: |
| 69 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 70 | + run: | |
| 71 | + gh pr create \ |
| 72 | + --title "🌍 Community translations by @${{ inputs.contributor }}" \ |
| 73 | + --body "Translations contributed by **@${{ inputs.contributor }}** (${{ inputs.contributor_name }}) directly via the Essentials in-app translation mode." \ |
| 74 | + --label "translations" \ |
| 75 | + --base main \ |
| 76 | + --head "$BRANCH_NAME" |
0 commit comments