Skip to content

Commit 689ab1b

Browse files
authored
Add localization metadata (#1681)
1 parent 753cba0 commit 689ab1b

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

source/_extensions/localization.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import Any, Dict
2+
3+
import docutils.nodes as nodes
4+
from sphinx.application import Sphinx
5+
6+
7+
def make_tag(language: str, link: str) -> str:
8+
return f'<link rel="alternate" hreflang="{language}" href="{link}" />'
9+
10+
11+
def html_page_context(
12+
app: Sphinx,
13+
pagename: str,
14+
templatename: str,
15+
context: Dict[str, Any],
16+
doctree: nodes.document,
17+
) -> None:
18+
if not doctree:
19+
return
20+
language = app.config.language or "en"
21+
localization_languages = app.config.localization_languages
22+
23+
pageurl: str = context["pageurl"]
24+
25+
tags = []
26+
for localization_language in localization_languages:
27+
local_pageurl = pageurl.replace(
28+
f"/{language}/", f"/{localization_language}/", 1
29+
)
30+
31+
tags.append(make_tag(localization_language.replace("_", "-"), local_pageurl))
32+
33+
if localization_language == "en":
34+
tags.append(make_tag("x-default", local_pageurl))
35+
36+
context["metatags"] += "\n" + "\n".join(tags)
37+
38+
39+
def setup(app: Sphinx) -> Dict[str, Any]:
40+
app.add_config_value("localization_languages", [], "html")
41+
42+
app.connect("html-page-context", html_page_context)
43+
44+
return {
45+
"parallel_read_safe": True,
46+
"parallel_write_safe": True,
47+
}

source/conf.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
local_extensions = [
5959
"_extensions.post_process",
60+
"_extensions.localization",
6061
]
6162

6263
extensions += local_extensions
@@ -76,6 +77,17 @@
7677
versionwarning_banner_title = "Warning!"
7778
versionwarning_body_selector = 'div[class="document"]'
7879

80+
# List of languages that frc-docs supports
81+
localization_languages = [
82+
"en",
83+
"es",
84+
"fr",
85+
"he",
86+
"pt",
87+
"tr",
88+
"zh_CN",
89+
]
90+
7991
# Redirect branch
8092
rediraffe_branch = "origin/main"
8193

@@ -89,7 +101,7 @@
89101
linkcheckdiff_branch = "origin/main"
90102

91103
# Configure OpenGraph support
92-
ogp_site_url = "https://docs.wpilib.org/en/latest/"
104+
ogp_site_url = "https://docs.wpilib.org/en/stable/"
93105
ogp_site_name = "FIRST Robotics Competition Documentation"
94106
ogp_image = (
95107
"https://raw.githubusercontent.com/wpilibsuite/branding/main/png/wpilib-128.png"
@@ -184,7 +196,7 @@
184196

185197
# Specify canonical root
186198
# This tells search engines that this domain is preferred
187-
html_baseurl = "https://docs.wpilib.org/"
199+
html_baseurl = "https://docs.wpilib.org/en/stable/"
188200

189201
html_theme_options = {
190202
"collapse_navigation": True,

0 commit comments

Comments
 (0)