Listening for Language Change Events
On this page
When the SwiftLingo language switcher changes the active language, it dispatches a custom DOM event called languageChange
. You can listen for this event in your own JavaScript code to react to language changes, for example, to update dynamic content or trigger other language-specific functionalities.
Event Details
- Event Name:
languageChange
- Event Type:
CustomEvent
- Detail Property: The
detail
property of the event object will contain an object with the following structure:{ language: 'fr' // The two-letter code of the newly selected language }
Example Usage
To listen for the languageChange
event, you can add an event listener to the window
object:
window.addEventListener('languageChange', (event) => {
const newLanguage = event.detail.language;
console.log('Language switched to:', newLanguage);
// Perform actions based on the new language
// For example, update content, reload a specific component, etc.
});
This event provides a powerful way to create a more dynamic and responsive multilingual user experience on your website.