Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-31045: Language switch #2652

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

Merged
merged 8 commits into from
Aug 7, 2017
Prev Previous commit
Next Next commit
Doc switchers: Desambiguate the need of a replace(/\/+$/g, '') by pro…
…per naming.
  • Loading branch information
JulienPalard committed Jul 11, 2017
commit 6f450c7656fd463fcf3340baf03a3385fd64c2ae
22 changes: 12 additions & 10 deletions 22 Doc/tools/static/switchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
function on_version_switch() {
var selected_version = $(this).children('option:selected').attr('value') + '/';
var url = window.location.href;
var current_language = find_language_in_url(url);
var current_version = find_version_in_url(url);
var current_language = language_segment_from_url(url);
var current_version = version_segment_in_url(url);
var new_url = url.replace('.org/' + current_language + current_version,
'.org/' + current_language + selected_version);
if (new_url != url) {
Expand All @@ -82,8 +82,8 @@
function on_language_switch() {
var selected_language = $(this).children('option:selected').attr('value') + '/';
var url = window.location.href;
Copy link
Member

Choose a reason for hiding this comment

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

most of the code is copied from on_version_switch(), can't you try to factorize the code a little bit more?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I could factorize it, I actually though about it a few seconds, but I don't see how it can make code any better, in the "more readable" sense. Yes it can probably be made shorter (not even sure). But I like the readability of:

var new_url = url.replace('.org/' + current_language + current_version,
                          '.org/' + selected_language + current_version);

and

var new_url = url.replace('.org/' + current_language + current_version,
                          '.org/' + current_language + selected_version);

and factorizing it will put a new level of "indirection", making it ultimately harder to read.

var current_language = find_language_in_url(url);
var current_version = find_version_in_url(url);
var current_language = language_segment_from_url(url);
var current_version = version_segment_in_url(url);
if (selected_language == 'en/') // Special 'default' case for english.
selected_language = '';
var new_url = url.replace('.org/' + current_language + current_version,
Expand All @@ -94,17 +94,19 @@
}
}

// Returns the path segment as a string, like 'fr/' or '' if not found.
function find_language_in_url(url) {
// Returns the path segment of the language as a string, like 'fr/'
// or '' if not found.
function language_segment_from_url(url) {
var language_regexp = '\.org/(' + Object.keys(all_languages).join('|') + '/)';
var match = url.match(language_regexp);
if (match !== null)
return match[1];
return '';
}

// Returns the path segment as a string, like '3.6/' or '' if not found.
function find_version_in_url(url) {
// Returns the path segment of the version as a string, like '3.6/'
// or '' if not found.
function version_segment_in_url(url) {
var language_segment = '(?:(?:' + Object.keys(all_languages).join('|') + ')/)';
var version_segment = '(?:(?:' + version_regexs.join('|') + ')/)';
var version_regexp = '\\.org/' + language_segment + '?(' + version_segment + ')';
Expand All @@ -116,8 +118,8 @@

$(document).ready(function() {
var release = DOCUMENTATION_OPTIONS.VERSION;
var current_language = find_language_in_url(window.location.href).replace(
/\/+$/g, '') || 'en';
var language_segment = language_segment_from_url(window.location.href);
var current_language = language_segment.replace(/\/+$/g, '') || 'en';
var version = release.substr(0, 3);
var version_select = build_version_select(version, release);

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.