-
-
Notifications
You must be signed in to change notification settings - Fork 728
✨ Add opt-in client-side language redirect #2935
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
Open
aplgr
wants to merge
3
commits into
nunocoracao:main
Choose a base branch
from
aplgr:feature/client-language-redirect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| (function () { | ||
| var config = window.BlowfishLanguageRedirectConfig; | ||
|
|
||
| if (!config || config.enabled !== true) { | ||
| return; | ||
| } | ||
|
|
||
| function getStorage() { | ||
| if (!config.storageKey) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| return window.localStorage; | ||
| } catch (error) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| var storage = getStorage(); | ||
|
|
||
| function normalizeLanguageTag(value) { | ||
| if (typeof value !== "string") { | ||
| return ""; | ||
| } | ||
|
|
||
| return value.trim().toLowerCase(); | ||
| } | ||
|
|
||
| function normalizeLanguage(value) { | ||
| return normalizeLanguageTag(value).split("-")[0]; | ||
| } | ||
|
|
||
| function getTranslationLanguage(translation) { | ||
| if (!translation) { | ||
| return ""; | ||
| } | ||
|
|
||
| return normalizeLanguageTag(translation.lang || translation.languageCode); | ||
| } | ||
|
|
||
| function getAvailableTranslations() { | ||
| if (!Array.isArray(config.translations)) { | ||
| return []; | ||
| } | ||
|
|
||
| return config.translations.filter(function (translation) { | ||
| return translation && translation.url && normalizeLanguage(translation.lang || translation.languageCode); | ||
| }); | ||
| } | ||
|
|
||
| var translations = getAvailableTranslations(); | ||
|
|
||
| function findTranslation(language) { | ||
| var languageTag = normalizeLanguageTag(language); | ||
| var baseLanguage = normalizeLanguage(language); | ||
|
|
||
| if (!baseLanguage) { | ||
| return null; | ||
| } | ||
|
|
||
| for (var i = 0; i < translations.length; i += 1) { | ||
| var translation = translations[i]; | ||
|
|
||
| if ( | ||
| normalizeLanguageTag(translation.lang) === languageTag || | ||
| normalizeLanguageTag(translation.languageCode) === languageTag | ||
| ) { | ||
| return translation; | ||
| } | ||
| } | ||
|
|
||
| for (var j = 0; j < translations.length; j += 1) { | ||
| var baseTranslation = translations[j]; | ||
|
|
||
| if ( | ||
| normalizeLanguage(baseTranslation.lang) === baseLanguage || | ||
| normalizeLanguage(baseTranslation.languageCode) === baseLanguage | ||
| ) { | ||
| return baseTranslation; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function getStoredLanguage() { | ||
| if (!storage || config.storedLanguageRedirect !== true) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| return storage.getItem(config.storageKey); | ||
| } catch (error) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function setStoredLanguage(language) { | ||
| if (!storage) { | ||
| return; | ||
| } | ||
|
|
||
| var languageTag = normalizeLanguageTag(language); | ||
|
|
||
| if (!languageTag) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| storage.setItem(config.storageKey, languageTag); | ||
| } catch (error) { | ||
| // Ignore storage errors so the language link keeps its normal behavior. | ||
| } | ||
| } | ||
|
|
||
| function getBrowserLanguage() { | ||
| var languages = []; | ||
|
|
||
| if (Array.isArray(navigator.languages)) { | ||
| languages = languages.concat(navigator.languages); | ||
| } | ||
|
|
||
| if (navigator.language) { | ||
| languages.push(navigator.language); | ||
| } | ||
|
|
||
| for (var i = 0; i < languages.length; i += 1) { | ||
| var baseLanguage = normalizeLanguage(languages[i]); | ||
|
|
||
| if (findTranslation(baseLanguage)) { | ||
| return baseLanguage; | ||
| } | ||
| } | ||
|
|
||
| if (config.fallbackLanguage && findTranslation(config.fallbackLanguage)) { | ||
| return config.fallbackLanguage; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function normalizePath(pathname) { | ||
| if (pathname.length > 1 && pathname.charAt(pathname.length - 1) === "/") { | ||
| return pathname.slice(0, -1); | ||
| } | ||
|
|
||
| return pathname; | ||
| } | ||
|
|
||
| function getUrl(value) { | ||
| try { | ||
| return new URL(value, window.location.origin); | ||
| } catch (error) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function redirectToLanguage(language) { | ||
| var translation = findTranslation(language); | ||
|
|
||
| if (!translation || !translation.url) { | ||
| return false; | ||
| } | ||
|
|
||
| var currentLanguage = normalizeLanguageTag(config.currentLanguage || config.currentLanguageCode); | ||
| var targetLanguage = getTranslationLanguage(translation); | ||
|
|
||
| if (currentLanguage && targetLanguage && currentLanguage === targetLanguage) { | ||
| return false; | ||
| } | ||
|
|
||
| var targetUrl = getUrl(translation.url); | ||
|
|
||
| if (!targetUrl || normalizePath(targetUrl.pathname) === normalizePath(window.location.pathname)) { | ||
| return false; | ||
| } | ||
|
|
||
| window.location.replace(targetUrl.href); | ||
| return true; | ||
| } | ||
|
|
||
| function findTranslationByUrl(url) { | ||
| var linkUrl = getUrl(url); | ||
|
|
||
| if (!linkUrl) { | ||
| return null; | ||
| } | ||
|
|
||
| for (var i = 0; i < translations.length; i += 1) { | ||
| var translationUrl = getUrl(translations[i].url); | ||
|
|
||
| if (translationUrl && normalizePath(translationUrl.pathname) === normalizePath(linkUrl.pathname)) { | ||
| return translations[i]; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function bindLanguageSwitcher() { | ||
| document.addEventListener("click", function (event) { | ||
| if (!event.target || !event.target.closest) { | ||
| return; | ||
| } | ||
|
|
||
| var link = event.target.closest(".translation a[href]"); | ||
|
|
||
| if (!link) { | ||
| return; | ||
| } | ||
|
|
||
| var translation = findTranslationByUrl(link.href); | ||
|
|
||
| if (translation) { | ||
| setStoredLanguage(getTranslationLanguage(translation)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function maybeRedirect() { | ||
| var storedLanguage = getStoredLanguage(); | ||
|
|
||
| if (storedLanguage) { | ||
| redirectToLanguage(storedLanguage); | ||
| return; | ||
| } | ||
|
|
||
| if (config.browserRedirectHomeOnly === true && config.isHome !== true) { | ||
| return; | ||
| } | ||
|
|
||
| var browserLanguage = getBrowserLanguage(); | ||
|
|
||
| if (browserLanguage) { | ||
| redirectToLanguage(browserLanguage); | ||
| } | ||
| } | ||
|
|
||
| bindLanguageSwitcher(); | ||
| maybeRedirect(); | ||
| })(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| {{ $params := site.Params.languageRedirect | default dict }} | ||
| {{ if and ($params.enabled | default false) hugo.IsMultilingual }} | ||
| {{ $translations := slice }} | ||
| {{ range .AllTranslations }} | ||
| {{ $translations = $translations | append (dict "lang" .Language.Lang "languageCode" (.Language.LanguageCode | default "") "url" .RelPermalink) }} | ||
| {{ end }} | ||
|
|
||
| {{ $browserRedirectHomeOnly := true }} | ||
| {{ if isset $params "browserredirecthomeonly" }} | ||
| {{ $browserRedirectHomeOnly = $params.browserRedirectHomeOnly }} | ||
| {{ end }} | ||
|
|
||
| {{ $storedLanguageRedirect := true }} | ||
| {{ if isset $params "storedlanguageredirect" }} | ||
| {{ $storedLanguageRedirect = $params.storedLanguageRedirect }} | ||
| {{ end }} | ||
|
|
||
| {{ $fallbackLanguage := hugo.Sites.Default.Language.Lang }} | ||
| {{ with $params.fallbackLanguage }} | ||
| {{ $fallbackLanguage = . }} | ||
| {{ end }} | ||
|
|
||
| {{ $config := dict "enabled" true "storageKey" ($params.storageKey | default "blowfish_preferred_language") "fallbackLanguage" $fallbackLanguage "browserRedirectHomeOnly" $browserRedirectHomeOnly "storedLanguageRedirect" $storedLanguageRedirect "currentLanguage" .Language.Lang "currentLanguageCode" (.Language.LanguageCode | default "") "isHome" .IsHome "translations" $translations }} | ||
| <script> | ||
| window.BlowfishLanguageRedirectConfig = {{ $config | jsonify | safeJS }}; | ||
| </script> | ||
| {{ $alg := site.Params.fingerprintAlgorithm | default "sha512" }} | ||
| {{ with resources.Get "js/language-redirect.js" }} | ||
| {{ $jsLanguageRedirect := . | resources.Minify | resources.Fingerprint $alg }} | ||
| <script | ||
| type="text/javascript" | ||
| src="{{ $jsLanguageRedirect.RelPermalink }}" | ||
| integrity="{{ $jsLanguageRedirect.Data.Integrity }}"></script> | ||
| {{ end }} | ||
| {{ end }} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.