Skip to content

Commit a4c0bc1

Browse files
committed
Added internationalization to the nox file
1 parent 45c143b commit a4c0bc1

File tree

1 file changed

+154
-7
lines changed

1 file changed

+154
-7
lines changed

noxfile.py

Lines changed: 154 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
## Sphinx related options
99

1010
# Sphinx output and source directories
11-
OUTPUT_DIR = pathlib.Path("_build", "html")
11+
BUILD_DIR = '_build'
12+
OUTPUT_DIR = pathlib.Path(BUILD_DIR, "html")
1213
SOURCE_DIR = pathlib.Path(".")
1314

15+
# Location of the translation templates
16+
TRANSLATION_TEMPLATE_DIR = pathlib.Path(BUILD_DIR, "gettext")
17+
1418
# Sphinx build commands
1519
SPHINX_BUILD = "sphinx-build"
1620
SPHINX_AUTO_BUILD = "sphinx-autobuild"
@@ -21,39 +25,109 @@
2125
# Sphinx parameters used to test the build of the guide
2226
TEST_PARAMETERS = ['-W', '--keep-going', '-E', '-a']
2327

28+
# Sphinx parameters to generate translation templates
29+
TRANSLATION_TEMPLATE_PARAMETERS = ["-b", "gettext"]
30+
2431
# Sphinx-autobuild ignore and include parameters
2532
AUTOBUILD_IGNORE = [
2633
"_build",
34+
".nox",
2735
"build_assets",
2836
"tmp",
2937
]
3038
AUTOBUILD_INCLUDE = [
3139
pathlib.Path("_static", "pyos.css")
3240
]
3341

42+
## Localization options (translations)
43+
44+
# List of languages for which locales will be generated in (/locales/<lang>)
45+
LANGUAGES = ["es"]
46+
47+
# List of languages that should be built when releasing the guide
48+
RELEASE_LANGUAGES = ['es']
49+
50+
3451
@nox.session
3552
def docs(session):
3653
"""Build the packaging guide."""
3754
session.install("-e", ".")
38-
cmd = [SPHINX_BUILD, *BUILD_PARAMETERS, SOURCE_DIR, OUTPUT_DIR, *session.posargs]
39-
session.run(*cmd)
55+
session.run(SPHINX_BUILD, *BUILD_PARAMETERS, SOURCE_DIR, OUTPUT_DIR, *session.posargs)
56+
# When building the guide, also build the translations in RELEASE_LANGUAGES
57+
session.notify("build-translations", ['release-build'])
58+
4059

4160
@nox.session(name="docs-test")
4261
def docs_test(session):
43-
"""Build the packaging guide with additional parameters."""
62+
"""
63+
Build the packaging guide with more restricted parameters.
64+
65+
Note: this is the session used in CI/CD to release the guide.
66+
"""
4467
session.install("-e", ".")
45-
cmd = [SPHINX_BUILD, *BUILD_PARAMETERS, *TEST_PARAMETERS, SOURCE_DIR, OUTPUT_DIR, *session.posargs]
46-
session.run(*cmd)
68+
session.run(SPHINX_BUILD, *BUILD_PARAMETERS, *TEST_PARAMETERS, SOURCE_DIR, OUTPUT_DIR, *session.posargs)
69+
# When building the guide with additional parameters, also build the translations in RELEASE_LANGUAGES
70+
# with those same parameters.
71+
session.notify("build-translations", ['release-build', *TEST_PARAMETERS])
72+
4773

4874
@nox.session(name="docs-live")
4975
def docs_live(session):
50-
"""Build and launch a local copy of the guide."""
76+
"""
77+
Build and launch a local copy of the guide.
78+
79+
This session will use sphinx-autobuild to build the guide and launch a local server so you can
80+
browse it locally. It will automatically rebuild the guide when changes are detected in the source.
81+
82+
It can be used with the language parameter to build a specific translation, for example:
83+
84+
nox -s docs-live -- -D language=es
85+
86+
Note: The docs-live-lang session below is provided as a convenience session for beginner contributors
87+
so they don't need to remember the specific sphinx-build parameters to build a different language.
88+
"""
5189
session.install("-e", ".")
5290
cmd = [SPHINX_AUTO_BUILD, *BUILD_PARAMETERS, SOURCE_DIR, OUTPUT_DIR, *session.posargs]
5391
for folder in AUTOBUILD_IGNORE:
5492
cmd.extend(["--ignore", f"*/{folder}/*"])
93+
# This part was commented in the previous version of the nox file, keeping the same here
94+
# for folder in AUTOBUILD_INCLUDE:
95+
# cmd.extend(["--watch", folder])
5596
session.run(*cmd)
5697

