Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions .github/workflows/translate-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ on:
required: false
type: string
api_provider:
description: 'AI API provider for translation'
description: 'AI API provider for translation (OpenAI-compatible)'
required: true
type: choice
options:
- openai
- anthropic
default: openai
specific_files:
description: 'Specific files to translate (space-separated, relative to docs/en/). Example: index.md start/installation.md'
Expand Down Expand Up @@ -51,35 +50,47 @@ jobs:
with:
python-version: '3.12'

# The package itself renders the generated blocks after translating.
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install openai anthropic
pip install . openai

- name: Configure Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"

# The next step opens the pull request, so the script never does (--no-pr).
- name: Run translation
env:
TRANSLATION_API_KEY: ${{ secrets.TRANSLATION_API_KEY }}
API_PROVIDER: ${{ inputs.api_provider }}
TARGET_LANGUAGE: ${{ inputs.target_language }}
SPECIFIC_FILES: ${{ inputs.specific_files }}
run: |
ARGS="--api-provider ${{ inputs.api_provider }}"
ARGS=(--api-provider "$API_PROVIDER" --no-pr)

if [ -n "${{ inputs.target_language }}" ]; then
ARGS="$ARGS --target-lang ${{ inputs.target_language }}"
if [ -n "$TARGET_LANGUAGE" ]; then
ARGS+=(--target-lang "$TARGET_LANGUAGE")
fi

if [ -n "${{ inputs.specific_files }}" ]; then
ARGS="$ARGS --files ${{ inputs.specific_files }}"
if [ -n "$SPECIFIC_FILES" ]; then
read -r -a FILES <<< "$SPECIFIC_FILES"
ARGS+=(--files "${FILES[@]}")
fi

if [ "${{ inputs.create_pr }}" = "false" ]; then
ARGS="$ARGS --no-pr"
fi
python scripts/translate.py "${ARGS[@]}"

python scripts/translate.py $ARGS
- name: Render generated blocks
env:
TARGET_LANGUAGE: ${{ inputs.target_language }}
run: |
if [ -n "$TARGET_LANGUAGE" ]; then
python scripts/docs_blocks.py --lang "$TARGET_LANGUAGE"
else
python scripts/docs_blocks.py
fi

- name: Create Pull Request
if: inputs.create_pr == true
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Every 401 carries `WWW-Authenticate`, and each project reads its configuration f

- The documentation is reorganised into Start, Learn, How-to, Reference and Contributing; the Learn track follows one project from `hello` to `production`, and old page addresses redirect to their new pages.
- CLI reference tables and help texts are generated from the catalog and the CLI, and the configuration-file and environment-variable sections that described settings fastkit never read are removed.
- The documentation translation script leaves code and generated blocks untouched, skips hand-translated pages and fails when a page could not be translated; its workflow offers only the provider the script supports.

### Upgrade notes for existing projects

Expand Down
36 changes: 10 additions & 26 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Key commands for contributors:
- `make serve-docs` - Serve documentation locally

#### Translation
- `make translate` - Translate documentation (supports LANG, PROVIDER, MODEL parameters)
- `make translate` - Translate documentation (supports LANG and MODEL parameters)

### Development Workflow

Expand Down Expand Up @@ -434,12 +434,12 @@ FastAPI-fastkit documentation supports multiple languages. Contributing translat

```bash
pdm install -G docs -G translation
# Or: pip install mkdocs mkdocs-material mkdocs-static-i18n openai anthropic
# Or: pip install mkdocs mkdocs-material mkdocs-static-i18n openai
```

#### 2. Set Up API Key

Get an API key from OpenAI or Anthropic and set it as an environment variable:
Get an OpenAI API key (or a key for another OpenAI-compatible API, used with `--api-base-url`) and set it as an environment variable:

