Skip to content

About

Translate docstrings in Julia into your preferred language.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Latest commit

 

History

94 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DocstringTranslation.jl

Build Status Stable Dev Aqua QA DeepWiki

Description

This Julia package inserts Large Language Model (LLM) hooks into the API in the Base.Docs module, giving non-English speaking users the opportunity to help smooth API comprehension.

Prerequisite

Install Julia

Install Julia using juliaup.

$ curl -fsSL https://install.julialang.org | sh -s -- --yes

Get OpenAI API Key

Currently, this package utilizes OpenAI.jl, a OpenAPI wrapper for Julia. Please prepare API key. See the following resources to learn more

Set OPENAI_API_KEY environment variable

To use the OpenAI API, you'll need to set your API key as an environment variable named OPENAI_API_KEY. Create a file named .env the current working directory. Add the following line to the .env file, replacing <your_api_key> with your actual API key:

OPENAI_API_KEY=sk-<your_api_key>

Use the DotEnv.jl package to load the environment variables from the .env file:

using Pkg; Pkg.add("DotEnv")
using DotEnv

DotEnv.load!()

To ensure the API key is set correctly, use the following Julia code:

@assert haskey(ENV, "OPENAI_API_KEY")

This assertion will throw an error if the OPENAI_API_KEY environment variable is not defined.

Usage

$ cd path/to/directory
$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.11.1 (2024-10-16)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using Pkg; Pkg.activate("."); Pkg.instantiate()

julia> using DocstringTranslation

julia> using DotEnv

julia> DotEnv.load!()

julia> @switchlang! :Japanese

julia> @doc pi
  π
  pi

  定数pi。

  Unicode πはJulia
  REPLで\piと入力してからタブキーを押すことで入力できます。また、多くのエディタでも同様です。

  参照: sinpi、sincospi、deg2rad。

  例
  ≡≡

  julia> pi
  π = 3.1415926535897...

  julia> 1/2pi
  0.15915494309189535

julia> @switchlang! :German

julia> # You can also ask from help mode

help?> ℯ
julia> @doc ℯ
  ℯ
  e

  Die Konstante ℯ.

  Unicode ℯ kann eingegeben werden, indem man \euler schreibt und die
  Tabulatortaste im Julia REPL und in vielen Editoren drückt.

  Siehe auch: exp, cis, cispi.

  Beispiele
  ≡≡≡≡≡≡≡≡≡

  julia> ℯ
  ℯ = 2.7182818284590...

  julia> log(ℯ)
  1

  julia> ℯ^(im)π ≈ -1
  true

julia>

Translate documentation pages concurrently

Before Documenter.makedocs, prepare translations with bounded asynchronous workers. This overlaps API wait time even with one Julia thread:

using DocstringTranslation
using DotEnv
DotEnv.load!() # load OPENAI_API_KEY from .env
@switchlang! "ja"

source = joinpath(@__DIR__, "src") # in docs/make.jl
paths = [joinpath(dir, file) for (dir, _, files) in walkdir(source)
         for file in files if endswith(file, ".md")]
result = DocstringTranslation.translate_pages(paths; concurrency=32)
for (path, err) in result.failed
    @error "Page translation failed" path exception=err
end
isempty(result.failed) || error("Fix failed pages and rerun before building")
# Documenter.makedocs(...)

translated and cached list successful absolute paths; failed pairs paths with exceptions. Completed pages stay cached, so rerunning resumes failed work. Repeated paths and symlinks are processed once within a batch. Whole-page Markdown protection and validation remain enabled. Use a lower concurrency if your provider limits simultaneous requests; the worker count does not impose a requests-per-minute limit. Configure the language, model, target package and cache root before starting, and keep them fixed until the batch completes.

Model fallback

Validated page/docstring translation normally uses GPT-6 Luna. After up to three failed primary attempts, only that document is tried once with GPT-6.1 Sol (medium reasoning). DocstringTranslation.switchfallbackmodel!(nothing) disables escalation; set a model string to enable it. switchmodel! selects the primary. Successful cache entries remain reusable regardless of which model produced them.

Custom request callbacks do not trigger automatic network escalation unless an explicit fallback_request is supplied. Diagnostics identify the model used in each attempt, and final errors retain both causes. DOCSTRINGTRANSLATION_REQUEST_TIMEOUT_SECONDS sets a positive integer HTTP read timeout (default 180 seconds). HTTP transport retries are disabled; validation retries and model escalation are explicit.

Translation failures and validation

Validation allows natural word order, emphasis changes and regrouping adjacent prose paragraphs. Code, mathematics, links, headings and structural containers (list items, table cells, quotes and admonitions) remain protected.

The @switchlang! hooks use pristine original Markdown when a translation fails, so a provider outage or invalid response does not stop a documentation build. Each failed source is reported once per process/configuration, and fallback text is never cached as a successful translation. Direct translation calls and translate_pages retain strict failure reporting.

Inspect DocstringTranslation.translation_failures() for source identities and sanitized reasons. Call DocstringTranslation.clear_translation_failures!() between builds to retry failed sources in the same process. Starting a new Julia process also retries them. Concurrent requests for the same cache key share a lock.

Set DOCSTRINGTRANSLATION_DIAGNOSTICS_DIR to retain validation failures as separate original.md, protected.md, response.md and error.txt diagnostic files. Errors report the source, first structural mismatch, attempt count and artifact directory. These diagnostic files are not successful translation cache entries.

Appendix

If you are using 1PassWord, op command is good for you. Store the following content instead of writing API key directly:

# .env
OPENAI_API_KEY=op://Personal/OpenAI API Key/api key

To launch julia run the following command:

$ op run --env-file=./.env -- julia

In this case, you don't have to load DotEnv package:

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.11.5 (2025-04-14)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using DocstringTranslation; @switchlang! :Japanese; @doc exp

About

Translate docstrings in Julia into your preferred language.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages