-
Notifications
You must be signed in to change notification settings - Fork 326
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
Language redirector #503
Language redirector #503
Changes from 6 commits
8aaca25
84d0f77
222b4e8
1fcc422
c6f8b59
4106fa7
49c47e9
b9fae2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
layout: default | ||
lang: cat | ||
lang: ca | ||
--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
layout: default | ||
lang: pt_br | ||
lang: pt-BR | ||
--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
layout: default | ||
lang: zh-cn | ||
lang: zh-CN | ||
--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
layout: default | ||
lang: zh-tw | ||
lang: zh-TW | ||
--- |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,36 @@ | ||||||||||||||||||||||||||||||||
// Wrap all this to not pollute global namespace | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
// Generate an array of languages from page | ||||||||||||||||||||||||||||||||
const JDM_LANGS = [...document.querySelectorAll('#language-dropdown > ul > li')].map(el => el.classList[0]); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
function redirectTo(lang) { | ||||||||||||||||||||||||||||||||
const link = document.querySelector(`#language-dropdown > ul > li.${lang} > a`); | ||||||||||||||||||||||||||||||||
if (link) | ||||||||||||||||||||||||||||||||
link.click(); | ||||||||||||||||||||||||||||||||
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since our language URL is very simple, I'd rather set
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not as simple. At least, we have
Suggested change
|
||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
function guessedTranslation() { | ||||||||||||||||||||||||||||||||
const langs = navigator.languages; | ||||||||||||||||||||||||||||||||
for (let i=0; i < langs.length; i++) { | ||||||||||||||||||||||||||||||||
let lang = langs[i]; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if (JDM_LANGS.includes(lang)) | ||||||||||||||||||||||||||||||||
return lang; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// Test 'ru' in addition to 'ru-RU' | ||||||||||||||||||||||||||||||||
lang = lang.slice(0,2); | ||||||||||||||||||||||||||||||||
if (JDM_LANGS.includes(lang)) | ||||||||||||||||||||||||||||||||
return lang; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
tupaschoal marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// Do redirection only one time | ||||||||||||||||||||||||||||||||
if (!localStorage.getItem('langRedirectionHasDone')) { | ||||||||||||||||||||||||||||||||
localStorage.setItem('langRedirectionHasDone', true); | ||||||||||||||||||||||||||||||||
redirectTo(guessedTranslation()); | ||||||||||||||||||||||||||||||||
Comment on lines
+46
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I guess I'd swap the order here, it seems more legit hahah |
||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define this as a global constant via
<script>
block in Jekyll instead of parsing languages out of a query selector of language dropdown links.