GPT Translate GitHub Action

repository·master·Indexed 42 days ago

https://github.com/3ru/gpt-translate

A GitHub Action for automating the translation of Markdown (.md), MDX (.mdx), and JSON (.json) files into multiple languages. It is triggered via issue or pull request comments using /gpt-translate or /gt commands and supports various AI providers including OpenAI, Anthropic, Google Gemini, Azure, Perplexity, Groq, Fireworks AI, Mistral AI, and Cohere. Version 1.2.2.

Tokens
19.9K
Snippets
67
Records
95
Agent score
49%

What's inside GPT Translate

  1. Overview of GPT-Translate

    master
    GPT-Translate is a GitHub workflow designed to help developers implement internationalization (i18n) for their documentation easily. It automates the translation process for documentation written in any language, allowing projects to deliver content to a global audience without the high manual cost of translating extensive documentation files.
  2. Understand supported languages for translation

    master

    The translation capabilities of gpt-translate are determined by the underlying LLM used (GPT-3.5 or GPT-4).

    • General Rule: Any language interpreted by GPT-3.5 or GPT-4 can be translated.
    • Accuracy Factors: Translation accuracy is highly dependent on the amount of training data available for a specific language. Languages with fewer speakers (and thus less training data) may result in lower translation accuracy.
    • Performance Trends: While not a direct evaluation of translation quality, OpenAI's MMLU (Massive Multitask Language Understanding) benchmarks indicate that model performance generally follows a trend based on language prevalence.
  3. Supported File Types and Languages

    master

    Supported File Formats

    The action currently only supports translating the following file types:

    • Markdown (.md)
    • Markdown-JSX (.mdx)
    • JSON (.json)

    Supported Languages

    You can translate into any language that can be interpreted by GPT-4 or GPT-3.5.

  4. Supported languages and providers

    master

    Languages

    The bot supports any language that can be interpreted by GPT-4 or GPT-3.5.

    AI Providers

    While the default configuration often uses OpenAI, the project supports multiple providers including:

    • OpenAI
    • Azure OpenAI
    • Anthropic
    • Perplexity
    • Google Gemini
    • Groq
    • Fireworks AI
    • Mistral AI
    • Cohere
  5. Best practices for Markdown (`.md`) translation

    master

    When translating large Markdown files, avoid extremely long sentences or blocks of text without line breaks.

    If a text block contains approximately 80,000 consecutive characters without line breaks (specifically when using gpt-3.5), it may cause errors during the splitting process. To ensure successful translation, use frequent line breaks to separate paragraphs or sections.

    You can measure the token count of your text using the official OpenAI tokenizer.

    # Friendly format (Recommended)
    
    This is a sample novel.
    
    Once upon a time...
  6. Customize translation behavior with `prompt`

    master

    The prompt argument allows you to define a custom instruction for the AI model to follow during translation. You can use dynamic variables within curly braces {} to inject context into your prompt:

    • {targetLanguage}: The desired language for translation.
    • {targetFileExt}: The desired file extension (e.g., .md).

    Example configuration using a custom prompt:

    with:
      apikey: ${{ secrets.OPENAI_API_KEY }}
      prompt: 'Please translate the given text into naturalistic {targetLanguage} and output it in {targetFileExt} format.'
    with:
      apikey: ${{ secrets.OPENAI_API_KEY }}
      prompt: 'Please translate the given text into naturalistic {targetLanguage}.'
  7. Setup GPT Translate GitHub Action

    master

    To use GPT Translate, you must configure your repository permissions and secrets to allow the action to write files and access AI models.

    1. Repository Permissions

    Navigate to Settings > Actions > General and enable:

    • Read and write permissions
    • Allow GitHub Actions to create and approve pull requests

    2. Configure API Secrets

    Navigate to Settings > Secrets and variables > Actions and add your AI provider's API key as a secret named OPENAI_API_KEY (or the relevant key for your provider).

    3. Workflow Configuration

    Create a workflow file (e.g., .github/workflows/gpt-translate.yml). It is highly recommended to trigger the action only when a specific comment is made to save runtime costs.

    Minimum Workflow Example:

    # .github/workflows/gpt-translate.yml
    name: GPT Translate
    
    on:
      issue_comment:
        types: [ created ]
    
    jobs:
      gpt_translate:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v4
    
          - name: Run GPT Translate
            if: |
              contains(github.event.comment.body, '/gpt-translate') || 
              contains(github.event.comment.body, '/gt')
            uses: 3ru/gpt-translate@master
            with:
              apikey: ${{ secrets.OPENAI_API_KEY }}
  8. Set up GPT Translate GitHub Action

    master

    To use GPT Translate, you must configure your repository permissions and secrets, then create a workflow file.

    1. Repository Settings

    • Permissions: Navigate to Settings > Actions > General and enable Read and write permissions. Also, enable Allow GitHub Actions to create and approve pull requests.
    • Secrets: Navigate to Settings > Secrets and variables > Actions and add your AI provider's API key as a secret named OPENAI_API_KEY (or the relevant key for your provider).

    2. Workflow Configuration

    Create a workflow file (e.g., .github/workflows/gpt-translate.yml). The workflow must:

    • Use actions/checkout@v4 (or similar) to check out the repository.
    • Trigger on issue_comment with types: [ created ].
    • Provide the API key via the apikey input.

    It is recommended to use a conditional if check so the action only runs when a specific command is mentioned in the comment to save execution time.

    # .github/workflows/gpt-translate.yml
    name: GPT Translate
    
    on:
      issue_comment:
        types: [ created ]
    
    jobs:
      gpt_translate:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v4
    
          - name: Run GPT Translate
            if: |
              contains(github.event.comment.body, '/gpt-translate') || 
              contains(github.event.comment.body, '/gt')
            uses: 3ru/gpt-translate@master
            with:
              apikey: ${{ secrets.OPENAI_API_KEY }}
  9. Configure the command-based translation workflow

    master

    To enable command-based translation, create a GitHub Actions workflow file (e.g., .github/workflows/gpt-translate.yml) with the following configuration.

    Important Requirements:

    1. Checkout the repository: You must include actions/checkout@v3 before the translation step so the action has access to the files in your repository.
    2. Conditional Execution: Use an if statement to ensure the action only runs when /gpt-translate or /gt is detected in the comment body.
    3. API Key: Pass your OpenAI API key via a GitHub Secret (e.g., OPENAI_API_KEY).
    name: GPT翻译
    
    on:
      issue_comment:
        types: [created]
    
    jobs:
      gpt_translate:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v3
    
          - name: 运行GPT翻译
            if: |
              contains(github.event.comment.body, '/gpt-translate') ||
              contains(github.event.comment.body, '/gt')
            uses: 3ru/gpt-translate@master
            with:
              apikey: ${{ secrets.OPENAI_API_KEY }}