The requirement, I understand boils down to this:
If an end-user had selected a language, the selection should always be transparent in the URL in order to enable proper functioning of the translation module. Meeting this requirement should not cause an unnecessary page reload. However, the scope of this question ignores the fact that on a cold-start where the clang=xx
does not exist, a manual redirect would be necessary to load the correct content since previous user selection is only known to the client.
You're almost there, based on the snippet you posted. Function changeLanguage
correctly loads the content in the selected language. If I were doing a code review on it though, I would request a few changes to the following outcome:
Patched HTML
<a href="#!" id="{{icon}}" onclick="return loadContentInSelectedLanguage(this.id)"></a>
Patched JS:
function processLanguageSelection(selectedLang) {
highlightCurrentlySelectedLang(selectedLang);
saveSelectedLangAcrossSession("clang", selectedLang);
reloadContentInSelectedLang(selectedLang);
}
function highlightCurrentlySelectedLanguage(selectedLang) {
$('#languages img').attr('src', '/images/flag-icons/' + selectedLang + '.png');
}
function saveSelectedLangAcrossSession(entryKey, selectedLang) {
window.localStorage.setItem(entryKey, selectedLang);
}
function reloadContentInSelectedLang(selectedLang) {
var search = window.location.search || "?";
if (/clang=\w{2}/.test(search)) {
search = search.replace(/clang=\w{2}/, "clang=" + selectedLang);
} else {
search += ("?" === search ? "clang=" : "&clang=") + selectedLang;
}
window.history.replaceState(null, "", search);
window.location.reload();
}
You're almost there, like I said. So, what's left to get the ultimately? -- The missing chip is this: previous user selection should be applied just when the page starts to load. And any necessary redirection should be done as early as possible before the browser starts to process the document body. Naturally, it means the part of your scripts with this responsibility should be the first thing that is run inside the <head>...</head>
section.
<head>
<script>
(function() {
var previouslySelectedLanguage = window.localStorage.getItem("clang");
if (/clang=\w{2}/.test(window.location.search)) {
// correct content should have been loaded in this case
// otherwise the server needs fixing, but that's not
// in the scope of this question.
// determine selectedLang from window.location.search, then..
highlightCurrentlySelectedLanguage(selectedLang);
if (!previouslySelectedLanguage || "selectedLang" !== previouslySelectedLanguage) {
saveSelectedLangAcrossSession("clang", selectedLang);
}
return;
}
if (previouslySelectedLanguage) {
// redirect
reloadContentInSelectedLang(previouslySelectedLanguage);
}
})();
</script>
</head>
Kindly note that the examples offered in this answer are not production ready. Do exercise due diligence by ensuring they're robust, resilient and meet other standards required in your organisation. For example, this regex, /clang=\w{2}/
makes an audacious assumption that can't always be true.