```bash
export TRANSLATION_API_KEY="your-api-key-here"
Expand All @@ -466,9 +466,8 @@ make translate
# Translate to specific language
make translate LANG=ko

# Specify API provider and model
make translate LANG=ko PROVIDER=github MODEL=gpt-4o-mini
make translate LANG=ko PROVIDER=openai MODEL=gpt-4
# Specify the model
make translate LANG=ko MODEL=gpt-4
```

#### Using Script Directly
Expand Down Expand Up @@ -523,24 +522,14 @@ You can also translate manually:

To add support for a new language:

1. **Update `scripts/translation_config.json`**:
```json
{
"code": "pt",
"name": "Portuguese",
"native_name": "Português",
"enabled": true
}
```

2. **Update `mkdocs.yml`**:
1. **Update `mkdocs.yml`**:
```yaml
- locale: pt
name: Português
build: true
```

3. **Run translation**:
2. **Run translation**:
```bash
python scripts/translate.py --target-lang pt
```
Expand All @@ -550,19 +539,14 @@ python scripts/translate.py --target-lang pt
**Core Components:**

- **`scripts/translate.py`**: AI-powered translation script
- Supports OpenAI GPT-4 and Anthropic Claude
- Preserves markdown formatting and code blocks
- Calls the OpenAI API, or another OpenAI-compatible API with `--api-base-url`
- Sends only the prose: code blocks and generated blocks stay unchanged
- Creates GitHub PRs automatically

- **`scripts/setup-i18n-structure.sh`**: Migration tool
- Moves docs to language-specific directories
- Creates symbolic links for shared assets

- **`scripts/translation_config.json`**: Configuration
- Language settings
- Translation options
- Quality check parameters

- **`.github/workflows/translate-docs.yml`**: GitHub Actions workflow
- Automated translation on trigger
- Manual workflow dispatch
Expand All @@ -587,7 +571,7 @@ docs/

**Import Error:**
```bash
pip install openai anthropic
pip install openai
```

**API Key Error:**
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ all: ## Run complete development workflow
$(MAKE) build

