简体   繁体   中英

How to get browser language in ServiceWorker?

如何在 ServiceWorker 中获取浏览器语言?

navigator.language || navigator.userLanguage; //not work

This should work now according to the spec .

If your users are using an older version of Chrome, you have to rely on the language read from the global (regular) window.navigator.language and pass it to the Service Worker via navigator.serviceWorker.controller.postMessage . Here is the old Chrome tracking bug .

Alternatively, you can try reading Accept-Language header from your Requests in fetch event handler - event.request.headers.get('Accept-Language') - although it didn't work for me for some reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM