name: Download translations from Crowdin

on:
  workflow_dispatch:
  push:
    branches:
      - 'release/**'

jobs:
  install-front:
    uses: ./.github/workflows/front-dependencies-installation.yml
    with:
      node_version: '20.x'

  synchronize-with-crowdin:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - name: Checkout
        uses: actions/checkout@v6
      - name: Create empty source files
        run: |
          touch src/backend/locale/django.pot
          mkdir -p src/frontend/packages/i18n/locales/impress/
          touch src/frontend/packages/i18n/locales/impress/translations-crowdin.json
      # crowdin workflow
      - name: crowdin action
        uses: crowdin/github-action@v2
        with:
          config: crowdin/config.yml
          upload_sources: false
          upload_translations: false
          download_translations: true
          create_pull_request: false
          push_translations: false
          push_sources: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          # A numeric ID, found at https://crowdin.com/project/<projectName>/tools/api
          CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}

          # Visit https://crowdin.com/settings#api-key to create this token
          CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}

          CROWDIN_BASE_PATH: "../src/"
      # frontend i18n
      - name: Restore the frontend cache
        uses: actions/cache@v4
        with:
          path: "src/frontend/**/node_modules"
          key: front-node_modules-${{ hashFiles('src/frontend/**/yarn.lock') }}
          fail-on-cache-miss: true
      - name: generate translations files
        working-directory: src/frontend
        run: yarn i18n:deploy
      # Create a new PR
      - name: Create a new Pull Request with new translated strings
        uses: peter-evans/create-pull-request@v7
        with:
          commit-message: |
            🌐(i18n) update translated strings

            Update translated files with new translations
          title: 🌐(i18n) update translated strings
          body: |
            ## Purpose

            update translated strings

            ## Proposal

            - [x]  update translated strings
          branch: i18n/update-translations
          labels: i18n
