Skip to content

Commit 08e3bc7

Browse files
author
vke
committed
enable sphinx-i18n
1 parent b28f768 commit 08e3bc7

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ genrst/
6565
stderr-ldview
6666
stdout-ldview
6767
LPub3D/
68+
69+
# Translation files
70+
######################
71+
doc/locales/pot/
72+
*.mo

CONTRIBUTING.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,47 @@ Build docs:
149149
poetry run doc\make.bat html
150150
Invoke-Item doc\main\build\html\index.html
151151

152+
Translations:
153+
154+
The documentation supports multiple languages through Sphinx's internationalization (i18n) feature.
155+
156+
Directory structure:
157+
- `doc/locales/pot/` - Contains template (.pot) files generated by `make gettext`. These files contain all original strings that need to be translated.
158+
- `doc/locales/<language>/LC_MESSAGES/` - Contains translation (.po) files for each language. These files must be directly in the LC_MESSAGES directory (not in subdirectories) and contain the actual translations that will be used to build the documentation.
159+
160+
Note about translations:
161+
The translation system uses unique IDs (UUIDs) for each translatable string. This ensures that translations are preserved even if you move text to different files or lines. The source file locations are included as comments in the .po files to help track where translations are used.
162+
163+
Translation files:
164+
- `.po` files are human-readable text files containing the translations. These should be committed to the repository.
165+
- `.mo` files are compiled binary versions of .po files, generated automatically during build. These should not be committed.
166+
167+
To work with translations:
168+
169+
1. Generate translation templates:
170+
171+
poetry run make -C doc gettext
172+
173+
This creates .pot template files in `doc/locales/pot/`
174+
175+
2. Initialize or update a language (e.g., German 'de'):
176+
177+
poetry run make -C doc update-po-de
178+
179+
This creates or updates .po files in `doc/locales/de/LC_MESSAGES/`
180+
181+
3. Edit the .po files in `doc/locales/<language>/LC_MESSAGES/` to add translations
182+
183+
4. Build documentation for a specific language:
184+
185+
poetry run make -C doc html-de # For German
186+
poetry run make -C doc html-fr # For French
187+
poetry run make -C doc html-ja # For Japanese
188+
189+
When adding translations to the repository:
190+
- Commit the .po files in `doc/locales/<language>/LC_MESSAGES/`
191+
- Do not commit the .pot files in `doc/locales/pot/` (these are generated files)
192+
152193
Building IDE docs variant:
153194

154195
# Linux/macOS

doc/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ SPHINXPROJ = Pybricks
88
SOURCEDIR = main
99
BUILDDIR = "$(SOURCEDIR)"/build
1010
TAG = main
11+
LOCALEDIR = locales
1112

1213
# Put it first so that "make" without argument is like "make help".
1314
help:
@@ -19,7 +20,19 @@ diagrams:
1920
@$(MAKE) -C "$(SOURCEDIR)"/diagrams_source clean
2021
@$(MAKE) -C "$(SOURCEDIR)"/diagrams_source
2122

23+
# i18n targets
24+
gettext:
25+
@$(SPHINXBUILD) -b gettext "$(SOURCEDIR)" "$(LOCALEDIR)/pot" -D gettext_compact=0 $(SPHINXOPTS) -t $(TAG) $(O)
26+
27+
update-po-%:
28+
@sphinx-intl update -p "$(LOCALEDIR)/pot" -l $*
29+
30+
html-%:
31+
@$(SPHINXBUILD) -b html "$(SOURCEDIR)" "$(BUILDDIR)/$*" $(SPHINXOPTS) -t $(TAG) -D language=$* $(O)
32+
2233
# Catch-all target: route all unknown targets to Sphinx using the new
2334
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
2435
%: Makefile
2536
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) -t $(TAG) $(O)
37+
38+
.PHONY: gettext update-po-% html-%

doc/main/conf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353
extensions.append("sphinx.ext.imgmath") # noqa F821
5454
html_theme_options["prev_next_buttons_location"] = None # noqa F821
5555

56+
# Internationalization configuration
57+
language = 'en'
58+
locale_dirs = ['../locales'] # path relative to conf.py location
59+
gettext_compact = False # optional
60+
gettext_uuid = True # Use UUIDs to preserve translations when text moves
61+
gettext_location = True # Include locations as comments for reference
62+
gettext_additional_targets = ['literal-block', 'image'] # Also translate code blocks and image captions
63+
5664
# Build hub specific example scripts.
5765
sys.path.append(os.path.abspath("../../examples/pup/hub_common"))
5866
import make_examples # noqa F401, E402

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ flake8 = "^4.0"
3636
[tool.poetry.group.doc.dependencies]
3737
Sphinx = { git = "https://github.com/pybricks/sphinx.git", rev = "cd277d09" }
3838
sphinx-rtd-theme = "^1.0.0"
39+
sphinx-intl = "^2.1.0"
3940
toml = "^0.10.0"
4041

4142
[build-system]

0 commit comments

Comments
 (0)