Skip to content

Commit 6a6f93f

Browse files
committed
feat: update translate action
1 parent 26333cc commit 6a6f93f

File tree

6 files changed

+204
-2
lines changed

6 files changed

+204
-2
lines changed

.github/actions/setup/action.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Setup Tools
2+
description: Action that sets up Node, pnpm, and caching
3+
runs:
4+
using: composite
5+
steps:
6+
- name: Setup pnpm
7+
uses: pnpm/[email protected]
8+
- name: Setup Node
9+
uses: actions/[email protected]
10+
with:
11+
node-version-file: .nvmrc
12+
- name: Get pnpm store directory
13+
shell: bash
14+
run: |
15+
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
16+
- name: Setup pnpm cache
17+
uses: actions/[email protected]
18+
with:
19+
path: ${{ env.STORE_PATH }}
20+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
21+
restore-keys: |
22+
${{ runner.os }}-pnpm-store-
23+
- name: Install dependencies
24+
shell: bash
25+
run: pnpm install --frozen-lockfile
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
name: "Translate Documentation Action"
2+
description: "Translates documentation files automatically using AI"
3+
4+
inputs:
5+
custom_arguments:
6+
description: "Custom arguments to pass to the translation package command"
7+
required: false
8+
default: ""
9+
github_token:
10+
description: "GitHub token for creating PRs"
11+
required: false
12+
default: ${{ github.token }}
13+
api_key:
14+
description: "API key for translation service (e.g., OPENAI_API_KEY)"
15+
required: true
16+
translation_command:
17+
description: "command to use for translation"
18+
required: false
19+
default: "pnpm run translate"
20+
base_branch:
21+
description: "Base branch to create PR against"
22+
required: false
23+
default: "main"
24+
pr_branch:
25+
description: "Branch name for the PR"
26+
required: false
27+
default: "docs/update-translations"
28+
pr_title:
29+
description: "Title for the PR"
30+
required: false
31+
default: "Update translations"
32+
pr_body:
33+
description: "Body text for the PR"
34+
required: false
35+
default: |
36+
This PR updates the documentation translations automatically.
37+
38+
Generated by the translate workflow.
39+
commit_message:
40+
description: "Commit message for the translation changes"
41+
required: false
42+
default: "docs: update documentation translations"
43+
schedule_cron:
44+
description: "Cron schedule for automatic translation (used in workflow template)"
45+
required: false
46+
default: "0 20 * * *"
47+
add_paths:
48+
description: "A comma or newline-separated list of file paths to commit. Paths should follow git's pathspec syntax."
49+
required: false
50+
default: "apps/docs/content/**"
51+
enable_formatting:
52+
description: "Whether to run code formatting before creating the PR"
53+
required: false
54+
default: "true"
55+
format_command:
56+
description: "The command to run for formatting code"
57+
required: false
58+
default: "pnpm prettier:write"
59+
60+
runs:
61+
using: "composite"
62+
steps:
63+
- name: Checkout code
64+
uses: actions/checkout@v3
65+
with:
66+
fetch-depth: 0 # Fetch all history for proper timestamp lookup
67+
68+
- name: Check if PR branch already exists
69+
id: check_branch
70+
shell: bash
71+
run: |
72+
if git ls-remote --heads origin ${{ inputs.pr_branch }} | grep -q ${{ inputs.pr_branch }}; then
73+
echo "exists=true" >> $GITHUB_OUTPUT
74+
else
75+
echo "exists=false" >> $GITHUB_OUTPUT
76+
fi
77+
- name: Setup Tools
78+
if: steps.check_branch.outputs.exists == 'false'
79+
uses: ./.github/actions/setup
80+
81+
- name: Run translation
82+
if: steps.check_branch.outputs.exists == 'false'
83+
shell: bash
84+
run: |
85+
echo "Running translation with custom arguments: ${{ inputs.custom_arguments }}"
86+
${{ inputs.translation_command }} ${{ inputs.custom_arguments }}
87+
env:
88+
OPENAI_API_KEY: ${{ inputs.api_key }}
89+
90+
- name: Check for modifications
91+
if: steps.check_branch.outputs.exists == 'false'
92+
id: check-changes
93+
shell: bash
94+
run: |
95+
if [[ $(git status --porcelain | wc -l) -gt 0 ]]; then
96+
echo "has_changes=true" >> $GITHUB_OUTPUT
97+
echo "Changes detected. Will proceed with formatting and commit."
98+
else
99+
echo "has_changes=false" >> $GITHUB_OUTPUT
100+
echo "No changes detected after translation. Exiting workflow."
101+
fi
102+
103+
- name: Fix formatting
104+
if: steps.check_branch.outputs.exists == 'false' && steps.check-changes.outputs.has_changes == 'true' && inputs.enable_formatting == 'true'
105+
shell: bash
106+
run: ${{ inputs.format_command }}
107+
108+
- name: Create Pull Request
109+
if: steps.check_branch.outputs.exists == 'false' && steps.check-changes.outputs.has_changes == 'true'
110+
uses: peter-evans/create-pull-request@v5
111+
with:
112+
token: ${{ inputs.github_token }}
113+
commit-message: ${{ inputs.commit_message }}
114+
title: ${{ inputs.pr_title }}
115+
body: ${{ inputs.pr_body }}
116+
branch: ${{ inputs.pr_branch }}
117+
delete-branch: true
118+
base: ${{ inputs.base_branch }}
119+
add-paths: ${{ inputs.add_paths }}
120+
121+
branding:
122+
icon: "globe"
123+
color: "blue"