# Translation commands
translate: ## Translate documentation - Usage: make translate LANG=ko PROVIDER=github MODEL=gpt-4o-mini
translate: ## Translate documentation - Usage: make translate LANG=ko MODEL=gpt-4o-mini
@echo "Starting translation..."
@CMD="python scripts/translate.py"; \
if [ -n "$(LANG)" ]; then CMD="$$CMD --target-lang $(LANG)"; fi; \
if [ -n "$(PROVIDER)" ]; then CMD="$$CMD --api-provider $(PROVIDER)"; fi; \
if [ -n "$(MODEL)" ]; then CMD="$$CMD --model $(MODEL)"; fi; \
echo "Running: $$CMD"; \
$$CMD
Expand Down
2 changes: 1 addition & 1 deletion docs/en/contributing/template-creation-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ app = FastAPI(

`tests/test_templates/test_concept_comments.py` checks every rule in this section.

## Checks
## Checks {#checks}

### Inspect a template locally

Expand Down
55 changes: 39 additions & 16 deletions docs/en/contributing/translation-guide.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# Translation guide

The documentation is published in seven languages: English (`en`), Korean (`ko`), Japanese (`ja`), Chinese (`zh`), Spanish (`es`), French (`fr`) and German (`de`). This page explains what to translate, what stays in English, and how the generated tables, the catalog texts and the web generator page are translated.
The documentation is published in seven languages: English (`en`), Korean (`ko`), Japanese (`ja`), Chinese (`zh`), Spanish (`es`), French (`fr`) and German (`de`). This page explains what to translate, what stays in English, how the generated tables, the catalog texts and the web generator page are translated, and how the tests check a translation.

## English is the source

Every page is written in English first, in `docs/en/`. Each language has the same page at the same path under `docs/<lang>/`, for example `docs/en/learn/generate.md` and `docs/ko/learn/generate.md`. Every page exists in all seven languages; when you add or change an English page, the six translations follow in the same pull request or the next one.

When a language has no file for a page, the site shows the English page in that language's menu. Treat that as a gap to fill, not as the normal state.

`CHANGELOG.md` stays in English. Each `docs/<lang>/changelog.md` contains only `{!CHANGELOG.md!}`, which includes the English file.
`CHANGELOG.md` stays in English. Each `docs/<lang>/changelog.md` contains only `\{!CHANGELOG.md!}`, which includes the English file.

## What stays in English

- Everything the command line prints or asks: prompts, messages, tables and help texts. Readers compare the page with their terminal.
- Everything the command line prints or asks: prompts, messages, tables, help texts and other output. Readers compare the page with their terminal.
- Commands, options, file names, paths, code, configuration keys and package names, inside and outside code blocks.
- Code blocks. Every fenced block stays exactly as in English, comments and sample output included.
- The `cli` generated blocks (the `--help` texts on the CLI reference page) and the check titles of the `doctor-checks` table. The script renders them in English in every language.
- Text inside images. Images are shared files in `docs/img/`; every language uses the same image, so link to it and never make a translated copy.
- The template-id headings on the example projects page (`## fastapi-auth-jwt` and the others). Their anchors are redirect targets.
- Link targets and heading ids. Some headings end with an id such as `{#features}`, because other pages and redirects link to it. Translate the heading text and keep the id. The template-id headings on the example projects page (`## fastapi-auth-jwt {#fastapi-auth-jwt}` and the others) stay as they are.

Translate the prose around them, headings, admonition text and link texts. Keep relative links as they are: the paths are the same in every language.
Translate the prose around them, headings, admonition text, link texts and the alt text of every image. Keep relative links as they are: the paths are the same in every language.

## Generated blocks

Expand All @@ -37,9 +38,9 @@ $ python scripts/docs_blocks.py --lang ko
$ python scripts/docs_blocks.py --check
```

Catalog blocks (`levels`, `features`, `rules`, `package-managers`, `examples`, `concepts`) take their texts from the catalog overlay of the page's language, described below. The fixed words of the tables, such as the column headings, come from the `HEADINGS` table at the top of `scripts/docs_blocks.py`; a language without an entry there gets the English words, so add one for yours. The `cli` blocks and the check titles in the `doctor-checks` table stay in English, because they are the command line's own text.
Catalog blocks (`levels`, `features`, `rules`, `package-managers`, `examples`, `concepts`) take their texts from the catalog overlay of the page's language, described below. The fixed words of the tables come from the `HEADINGS` table at the top of `scripts/docs_blocks.py`: column headings, short sentences such as `{when} needs {require}.`, and the punctuation and joins of lists (`separator`, `join_or`, `join_and`), so that a language such as Japanese can drop the spaces around them. A translated language needs an entry with every key of `en` and the same `{placeholders}`; a language without an entry gets the English words. The `cli` blocks and the check titles in the `doctor-checks` table stay in English, because they are the command line's own text.

## Catalog overlays
## Catalog overlays {#catalog-overlays}

Level descriptions, feature labels and summaries, rule messages, concept titles and the Next steps texts come from the catalog. The English text is in `src/fastapi_fastkit/catalog/catalog.toml`; each language has an overlay at `src/fastapi_fastkit/catalog/i18n/<lang>.toml` with the same keys:

Expand Down Expand Up @@ -97,27 +98,39 @@ A label that is not listed stays in English. When the English `nav` gets a new s

## Machine translation

`scripts/translate.py` sends English pages to an OpenAI-compatible API and writes the result to `docs/<lang>/` at the same path. It needs the `translation` dependency group (`pdm install -G translation`) and an API key in `--api-key`, `TRANSLATION_API_KEY` or, with `--api-provider github`, `GITHUB_TOKEN`.
`scripts/translate.py` sends English pages to the OpenAI API and writes the result to `docs/<lang>/` at the same path. Another OpenAI-compatible API works too: pass its address with `--api-base-url`. It needs the `translation` dependency group (`pdm install -G translation`) and an API key in `--api-key` or `TRANSLATION_API_KEY`.

```console
$ python scripts/translate.py --help
usage: translate.py [-h] [--target-lang TARGET_LANG]
[--api-provider {openai,github}] [--api-key API_KEY]
[--api-base-url API_BASE_URL] [--model MODEL] [--no-pr]
[--files FILES [FILES ...]] [--docs-dir DOCS_DIR]
[--max-retries MAX_RETRIES] [--retry-delay RETRY_DELAY]
usage: translate.py [-h] [--target-lang TARGET_LANG] [--api-provider {openai}]
[--api-key API_KEY] [--api-base-url API_BASE_URL]
[--model MODEL] [--no-pr] [--files FILES [FILES ...]]
[--docs-dir DOCS_DIR] [--max-retries MAX_RETRIES]
[--retry-delay RETRY_DELAY]
[--request-delay REQUEST_DELAY]
...
```

Use it with care:

- Without `--files` it translates every `.md` file under `docs/en/` and overwrites the existing translations. Pass the pages you mean with `--files` (paths relative to `docs/en/`), and leave out `start/web-generator.md`, whose rules are above.
- It translates the whole file, generated blocks included. Run `python scripts/docs_blocks.py --lang <lang>` afterwards to restore them.
- Without `--files` it translates every `.md` file under `docs/en/` and overwrites the existing translations. Pass the pages you mean with `--files` (paths relative to `docs/en/`).
- It never translates `start/web-generator.md`, whose rules are above, or `changelog.md`.
- Code blocks, generated blocks and front matter are not sent to the API and come back unchanged, with the file's line endings. Run `python scripts/docs_blocks.py --lang <lang>` afterwards to render the generated blocks in your language.
- When a page cannot be translated, for example because the answer lost one of the placeholder lines that stand for the code, the script writes the other pages and then exits with status 1.
- Without `--no-pr` it creates a branch, commits, pushes and opens a pull request with `gh`, then switches to `main`. Pass `--no-pr` to review the files locally first.
- A fluent speaker reviews every machine translation before it is merged.

`make translate LANG=ko PROVIDER=github MODEL=openai/gpt-4o-mini` runs the same script.
`make translate LANG=ko MODEL=gpt-4o-mini` runs the same script. The "Translate Documentation" workflow on GitHub runs it, renders the generated blocks and opens the pull request.

## Translate a language

A language is either pending, with its old 1.x pages still in place, or translated. The pending languages are listed in `PENDING_LOCALES` in `tests/test_docs/docs_support.py`. The pull request that translates a language:

1. Translates every page of `docs/en/` into `docs/<lang>/` at the same path, including the alt text of every image.
2. Deletes the pages of `docs/<lang>/` that have no English page. Their old addresses redirect to the new pages.
3. Adds the language's entry to `HEADINGS` in `scripts/docs_blocks.py`, with every key of `en`.
4. Renders the generated blocks with `python scripts/docs_blocks.py --lang <lang>`.
5. Removes the language from `PENDING_LOCALES`, so that the checks below run for it.

## Check your translation

Expand All @@ -127,4 +140,14 @@ $ python -m pytest tests/test_docs tests/test_catalog -q
$ mkdocs build --strict -d site
```

For every language that is not pending, `tests/test_docs/test_locale_parity.py` compares each page with the English page and names the page and the problem:

- the same pages as `docs/en/`, no more and no fewer, and `changelog.md` identical to the English file;
- the same generated blocks in the same order, rendered for the language, and the same front matter;
- the same code blocks, with identical content;
- the same heading levels in the same order, the same heading ids, and the same admonitions and tabs (`!!!`, `???`, `===`) with their types;
- the same images in the same order, each with an alt text, and the same link targets;
- every link to a `#fragment` reaches a heading of that language's page;
- translated prose: a page that is the English text fails, and so does a sentence of four words or more left in English (outside tables).

The strict build fails on a link to a page that exists neither in your language nor in English. Open the built pages of your language and check the menu labels, the generated tables and the web generator page.
10 changes: 5 additions & 5 deletions docs/en/how-to/example-projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Besides the five levels, fastkit ships five example projects. Each one shows a s

The endpoint lists come from the `/openapi.json` of each project, started with `uv run uvicorn` right after `uv sync`. Every example also answers `GET /health` with `{"status":"ok"}`, which the tables leave out.

## fastapi-custom-response
## fastapi-custom-response {#fastapi-custom-response}

One response envelope for every JSON answer, errors included: `timestamp`, `status`, `code`, `path` and `message`. Exception handlers wrap `HTTPException`, validation errors and unhandled exceptions in the same envelope. The items live in a JSON file (`src/mocks/mock_items.json`), read and written asynchronously. It needs no setup beyond `uv sync`. The request after the table shows a 404 in the envelope.

Expand Down Expand Up @@ -46,7 +46,7 @@ $ curl -s http://127.0.0.1:8000/api/v1/items/999

<!-- /fastkit:generated -->

## fastapi-auth-jwt
## fastapi-auth-jwt {#fastapi-auth-jwt}

Accounts with access and refresh tokens. Refresh tokens rotate on every use, passwords are hashed with Argon2id, and dependencies guard routes by role and by scope. Users are stored with SQLModel; the login, register and refresh endpoints are rate limited.

Expand Down Expand Up @@ -78,7 +78,7 @@ Setup: none for a first run. The app uses a local SQLite file (`DATABASE_URL=sql

<!-- /fastkit:generated -->

## fastapi-sqlmodel
## fastapi-sqlmodel {#fastapi-sqlmodel}

SQLModel over an async engine, Alembic migrations, a generic CRUD base class and paginated list endpoints. One `DATABASE_URL` selects SQLite, PostgreSQL or MySQL, and the tests stay on SQLite.

Expand Down Expand Up @@ -115,7 +115,7 @@ For PostgreSQL or MySQL, the README section "Use PostgreSQL or MySQL" starts a l

<!-- /fastkit:generated -->

## fastapi-mcp
## fastapi-mcp {#fastapi-mcp}

A FastAPI app whose item endpoints are also MCP (Model Context Protocol) tools: `list_items`, `get_item`, `create_item`, `replace_item`, `update_item` and `delete_item`. Users log in with the OAuth2 password flow. Item reads are public; writes need the owner's token.

Expand Down Expand Up @@ -155,7 +155,7 @@ Setup: the user store is empty until you plug in your own (`get_users_db()` in `

<!-- /fastkit:generated -->

## fastapi-llm-agent
## fastapi-llm-agent {#fastapi-llm-agent}

A Claude chat agent that streams its reply over server-sent events and runs a tool-call loop with two bundled tools (`calculator`, `current_time`). Conversations are kept in memory behind an interface you can replace. The tests replace the Anthropic client with a scripted fake, so they need no key and no network.

Expand Down
2 changes: 1 addition & 1 deletion docs/en/how-to/scripts-and-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ description = "[FastAPI-fastkit templated]"

An invalid value is reported (`Config key 'version' must be a PEP 440 version such as 1.0.0, got 'one'`) and nothing is written.

## Configurations saved by fastkit 1.x
## Configurations saved by fastkit 1.x {#configurations-saved-by-fastkit-1x}

Files written by fastkit 1.x keep working. A `minimal` preset (or no preset) creates a `fastapi-single-module` project, and the CORS and Pagination utilities, which every project now includes, are accepted. For a 1.x file `old.json` with `"architecture_preset": "minimal"` and `"utilities": ["CORS", "Pagination"]`:

Expand Down
Loading
Loading