1

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!

89design
  • 13
  • 2
  • Have you tried to store the language code in a cookie? Maybe in the `sessionStorage` – CcmU Jan 20 '22 at 10:36
  • [Write a cookie with the language code](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie) when you set it, and then read in the code from the cookie when you reload the page. – Andy Jan 20 '22 at 10:37
  • Does this answer your question? [Keep the selected color by clicking even after reload the page using Javascript](https://stackoverflow.com/questions/33499363/keep-the-selected-color-by-clicking-even-after-reload-the-page-using-javascript) – evolutionxbox Jan 20 '22 at 10:37

1 Answers1

2

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.