98+
99+
@nox.session(name="docs-live-lang")
100+
def docs_live_lang(session):
101+
"""
102+
A convenience session for beginner contributors to use the docs-live session with
103+
a specific language.
104+
105+
It expects the language code to be passed as the first positional argument, so it needs
106+
to be called with from the command line with the following syntax:
107+
108+
nox -s docs-live-lang -- LANG
109+
110+
where LANG is one of the available languages defined in LANGUAGES.
111+
For example, for Spanish: nox -s docs-live-lang -- es
112+
"""
113+
if not session.posargs:
114+
session.error(
115+
"Please provide a language using:\n\n "
116+
"nox -s docs-live-lang -- LANG\n\n "
117+
f" where LANG is one of: {LANGUAGES}"
118+
)
119+
lang = session.posargs[0]
120+
if lang in LANGUAGES:
121+
session.posargs.pop(0)
122+
session.notify("docs-live", ('-D', f"language={lang}", *session.posargs))
123+
else:
124+
session.error(
125+
f"[{lang}] locale is not available. Try using:\n\n "
126+
"nox -s docs-live-lang -- LANG\n\n "
127+
f"where LANG is one of: {LANGUAGES}"
128+
)
129+
130+
57131
@nox.session(name="docs-clean")
58132
def clean_dir(session):
59133
"""Clean out the docs directory used in the live build."""
@@ -65,3 +139,76 @@ def clean_dir(session):
65139
shutil.rmtree(content)
66140
else:
67141
os.remove(content)
142+
143+
144+
@nox.session(name="update-translations")
145+
def update_translations(session):
146+
"""
147+
Update the translation files (./locales/*/.po) for all languages translations.
148+
149+
Note: this step is important because it makes sure that the translation files are
150+
up to date with the latest changes in the guide.
151+
"""
152+
session.install("-e", ".")
153+
session.install("sphinx-intl")
154+
session.log("Updating templates (.pot)")
155+
session.run(SPHINX_BUILD, *TRANSLATION_TEMPLATE_PARAMETERS, SOURCE_DIR, TRANSLATION_TEMPLATE_DIR, *session.posargs)
156+
for lang in LANGUAGES:
157+
session.log(f"Updating .po files for [{lang}] translation")
158+
session.run("sphinx-intl", "update", "-p", TRANSLATION_TEMPLATE_DIR, "-l", lang)
159+
160+
161+
@nox.session(name="build-languages")
162+
def build_languages(session):
163+
"""
164+
Build the translations of the guide for the specified language.
165+
166+
Note: This sessions expects a list of languages to build in the first position of the session arguments.
167+
It does not need to be called directly, it is started by build_translations session.
168+
"""
169+
if not session.posargs:
170+
session.error("Please provide the list of languages to build the translation for")
171+
languages_to_build = session.posargs.pop(0)
172+
173+
session.install("-e", ".")
174+
for lang in languages_to_build:
175+
if lang not in LANGUAGES:
176+
session.warn(f"Language [{lang}] is not available for translation")
177+
continue
178+
session.log(f"Building [{lang}] guide")
179+
session.run(SPHINX_BUILD, *BUILD_PARAMETERS, "-D", f"language={lang}", ".", OUTPUT_DIR / lang, *session.posargs)
180+
181+
182+
@nox.session(name="build-translations")
183+
def build_translations(session):
184+
"""
185+
Build translations of the guide.
186+
187+
Note: this session can be called directly to build all available translations (defined in LANGUAGES).
188+
It is also called by the docs and docs-test sessions with 'release-build' as the first positional
189+
argument, to build only the translations defined in RELEASE_LANGUAGES.
190+
"""
191+
release_build = False
192+
if session.posargs and session.posargs[0] == 'release-build':
193+
session.posargs.pop(0)
194+
release_build = True
195+
# if running from the docs or docs-test sessions, build only release languages
196+
BUILD_LANGUAGES = RELEASE_LANGUAGES if release_build else LANGUAGES
197+
session.log(f"Existing translations: {LANGUAGES}")
198+
session.log(f"Release Languages: {RELEASE_LANGUAGES}")
199+
session.log(f"Building languages{' for release' if release_build else ''}: {BUILD_LANGUAGES}")
200+
if not BUILD_LANGUAGES:
201+
session.warn("No translations to build")
202+
else:
203+
session.notify("build-languages", [BUILD_LANGUAGES, *session.posargs])
204+
205+
206+
@nox.session(name="build-translations-test")
207+
def build_translations_test(session):
208+
"""
209+
Build all translations of the guide with testing parameters.
210+
211+
This is a convenience session to test the build of all translations with the testing parameters
212+
in the same way docs-test does for the English version.
213+
"""
214+
session.notify("build-translations", [*TEST_PARAMETERS])

0 commit comments

Comments
 (0)