Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include languages' translated names in the switcher #245

Merged
merged 5 commits into from
Feb 19, 2025
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
11 changes: 10 additions & 1 deletion build_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def __gt__(self, other):
class Language:
iso639_tag: str
name: str
translated_name: str
in_prod: bool
sphinxopts: tuple
html_only: bool = False
Expand All @@ -204,6 +205,12 @@ class Language:
def tag(self):
return self.iso639_tag.replace("_", "-").lower()

@property
def switcher_label(self):
if self.translated_name:
return f"{self.name} | {self.translated_name}"
return self.name

@staticmethod
def filter(languages, language_tags=None):
"""Filter a sequence of languages according to --languages."""
Expand Down Expand Up @@ -388,7 +395,7 @@ def setup_switchers(
- Cross-link various languages in a language switcher
- Cross-link various versions in a version switcher
"""
language_pairs = sorted((l.tag, l.name) for l in languages if l.in_prod)
language_pairs = sorted((l.tag, l.switcher_label) for l in languages if l.in_prod)
Copy link
Contributor

@m-aciek m-aciek Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest sorting by name/switcher label instead of prioritizing the tag. This way it will be easier to find by English name lexicographically when the list will grow longer.

It will be predictable and natural where to look for a language knowing its English name.

version_pairs = [(v.name, v.picker_label) for v in reversed(versions)]

switchers_template_file = HERE / "templates" / "switchers.js"
Expand Down Expand Up @@ -1151,13 +1158,15 @@ def parse_languages_from_config() -> list[Language]:
"""Read config.toml to discover languages to build."""
config = tomlkit.parse((HERE / "config.toml").read_text(encoding="UTF-8"))
defaults = config["defaults"]
default_translated_name = defaults.get("translated_name", "")
default_in_prod = defaults.get("in_prod", True)
default_sphinxopts = defaults.get("sphinxopts", [])
default_html_only = defaults.get("html_only", False)
return [
Language(
iso639_tag=iso639_tag,
name=section["name"],
translated_name=section.get("translated_name", default_translated_name),
in_prod=section.get("in_prod", default_in_prod),
sphinxopts=section.get("sphinxopts", default_sphinxopts),
html_only=section.get("html_only", default_html_only),
Expand Down
19 changes: 19 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# name: the English name for the language.
# translated_name: the 'local' name for the language.
# in_prod: If true, include in the language switcher.
# html_only: If true, only create HTML files.
# sphinxopts: Extra options to pass to SPHINXOPTS in the Makefile.

[defaults]
# name has no default, it is mandatory.
translated_name = ""
in_prod = true
html_only = false
sphinxopts = [
Expand All @@ -13,6 +20,7 @@ name = "English"

[languages.es]
name = "Spanish"
translated_name = "español"
sphinxopts = [
'-D latex_engine=xelatex',
'-D latex_elements.inputenc=',
Expand All @@ -21,6 +29,7 @@ sphinxopts = [

[languages.fr]
name = "French"
translated_name = "français"
sphinxopts = [
'-D latex_engine=xelatex',
'-D latex_elements.inputenc=',
Expand All @@ -29,13 +38,16 @@ sphinxopts = [

[languages.id]
name = "Indonesian"
translated_name = "Indonesia"
in_prod = false

[languages.it]
name = "Italian"
translated_name = "italiano"

[languages.ja]
name = "Japanese"
translated_name = "日本語"
sphinxopts = [
'-D latex_engine=lualatex',
'-D latex_elements.inputenc=',
Expand All @@ -59,6 +71,7 @@ sphinxopts = [

[languages.ko]
name = "Korean"
translated_name = "한국어"
sphinxopts = [
'-D latex_engine=xelatex',
'-D latex_elements.inputenc=',
Expand All @@ -68,20 +81,25 @@ sphinxopts = [

[languages.pl]
name = "Polish"
translated_name = "polski"

[languages.pt_BR]
name = "Brazilian Portuguese"
translated_name = "Português brasileiro"

[languages.tr]
name = "Turkish"
translated_name = "Türkçe"

[languages.uk]
name = "Ukrainian"
translated_name = "українська"
in_prod = false
html_only = true

[languages.zh_CN]
name = "Simplified Chinese"
translated_name = "简体中文"
sphinxopts = [
'-D latex_engine=xelatex',
'-D latex_elements.inputenc=',
Expand All @@ -90,6 +108,7 @@ sphinxopts = [

[languages.zh_TW]
name = "Traditional Chinese"
translated_name = "繁體中文"
sphinxopts = [
'-D latex_engine=xelatex',
'-D latex_elements.inputenc=',
Expand Down
Loading