Skip to content
Prev Previous commit
Doc switchers: Better fallback on unexisting translated version.
  • Loading branch information
JulienPalard committed Jul 31, 2017
commit da7a03450217406c212eedbfdb2697c9d9183b92
29 changes: 21 additions & 8 deletions Doc/tools/static/switchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@
return buf.join('');
}

function naviagate_if_exists(url, default_url) {
// check beforehand if url exists, else redirect to default_url.
function navigate_to_first_existing(urls) {
// Navigate to the first existing URL in urls.
var url = urls.shift();
if (urls.length == 0) {
window.location.href = url;
return;
}
$.ajax({
url: url,
success: function() {
window.location.href = url;
window.location.href = url;
},
error: function() {
window.location.href = default_url;
navigate_to_first_existing(urls);
}
});
}
Expand All @@ -73,8 +78,14 @@
var new_url = url.replace('.org/' + current_language + current_version,
'.org/' + current_language + selected_version);
if (new_url != url) {
naviagate_if_exists(new_url, 'https://docs.python.org/' +
current_language + selected_version);
navigate_to_first_existing([
new_url,
url.replace('.org/' + current_language + current_version,
'.org/' + selected_version),
'https://docs.python.org/' + current_language + selected_version,
'https://docs.python.org/' + selected_version,
'https://docs.python.org/'
]);
}
}

Expand All @@ -88,8 +99,10 @@
var new_url = url.replace('.org/' + current_language + current_version,
'.org/' + selected_language + current_version);
if (new_url != url) {
naviagate_if_exists(new_url, 'https://docs.python.org/' +
selected_language + current_version);
navigate_to_first_existing([
new_url,
'https://docs.python.org/'
]);
}
}

Expand Down