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!

3

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.