name: Upload translatable strings to Crowdin

on:
  push:
    branches: [ "master", "release-*" ]
    paths-ignore:
      - 'po/xournalpp.pot'
      - 'po/*.po'
  workflow_dispatch:  # This can be triggered from the Github Action tab on the website

jobs:
  Translations:
    if: github.repository == 'xournalpp/xournalpp'
    name: Upload translatable strings to Crowdin
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - name: 'Install dependencies'
        shell: bash
        run: |
          sudo apt-get update
          sudo apt-get install -y gettext
          # Download the last .its/loc files for gettext - Cheaper than installing all of gtk + dependencies
          sudo wget https://raw.githubusercontent.com/GNOME/gtk/gtk-3-24/gtk/gtkbuilder.{its,loc} -P /usr/share/gettext/its/
      - name: 'Update translation template'
        id: check-diff
        shell: bash
        run: |
          mkdir build
          cd build
          cmake .. -DONLY_CONFIGURE_FOR_TARGET_POT=on
          cmake --build . --target pot
          nb=$(git diff -I'POT-Creation-Date: [0-9:+-]*' -- ../po/ | wc -l)
          echo "Number of translatable lines changed: $nb"
          if [ $nb -gt 0 ]; then
            echo "update-translation-sources=true" >> "$GITHUB_OUTPUT"
          fi
      - name: 'Push translation file to Crowdin'
        if: steps.check-diff.outputs.update-translation-sources == 'true'
        uses: crowdin/github-action@v2
        with:
          upload_sources: true
          upload_translations: false
          download_translations: false
          crowdin_branch_name: ${{ github.ref_name }}
        env:
          CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
          CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