.github/workflows/translate-docs.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Translate Documentation
2+
3+
on:
4+
schedule:
5+
- cron:
6+
'0 20 * * *' # Daily at 20:00 UTC (DeepSeek API off-peak pricing window 16:30-00:30 UTC)
7+
# Pacific Time: 1:00 PM PDT / 12:00 PM PST
8+
# Off-peak window in PT: ~9:30 AM to 5:30 PM PDT / ~8:30 AM to 4:30 PM PST
9+
push:
10+
# Run when merging from official repo to check if translations are outdated
11+
branches:
12+
- main
13+
workflow_dispatch: # Allow manual triggering
14+
inputs:
15+
custom_arguments:
16+
description: 'Custom arguments to pass to the translation package command. e.g., "-t zh-hans"'
17+
required: false
18+
type: string
19+
20+
# Add permissions needed for creating PRs
21+
permissions:
22+
contents: write
23+
pull-requests: write
24+
25+
jobs:
26+
translate:
27+
runs-on: ubuntu-latest
28+
# Run when manually triggered OR scheduled OR when the commit message contains "Merge pull request"
29+
if: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' || contains(github.event.head_commit.message, 'Merge pull request') }}
30+
steps:
31+
# Use the translate-docs-action
32+
- name: Translate documentation
33+
uses: ./.github/actions/translate-docs
34+
with:
35+
# Required inputs
36+
api_key: ${{ secrets.OPENAI_API_KEY }}
37+
# Optional inputs with their default values shown
38+
github_token: ${{ secrets.GITHUB_TOKEN }}
39+
custom_arguments: ${{ github.event.inputs.custom_arguments }}
40+
# translation_command: 'pnpm run translate'
41+
# base_branch: 'main'
42+
# pr_branch: 'docs/update-translations'
43+
# pr_title: 'Update translations'
44+
# The following uses YAML pipe syntax for multi-line strings
45+
# pr_body: |
46+
# This PR updates the documentation translations automatically.
47+
#
48+
# Generated by the translate workflow.
49+
# commit_message: 'docs: update documentation translations'
50+
# add_paths: 'apps/docs/content/**'
51+
enable_formatting: 'false'
52+
# format_command: 'pnpm prettier:write'

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v20.18.0

apps/docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dev": "next dev --turbo",
99
"start": "next start",
1010
"postinstall": "fumadocs-mdx",
11-
"translate": "@next-i18n/translate",
11+
"translate": "npx @next-i18n/translate",
1212
"check-types": "tsc --noEmit"
1313
},
1414
"dependencies": {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"format": "biome format --write .",
88
"lint": "biome lint .",
99
"lint:fix": "biome lint --write .",
10-
"dev": "pnpm --filter @next-i18n/translate --filter @next-i18n/docs --parallel dev"
10+
"dev": "pnpm --filter @next-i18n/translate --filter @next-i18n/docs --parallel dev",
11+
"translate": "pnpm --filter @next-i18n/docs translate"
1112
},
1213
"keywords": [],
1314
"devDependencies": {

0 commit comments

Comments
 (0)