name: i18n-ai-translate
description: Use ChatGPT or Google Gemini to automatically translate your i18n JSON on every PR
author: Taaha Mahdi

inputs:
  json-file-path:
    description: 'Path to the source i18n JSON file'
    required: true
  api-key:
    description: 'The API key'
    required: false
  host:
    description: "The ollama host and port number"
    required: false
  author-email:
    description: 'The email of the person to attribute the i18n change to'
    required: false
    default: 'tmahdi+i18n-ai-translate@proton.me'
  author-name:
    description: 'The name of the person to attribute the i18n change to'
    required: false
    default: 'Taaha Mahdi'
  language:
    description: 'Language to translate from'
    required: false
    default: 'en'
  engine:
    description: 'Engine to use for translation'
    required: false
    default: 'chatgpt'
  model:
    description: 'Model to use for the translation (e.g., gpt-4o, gpt-4, gpt-3.5-turbo, gemini-pro)'
    required: false
    default: 'gpt-4o'
  templated-string-prefix:
    description: 'Prefix for templated strings'
    required: false
    default: "{{"
  templated-string-suffix:
    description: 'Suffix for templated strings'
    required: false
    default: "}}"
  batch-size:
    description: 'How many keys to process at a time'
    required: false
    default: 32

runs:
  using: 'composite'
  steps:
    - name: Check out Repo
      uses: actions/checkout@v3
      with:
        ref: ${{ github.head_ref }}
        fetch-depth: 0

    - name: Set up Node.js
      uses: actions/setup-node@v3
      with:
        node-version: 18

    - name: Fetch original translation
      shell: bash
      run: |
        mv "${{ inputs.json-file-path }}" "${{ inputs.json-file-path }}-latest"
        git checkout origin/${{ github.base_ref }} -- "${{ inputs.json-file-path }}"

    - name: Translate the diff
      shell: bash
      run: |
        if [ -n "${{ inputs.api-key }}" ]; then
          npx --yes i18n-ai-translate@latest diff \
            -b "${{ inputs.json-file-path }}" \
            -a "${{ inputs.json-file-path }}-latest" \
            -l "${{ inputs.language }}" \
            --verbose \
            --engine "${{ inputs.engine }}" \
            --model "${{ inputs.model }}" \
            -p "${{ inputs.templated-string-prefix }}" \
            -s "${{ inputs.templated-string-suffix }}" \
            -n "${{ inputs.batch-size }}" \
            -k "${{ inputs.api-key }}"
        elif [ -n "${{ inputs.host }}" ]; then
          npx --yes i18n-ai-translate@latest diff \
            -b "${{ inputs.json-file-path }}" \
            -a "${{ inputs.json-file-path }}-latest" \
            -l "${{ inputs.language }}" \
            --verbose \
            --engine "${{ inputs.engine }}" \
            --model "${{ inputs.model }}" \
            -p "${{ inputs.templated-string-prefix }}" \
            -s "${{ inputs.templated-string-suffix }}" \
            -n "${{ inputs.batch-size }}" \
            -h "${{ inputs.host }}"
        else
          echo "No API key or host specified; skipping automatic translation."
        fi

        if [ $? -ne 0 ]; then
          echo "Translation step failed" >&2
          exit 1
        fi

    - name: Configure Git
      shell: bash
      run: |
        git config --global user.email "${{ inputs.author-email }}"
        git config --global user.name "${{ inputs.author-name }}"

    - name: Commit the difference
      shell: bash
      run: |
        rm -r "${{ inputs.json-file-path }}"
        mv "${{ inputs.json-file-path }}-latest" "${{ inputs.json-file-path }}"
        git add --all
        git commit -m "Update translations" || echo "No changes to commit"
        git push
