Loading localized texts/getting browser language in background service worker - Chrome extension manifest v3

Viewed 215

I am currently in the process of migrating my Chrome extension from manifest v2 to v3. My extension creates context menu options in the background.

The extension reacts to bunch of events such as on tab added, on window created:

chrome.tabs.onCreated.addListener(...);
chrome.windows.onCreated.addListener(...);

etc. In the callback function the extension tries to create/refresh context menu options created using chrome.contextMenus.

So far so good. Now when using background script in the version 2 of the manifest, I was able to use chrome.i18n.getMessage(...) to get localized texts, but after I moved to background service worker, I no longer can use this API since it's foreground only.

This brings me to my questions:

  1. How can I get localized text from the background service worker?

  2. Can I at least find out what is the current language setting to load the proper messages.json manually?

window.navigator.language is also unavailable.

  1. If that's not a way to go, then can you think of any alternative?

Any help appreciated. I'm still new to Chrome extensions and I would like to avoid any hacky solution. Maybe there's The Right Way to do this.

0 Answers
Related