Skip to content

Commit 76d9867

Browse files
author
PeterMaZep
committed
commmit war except not really
Its kinda a world standard to have /en-us/ in front for uhh idk purposes
1 parent 385a91f commit 76d9867

4 files changed

Lines changed: 99 additions & 4 deletions

File tree

‎_config.yml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ markdown: kramdown
55
kramdown:
66
input: GFM
77
languages: ["en-us", "zh-cn"]
8-
default_lang: "en-us"
98
lang_from_path: true
109
parallel_localization: false
1110
exclude_from_localization:

‎_includes/footer.html‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ <h3 class="footer-heading">
5454
<nav class="language-switcher" aria-label="{{ site.data.translations[site.active_lang].site.footer.language }}">
5555
{% for lang in site.languages %}
5656
{% assign language_path = page.permalink_lang[lang] | default: page.permalink | default: page.url %}
57-
{% capture language_url %}{{ site.baseurl }}{% unless lang == site.default_lang %}/{{ lang }}{% endunless %}{{ language_path }}{% endcapture %}
57+
{% capture language_url %}/{{ lang }}{{ language_path }}{% endcapture %}
5858
<a
5959
class="language-link no-icon{% if lang == site.active_lang %} is-active{% endif %}"
60-
{% static_href %}href="{{ language_url }}"{% endstatic_href %}
60+
href="{{ language_url }}"
61+
data-lang="{{ lang }}"
6162
lang="{{ lang }}"
6263
hreflang="{{ lang }}"
6364
{% if lang == site.active_lang %}aria-current="page"{% endif %}

‎_layouts/default.html‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{% assign site_tagline = site.data.translations[site.active_lang].site.tagline %}
1212
{% assign page_title = site.data.translations[site.active_lang].site.page_titles[page.id] | default: page.title | default: site.title %}
1313
{% assign localized_page_path = page.url %}
14-
{% if site.active_lang != site.default_lang %}
14+
{% if site.active_lang %}
1515
{% capture localized_page_path %}/{{ site.active_lang }}{{ page.url }}{% endcapture %}
1616
{% endif %}
1717

@@ -139,4 +139,85 @@
139139
{% include footer.html %}
140140
<script src="{{ '/assets/js/main.js?v=' | append: asset_revision | relative_url }}"></script>
141141
</body>
142+
<script>
143+
(function() {
144+
var supportedLangs = ['en-us', 'zh-cn'];
145+
var defaultLang = 'en-us';
146+
var cookieName = 'preferred_lang';
147+
148+
function getCookie(name) {
149+
var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
150+
return match ? decodeURIComponent(match[2]) : null;
151+
}
152+
153+
function setCookie(name, value) {
154+
var date = new Date();
155+
date.setTime(date.getTime() + 10 * 365 * 24 * 60 * 60 * 1000);
156+
document.cookie = name + '=' + encodeURIComponent(value) +
157+
'; expires=' + date.toUTCString() +
158+
'; path=/';
159+
}
160+
161+
function getLanguageFromPath(path) {
162+
for (var i = 0; i < supportedLangs.length; i++) {
163+
var prefix = '/' + supportedLangs[i];
164+
if (path.indexOf(prefix) === 0) {
165+
return supportedLangs[i];
166+
}
167+
}
168+
return null;
169+
}
170+
171+
function removeLanguageFromPath(path) {
172+
var lang = getLanguageFromPath(path);
173+
if (lang) {
174+
return path.replace('/' + lang, '');
175+
}
176+
return path;
177+
}
178+
179+
(function redirect() {
180+
var path = window.location.pathname;
181+
var lang = getCookie(cookieName);
182+
183+
if (!lang) {
184+
var browserLang = (navigator.language || navigator.userLanguage || '').toLowerCase();
185+
for (var i = 0; i < supportedLangs.length; i++) {
186+
if (browserLang === supportedLangs[i] || browserLang.startsWith(supportedLangs[i].split('-')[0])) {
187+
lang = supportedLangs[i];
188+
break;
189+
}
190+
}
191+
}
192+
193+
if (!lang || supportedLangs.indexOf(lang) === -1) {
194+
lang = defaultLang;
195+
}
196+
197+
if (!getCookie(cookieName)) {
198+
setCookie(cookieName, lang);
199+
}
200+
201+
var hasLangPrefix = getLanguageFromPath(path) !== null;
202+
203+
if (!hasLangPrefix) {
204+
var newPath = (path === '/' || path === '')
205+
? '/' + lang + '/'
206+
: '/' + lang + path;
207+
window.location.replace(newPath);
208+
}
209+
})();
210+
211+
document.addEventListener('DOMContentLoaded', function() {
212+
var links = document.querySelectorAll('.language-link[data-lang]');
213+
links.forEach(function(link) {
214+
link.addEventListener('click', function(e) {
215+
// Allow the link to navigate naturally (no e.preventDefault)
216+
var lang = this.getAttribute('data-lang');
217+
setCookie(cookieName, lang);
218+
});
219+
});
220+
});
221+
})();
222+
</script>
142223
</html>

‎pages/404.html‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,18 @@ <h1>404</h1>
1616
<a class="btn" href="{{ '/articles' | relative_url }}">{{ site.data.translations[site.active_lang].not-found.button-news }}</a>
1717
</div>
1818
</div>
19+
<script>
20+
var path = window.location.pathname;
21+
22+
var lang = 'en-us';
23+
var match = path.match(/^\/([a-z]{2}-[a-z]{2})\//);
24+
if (match) {
25+
lang = match[1];
26+
}
27+
28+
var target = '/' + lang + '/404';
29+
if (path !== target) {
30+
window.location.replace(target);
31+
}
32+
</script>
1933
</section>

0 commit comments

Comments
 (0)