Skip to content

Commit f864cde

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

File tree

7 files changed

+235
-7
lines changed

7 files changed

+235
-7
lines changed

.github/actions/setup/action.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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
26+
27+
- name: Build packages
28+
shell: bash
29+
run: pnpm build:packages
30+
31+
- name: Link binaries and make executable
32+
shell: bash
33+
run: |
34+
chmod +x ./packages/translate/dist/index.js
35+
echo "Verifying translate-docs binary exists"
36+
ls -la ./packages/translate/dist/index.js || true
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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: Check if repository is already checked out
64+
id: check_repo
65+
shell: bash
66+
run: |
67+
if [ -d ".git" ]; then
68+
echo "already_checked_out=true" >> $GITHUB_OUTPUT
69+
else
70+
echo "already_checked_out=false" >> $GITHUB_OUTPUT
71+
fi
72+
73+
- name: Checkout code
74+
if: steps.check_repo.outputs.already_checked_out != 'true'
75+
uses: actions/checkout@v3
76+
with:
77+
fetch-depth: 0 # Fetch all history for proper timestamp lookup
78+
79+
- name: Check if PR branch already exists
80+
id: check_branch
81+
shell: bash
82+
run: |
83+
if git ls-remote --heads origin ${{ inputs.pr_branch }} | grep -q ${{ inputs.pr_branch }}; then
84+
echo "exists=true" >> $GITHUB_OUTPUT
85+
else
86+
echo "exists=false" >> $GITHUB_OUTPUT
87+
fi
88+
- name: Setup Tools
89+
if: steps.check_branch.outputs.exists == 'false'
90+
uses: ./.github/actions/setup
91+
92+
- name: Run translation
93+
if: steps.check_branch.outputs.exists == 'false'
94+
shell: bash
95+
run: |
96+
echo "Running translation with custom arguments: ${{ inputs.custom_arguments }}"
97+
${{ inputs.translation_command }} ${{ inputs.custom_arguments }}
98+
env:
99+
OPENAI_API_KEY: ${{ inputs.api_key }}
100+
101+
- name: Check for modifications
102+
if: steps.check_branch.outputs.exists == 'false'
103+
id: check-changes
104+
shell: bash
105+
run: |
106+
if [[ $(git status --porcelain | wc -l) -gt 0 ]]; then
107+
echo "has_changes=true" >> $GITHUB_OUTPUT
108+
echo "Changes detected. Will proceed with formatting and commit."
109+
else
110+
echo "has_changes=false" >> $GITHUB_OUTPUT
111+
echo "No changes detected after translation. Exiting workflow."
112+
fi
113+
114+
- name: Fix formatting
115+
if: steps.check_branch.outputs.exists == 'false' && steps.check-changes.outputs.has_changes == 'true' && inputs.enable_formatting == 'true'
116+
shell: bash
117+
run: ${{ inputs.format_command }}
118+
119+
- name: Create Pull Request
120+
if: steps.check_branch.outputs.exists == 'false' && steps.check-changes.outputs.has_changes == 'true'
121+
uses: peter-evans/create-pull-request@v5
122+
with:
123+
token: ${{ inputs.github_token }}
124+
commit-message: ${{ inputs.commit_message }}
125+
title: ${{ inputs.pr_title }}
126+
body: ${{ inputs.pr_body }}
127+
branch: ${{ inputs.pr_branch }}
128+
delete-branch: true
129+
base: ${{ inputs.base_branch }}
130+
add-paths: ${{ inputs.add_paths }}
131+
132+
branding:
133+
icon: "globe"
134+
color: "blue"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
# Checkout the repository first to access local actions
32+
- name: Checkout code
33+
uses: actions/checkout@v3
34+
with:
35+
fetch-depth: 0 # Fetch all history for proper timestamp lookup
36+
37+
# Use the translate-docs-action
38+
- name: Translate documentation
39+
uses: ./.github/actions/translate-docs
40+
with:
41+
# Required inputs
42+
api_key: ${{ secrets.OPENAI_API_KEY }}
43+
# Optional inputs with their default values shown
44+
github_token: ${{ secrets.GITHUB_TOKEN }}
45+
custom_arguments: ${{ github.event.inputs.custom_arguments }}
46+
# translation_command: 'pnpm run translate'
47+
# base_branch: 'main'
48+
# pr_branch: 'docs/update-translations'
49+
# pr_title: 'Update translations'
50+
# The following uses YAML pipe syntax for multi-line strings
51+
# pr_body: |
52+
# This PR updates the documentation translations automatically.
53+
#
54+
# Generated by the translate workflow.
55+
# commit_message: 'docs: update documentation translations'
56+
# add_paths: 'apps/docs/content/**'
57+
enable_formatting: 'false'
58+
# 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 & 2 deletions
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": "node ../../packages/translate/dist/index.js",
1212
"check-types": "tsc --noEmit"
1313
},
1414
"dependencies": {
@@ -23,7 +23,6 @@
2323
"zod": "3.24.4"
2424
},
2525
"devDependencies": {
26-
"@next-i18n/translate": "workspace:*",
2726
"@next/bundle-analyzer": "^15.3.2",
2827
"@next/env": "^15.3.2",
2928
"@oramacloud/client": "^2.1.4",

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
"main": "index.js",
66
"scripts": {
77
"format": "biome format --write .",
8+
"build:packages": "pnpm --filter @next-i18n/translate build",
89
"lint": "biome lint .",
910
"lint:fix": "biome lint --write .",
10-
"dev": "pnpm --filter @next-i18n/translate --filter @next-i18n/docs --parallel dev"
11+
"dev": "pnpm --filter @next-i18n/translate --filter @next-i18n/docs --parallel dev",
12+
"translate": "pnpm --filter @next-i18n/docs translate"
1113
},
1214
"keywords": [],
1315
"devDependencies": {
@@ -16,5 +18,6 @@
1618
"@commitlint/config-conventional": "^19.8.1"
1719
},
1820
"author": "",
19-
"license": "ISC"
21+
"license": "ISC",
22+
"packageManager": "[email protected]"
2023
}

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)