Skip to content

Commit 30efbba

Browse files
committed
Fix language switcher to handle previews
1 parent 7666eb3 commit 30efbba

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ doc/source/savefig/
145145

146146
# Web & Translations #
147147
##############################
148+
web/preview/
148149
web/translations/
149150
web/pandas/es/
150151
web/pandas/pt/

web/pandas/static/js/language_switcher.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
window.addEventListener("DOMContentLoaded", function() {
2+
var absBaseUrl = document.baseURI;
23
var baseUrl = location.protocol + "//" + location.hostname
34
if (location.port) {
45
baseUrl = baseUrl + ":" + location.port
@@ -12,6 +13,15 @@ window.addEventListener("DOMContentLoaded", function() {
1213
'pt': 'Português'
1314
}
1415

16+
// Handle preview URLs on github
17+
// If preview URL changes, this regex will need to be updated
18+
const re = /preview\/pandas-dev\/pandas\/(?<pr>[0-9]*)\//g;
19+
var previewUrl = '';
20+
for (const match of absBaseUrl.matchAll(re)) {
21+
previewUrl = `/preview/pandas-dev/pandas/${match.groups.pr}`;
22+
}
23+
var pathName = location.pathname.replace(previewUrl, '')
24+
1525
// Create dropdown menu
1626
function makeDropdown(options) {
1727
var dropdown = document.createElement("li");
@@ -41,8 +51,8 @@ window.addEventListener("DOMContentLoaded", function() {
4151
if (i !== 'en') {
4252
urlLanguage = '/' + i;
4353
}
44-
var pathName = location.pathname.replace('/' + currentLanguage + '/', '/')
45-
var newUrl = baseUrl + urlLanguage + pathName
54+
pathName = pathName.replace('/' + currentLanguage + '/', '/')
55+
var newUrl = baseUrl + previewUrl + urlLanguage + pathName
4656
window.location.href = newUrl;
4757
});
4858
dropdownMenu.appendChild(dropdownItem);

web/pandas_web.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ def navbar_add_info(context):
8787
``has_subitems`` that tells which one of them every element is. It
8888
also adds a ``slug`` field to be used as a CSS id.
8989
"""
90+
lang = context["selected_language"]
9091
ignore = context["translations"]["ignore"]
92+
default_language = context["translations"]["default_language"]
9193
for i, item in enumerate(context["navbar"]):
9294
if item["target"] in ignore:
93-
item["target"] = "/" + item["target"]
95+
if lang != default_language:
96+
item["target"] = "../" + item["target"]
9497

9598
context["navbar"][i] = dict(
9699
item,

0 commit comments

Comments
 (0)