> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nika.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Localization factory

> T3 fan-out · product / i18n: the whole docs tree translated, voice intact, zero copy-paste.

> **T3 fan-out · product / content / i18n**: three chained fan-outs
> (read-all → translate-all → write-all) with jq `transpose` zipping
> paths back to texts between stages. The masterclass file for
> collections.

## The job

« Can we have the docs in French? » is weeks of copy-paste, or it's this
file: glob finds every markdown file, the texts are read and translated
in parallel (rate-limited so the provider doesn't choke), and the mirror
tree appears under `i18n/fr/` with the original paths preserved.

## The shape

```mermaid theme={"system"}
flowchart LR
  files["files · nika:glob"]:::invoke
  texts["texts · ∥ for_each · nika:read"]:::invoke
  pairs["pairs · nika:jq"]:::invoke
  translated["translated · ∥ for_each"]:::infer
  bundle["bundle · nika:jq"]:::invoke
  mirror["mirror · ∥ for_each · nika:write"]:::invoke
  files --> texts
  files --> pairs
  texts --> pairs
  pairs --> translated
  pairs --> bundle
  translated --> bundle
  bundle --> mirror
  classDef infer fill:#5b8cff22,stroke:#5b8cff,color:#5b8cff
  classDef invoke fill:#22d3ee22,stroke:#22d3ee,color:#22d3ee
```

## The file

```yaml localization-factory.nika theme={"system"}
nika: localization-factory

model: ollama/qwen3.5:4b   # local default · swap for mistral/mistral-large-latest (EU model for EU locales)

const:
  lang: "fr"
  # The committed rehearsal tree. Point this at ./docs (or wherever yours
  # lives) and change `permits.fs.read` to match, in the same edit.
  source_root: "./examples/fixtures/docs"

permits:
  tools: ["nika:glob", "nika:jq", "nika:read", "nika:write"]
  fs:
    # `nika:glob` opens the ROOT of its pattern, so the bound has to cover
    # the directory AND everything under it — `**` does both (it matches the
    # dir itself and any descendant at any depth). A bound naming only the
    # `.md` files refuses the glob before it lists anything:
    # `NIKA-SEC-004 · ./examples/…/docs resolves outside permits.fs.read`.
    read: ["./examples/fixtures/docs/**"]
    # The mirror is as deep as the source tree, so this one earns its `**`:
    # every locale lands under ./i18n/ and nothing lands outside it. Naming
    # the locale (`./i18n/fr/**`) would be tighter but would have to be
    # re-edited for every value of `const.lang` — the bound is the tree the
    # factory owns.
    write: ["./i18n/**"]

tasks:
  # ── the collection is discovered, never declared ────────────────────
  files:
    invoke:
      tool: "nika:glob"
      args:
        pattern: "${{ const.source_root }}/**/*.md"
        exclude: ["**/node_modules/**"]

  # ── stage 1 · read every file, 8 at a time ─────────────────────────
  texts:
    with:
      files: ${{ tasks.files.output }}
    for_each:
      items: ${{ with.files }}
      max_parallel: 8                    # local disk · the only cost is file handles
    invoke:
      tool: "nika:read"
      args: { path: "${{ item }}" }

  # ── the zip · path + text, and the source root comes off ───────────
  # `texts` is an array in the SAME ORDER as `files`, so transpose pairs
  # them index-for-index. `ltrimstr` makes each path relative to the source
  # root — that stripped path is what the mirror tree is built from.
  pairs:
    with:
      files: ${{ tasks.files.output }}
      texts: ${{ tasks.texts.output }}
    invoke:
      tool: "nika:jq"
      args:
        input: ["${{ with.files }}", "${{ with.texts }}"]
        expression: >-
          transpose
          | map({ path: (.[0] | ltrimstr("${{ const.source_root }}/")), text: .[1] })

  # ── stage 2 · translate every file, 3 at a time ────────────────────
  translated:
    with:
      pairs: ${{ tasks.pairs.output }}
    for_each:
      items: ${{ with.pairs }}
      max_parallel: 3                    # rate-limit the provider, not the disk
      fail_fast: false                   # finish the batch · one bad file is not a batch failure
    on_error:
      recover: null                    # null holds the index open so the zip below stays aligned
    infer:
      # a reasoning model needs 16384 here, or a documented run.reasoning.effort: low
      max_tokens: 4000                 # a doc page, not a book · the cost ceiling is a real number
      prompt: |
        Translate to ${{ const.lang }} · keep the markdown structure, leave
        code blocks untouched, and keep the original tone ·
        ${{ item.text }}

  # ── the fan-in · drop the files that failed, keep their paths ──────
  # Second transpose, same law: `translated` is index-aligned with `pairs`
  # because `recover: null` filled the failures in place. `select(.[1] !=
  # null)` is where a failed translation leaves the batch — by VALUE, after
  # the fact, not by aborting the fan-out.
  bundle:
    with:
      pairs: ${{ tasks.pairs.output }}
      translated: ${{ tasks.translated.output }}
    invoke:
      tool: "nika:jq"
      args:
        input: ["${{ with.pairs }}", "${{ with.translated }}"]
        expression: >-
          transpose
          | map(select(.[1] != null))
          | map({ path: .[0].path, text: .[1] })

  # ── stage 3 · write the mirror ─────────────────────────────────────
  mirror:
    with:
      bundle: ${{ tasks.bundle.output }}
    for_each:
      items: ${{ with.bundle }}
      max_parallel: 8
    invoke:
      tool: "nika:write"
      args:
        path: "./i18n/${{ const.lang }}/${{ item.path }}"
        content: "${{ item.text }}"
        create_dirs: true              # the mirror's subdirectories do not exist yet

outputs:
  translated:
    value: ${{ tasks.bundle.output }}
    description: "Every file that made it through, as {path, text} · the mirror's manifest"
```

## How it works

**1. The filesystem is the collection**

`nika:glob` returns the file list at runtime: add a doc tomorrow,
it's in the next run. `exclude:` keeps `node_modules` out.

**2. transpose zips parallel arrays**

Two fan-outs produce two aligned arrays (paths, texts). The jq
`transpose | map({path: .[0], text: .[1]})` zip turns them into
`[{path, text}]`, and `${{ item.path }}` works, because loop-locals
are full objects.

**3. The mirror tree comes out the other end**

The final fan-out writes `./i18n/fr/<original path>` with
`create_dirs: true`. Path templating is plain `${{ }}` interpolation
inside a string.

## Constructs you just used

| Construct | Where | Reference |
| - | - | - |
| `nika:glob` + `exclude` | `files` | [Builtins](https://docs.nika.sh/reference/builtins.md) |
| chained `for_each` stages | `texts` → `translated` → `mirror` | [Workflows](https://docs.nika.sh/concepts/workflows.md) |
| jq `transpose` zip | `pairs` · `bundle` | [Builtins](https://docs.nika.sh/reference/builtins.md) |
| `${{ item.path }}` object locals | `mirror` | [Bindings](https://docs.nika.sh/concepts/bindings.md) |

## Run it

> [!NOTE]
>
> **What it needs:** A local model ([Ollama](https://ollama.com) serving `qwen3.5:4b`) and the two sample documents.

**1. Rehearse it**

```bash theme={"system"}
nika try localization-factory
```

A mock model answers, the sample files come with it, and nothing is written in your folder.

**2. Get your own copy**

Save the file above as `localization-factory.nika` in a new folder, then download the samples it reads:

```bash theme={"system"}
FIXTURES=https://raw.githubusercontent.com/supernovae-st/nika/v0.124.1/crates/nika-pack/pack/examples/fixtures
mkdir -p examples/fixtures/docs examples/fixtures/docs/guides
curl -fsSLo examples/fixtures/docs/quickstart.md "$FIXTURES/docs/quickstart.md"
curl -fsSLo examples/fixtures/docs/guides/deploying.md "$FIXTURES/docs/guides/deploying.md"
```

**3. Check it, then run it**

```bash theme={"system"}
nika check localization-factory.nika
nika run localization-factory.nika --model mock/echo   # a mock model, no key
ollama pull qwen3.5:4b   # once, for the real run
nika run localization-factory.nika
```

The run writes the French mirror under `i18n/fr/`.

Any other model works: pass `--model <provider>/<name>` with its key, and cap a cloud model with `--max-cost-usd`.

To translate your own docs, change `const.source_root` and the matching `permits.fs.read` entry together (`NIKA-AUTH-007`).

## Make it yours

* All your locales: wrap the lang in a list and run per locale, or lift `const.lang` to a typed required input and call the workflow per language.
* Glossary discipline: interpolate your terminology table into the translate prompt so product names never drift.
* Only what changed: replace `glob` with `exec: { command: [git, diff, --name-only] }` (argv form, with `exec: ["git"]` in `permits`) and translate the delta, as [PR review fan-out](https://docs.nika.sh/examples/pr-review-fanout.md) does. On macOS in 0.124.1 the sandbox refuses that Git read ([Known limits](https://docs.nika.sh/changelog/known-limits.md#git-inside-a-workflow-is-refused-on-macos)).

> [card] [Next · Config drift sentinel](https://docs.nika.sh/examples/config-drift-sentinel.md)
>
> RFC 7396 merge-patch + RFC 6902 diff: jq decides, the model only
> explains.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.