Skip to content

Commit 845f01f

Browse files
committed
website: auto-route root by browser language; harden landing flag
Make the site behave like a real bilingual site at the entry point. A non-Chinese browser (or anyone whose remembered choice is English) hitting the root now lands on the English page instead of a wall of Chinese; an explicit 'zh' choice is honored and never bounced. The redirect runs as the first thing in <head>, before the non-async vue script, so it is not blocked on a download. Landing switchers write the choice to localStorage so the auto-route respects it. Also inline the landing flag's size so it can never depend on a cached stylesheet rule (the cause of the giant-flag render after the button->anchor change shipped while old CSS was still cached).
1 parent dab1a01 commit 845f01f

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

website/themes/moderncpp/layout/index.ejs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
<div id="lang-switch">
1616
<% if (isEn) { %>
17-
<a class="lang-btn" href="<%- url_for("/modern-cpp/") %>">
18-
<span class="flag" style="content: url(/modern-cpp/assets/lang/cn.svg);"></span>中文
17+
<a class="lang-btn" href="<%- url_for("/modern-cpp/") %>" onclick="try{localStorage.setItem('mcpp-lang','zh')}catch(e){}">
18+
<span class="flag" style="content: url(/modern-cpp/assets/lang/cn.svg); display: inline-block; width: 15px; height: 15px; vertical-align: middle; margin-right: 6px; margin-bottom: 2px;"></span>中文
1919
</a>
2020
<% } else { %>
21-
<a class="lang-btn" href="<%- url_for("/modern-cpp/en/") %>">
22-
<span class="flag" style="content: url(/modern-cpp/assets/lang/en.svg);"></span>English
21+
<a class="lang-btn" href="<%- url_for("/modern-cpp/en/") %>" onclick="try{localStorage.setItem('mcpp-lang','en')}catch(e){}">
22+
<span class="flag" style="content: url(/modern-cpp/assets/lang/en.svg); display: inline-block; width: 15px; height: 15px; vertical-align: middle; margin-right: 6px; margin-bottom: 2px;"></span>English
2323
</a>
2424
<% } %>
2525
</div>

website/themes/moderncpp/layout/layout.ejs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1-
<% var isIndex = page.layout === 'index' || page.path === 'index.html' %>
2-
1+
<%
2+
var isEn = page.type == 'book-en-us' || page.type == 'about-en';
3+
var isIndex = page.layout === 'index' || page.path === 'index.html';
4+
%>
35
<!DOCTYPE html>
4-
<html lang="<%- page.type == 'book-en-us' ? 'en' : 'zh-CN' %>">
6+
<html lang="<%- isEn ? 'en' : 'zh-CN' %>">
57
<head>
6-
<title><%- page.title ? page.title + ' - ' : '' %><%- page.type == 'book-en-us' ? 'Modern C++ Tutorial: C++11 to C++26 On the Fly' : '现代 C++ 教程: 高速上手 C++11 到 C++26' %></title>
78
<meta charset="utf-8">
9+
<% if (isIndex && !isEn) { %>
10+
<!-- Browser-language auto-routing: send non-Chinese browsers (or anyone
11+
whose remembered choice is English) to the English landing. A stored
12+
'zh' preference is honored and never bounced. Runs before any external
13+
script so the redirect is not blocked on a network download. -->
14+
<script>
15+
(function () {
16+
var pref = null;
17+
try { pref = localStorage.getItem('mcpp-lang'); } catch (e) {}
18+
if (pref === 'zh') return;
19+
var enBrowser = (navigator.language || navigator.userLanguage || 'en')
20+
.toLowerCase().indexOf('zh') !== 0;
21+
if (pref === 'en' || enBrowser) { location.replace('/modern-cpp/en/'); }
22+
})();
23+
</script>
24+
<% } %>
25+
<title><%- page.title ? page.title + ' - ' : '' %><%- isEn ? 'Modern C++ Tutorial: C++11 to C++26 On the Fly' : '现代 C++ 教程: 高速上手 C++11 到 C++26' %></title>
826
<meta name="description" content="<%- theme.site_description %>">
927
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
1028
<link rel="shortcut icon" type="image/x-icon" href="/modern-cpp/assets/cover-2nd.png">
@@ -23,7 +41,7 @@
2341
<script async src="//changkun.de/urlstat/client.js"></script>
2442
</head>
2543
<body class="<%- isIndex ? '' : 'docs' -%>">
26-
<% if (page.type == 'book-en-us') { %>
44+
<% if (isEn) { %>
2745
<div id="mobile-bar" data-bg-text="Modern C++ Tutorial" <%- isIndex ? 'class="top"' : '' %>>
2846
<a class="menu-button"></a>
2947
<a class="logo" href="/modern-cpp/"></a>

0 commit comments

Comments
 (0)