Back

Technologies:

javascriptjavascript
avatar
Tolerim
a month ago

How can I identify a language change in the window and update the language of my Electron application?

My goal is to determine when the window language changes so that I can adjust the language of the application accordingly. Currently, I'm able to determine the current language using app.getSystemLocale() in the electron's main file. However, I'm unsure of how to automatically detect any changes in the Windows language. Unfortunately, I couldn't find any relevant information in the documentation.

Answers(2)
avatar
Vogi
a month ago
Verified Answer
You can detect the change in the window language by using the following code in your electron app:
app.on('language-changed', () => {
  console.log('Language has been changed');
});
This event is emitted when the user changes the system language. You can then use app.getLocale() to get the new language and adjust your application accordingly. I hope this helps! Let me know if you have any other questions.

Sources

Detect change in window language and change language of Electron appDetect system language change in WPFSpellCheckersystemPreferences | Electron
avatar
Tolerim
a month ago
Unfortunately, there is no built-in event in Electron to detect when system language is changed. However, you can accomplish this using a third-party library like "windows-locale" or "electron-windows-locale". These libraries provide a way to listen for Windows language change events and trigger callbacks in your application code. Here is an example of how to use "windows-locale" library: 1. Install the library using npm:
2. In your main Electron file, require the library and use its onDidChange method to listen for language change events:
const { app } = require('electron');
   const { onDidChange } = require('windows-locale');
   
   app.on('ready', () => {
     onDidChange((newLocale) => {
       // Do something with the new system language here
     });
   });
This code will trigger the onDidChange callback every time the system language is changed, passing the new language code as a parameter to the callback function. You can then use this code to automatically change your application's language based on the system language.
;