Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
72 views
in Technique[技术] by (71.8m points)

javascript - When is html languagechange event fired in the browser?

I am trying to add localization in a simple web app. There is a languagechange event as per Mozilla docs.

My questions is:

  • when is this event actually fired?

Here is what I tried.

  • page html tag has lang attribute <html lang='ar-SA'>
  • user can use some dropdown to switch to another language.
  • the dropdown, changes the lang attribute on html tag to the desired lang

I was hoping that changing lang attribute on root html tag will fire the languagechange event, which in turn I can use to load the content in new lang, but it does not.

Did I miss something obvious here?

question from:https://stackoverflow.com/questions/65867093/when-is-html-languagechange-event-fired-in-the-browser

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The languagechange event is fired at the global scope object when the user's preferred language changes.

window.onlanguagechange = function(event) {
  console.log('languagechange event detected!');
};

Check your Browser compatibility


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...