I was wondering how to save the html language on a website after reloading the page. I created a couple of language buttons, here's one example:
<a onclick="changeLang('pt')" href="#" title="Portugese" data-lang="Portugese">
<div class="flag pt">Portugese</div>
</a>
And a bit of code in my JS file:
const changeLang = (languageCode) => {
document.documentElement.setAttribute("lang", languageCode);
};
How can I make sure the language stays Portugese after clicking the button?
Thank you for your help in advance!
There are few option, in my opinion best approach is to have different url for different language (domain.com, domain.com/pt, domain.com/fr) which is good for SEO. You can also save language in cookie with javascript and then on page load fetch current language from cookie, check here how to set and fetch cookie.