Skip to content

Commit f0c133d

Browse files
committed
website: language switch goes to the same page, not the preface
The header language menu always linked to the other language's preface, so switching language from chapter 7 or an About page dumped you at the preface. Map to the equivalent page instead: swap the zh-cn<->en-us segment for book chapters, /about/<->/about/en/ for About pages, and fall back to the other language's landing for anything else. Drop target=_blank so the switch happens in place, and record the choice in localStorage so the root auto-route honors it.
1 parent 79f8a66 commit f0c133d

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

website/themes/moderncpp/layout/partials/main_menu.ejs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,27 @@
2222
</ul>
2323
</li>
2424

25+
<%
26+
// Switch to the SAME page in English where one exists (book chapter, about
27+
// page), otherwise fall back to the English landing.
28+
var _p = page.path || '';
29+
var enHref;
30+
if (_p.indexOf('zh-cn/') !== -1) {
31+
enHref = url_for('/' + _p.replace('zh-cn/', 'en-us/').replace(/index\.html$/, ''));
32+
} else if (_p.indexOf('/about/') !== -1) {
33+
enHref = url_for('/' + _p.replace('/about/', '/about/en/'));
34+
} else {
35+
enHref = url_for('/modern-cpp/en/');
36+
}
37+
%>
2538
<li class="nav-dropdown-container language">
2639
<a class="nav-link">
2740
<span style="content: url(/modern-cpp/assets/lang/cn.svg); width: 15px; height: 15px; margin-right: 5px; vertical-align: middle; margin-bottom: 2px;"></span>
2841
中文
2942
</a><span class="arrow"></span>
3043
<ul class="nav-dropdown">
3144
<li><ul>
32-
<li><a class="nav-link" target="_blank" href="/modern-cpp/en-us/00-preface/">
45+
<li><a class="nav-link" href="<%- enHref %>" onclick="try{localStorage.setItem('mcpp-lang','en')}catch(e){}">
3346
<span style="content: url(/modern-cpp/assets/lang/en.svg); width: 15px; height: 15px; margin-right: 5px; vertical-align: middle; margin-bottom: 2px;"></span>
3447
English
3548
</a></li>

website/themes/moderncpp/layout/partials/main_menu_en.ejs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,27 @@
2222
</ul>
2323
</li>
2424

25+
<%
26+
// Switch to the SAME page in Chinese where one exists (book chapter, about
27+
// page), otherwise fall back to the Chinese landing.
28+
var _p = page.path || '';
29+
var zhHref;
30+
if (_p.indexOf('en-us/') !== -1) {
31+
zhHref = url_for('/' + _p.replace('en-us/', 'zh-cn/').replace(/index\.html$/, ''));
32+
} else if (_p.indexOf('/about/en/') !== -1) {
33+
zhHref = url_for('/' + _p.replace('/about/en/', '/about/'));
34+
} else {
35+
zhHref = url_for('/modern-cpp/');
36+
}
37+
%>
2538
<li class="nav-dropdown-container language">
2639
<a class="nav-link">
2740
<span style="content: url(/modern-cpp/assets/lang/en.svg); width: 15px; height: 15px; margin-right: 5px; vertical-align: middle; margin-bottom: 2px;"></span>
2841
English
2942
</a><span class="arrow"></span>
3043
<ul class="nav-dropdown">
3144
<li><ul>
32-
<li><a class="nav-link" target="_blank" href="/modern-cpp/zh-cn/00-preface/">
45+
<li><a class="nav-link" href="<%- zhHref %>" onclick="try{localStorage.setItem('mcpp-lang','zh')}catch(e){}">
3346
<span style="content: url(/modern-cpp/assets/lang/cn.svg); width: 15px; height: 15px; margin-right: 5px; vertical-align: middle; margin-bottom: 2px;"></span>
3447
中文
3548
</a></li>

0 commit comments

Comments
 (0)