We were panicking when trying to access the locale in a worker. This is because unfortunately, window isn't defined, so Navigator has to be accessed using self instead. This PR implements that to make sure we can reliably use sys-locale in both normal workers as well as service workers.
Additional thoughts
Ideally I'd like to see this sort of thing solved at a lower level, but in general the ecosystem seems to be somewhat immature in its support for anything other than page contexts. As an example - calling web_sys::window() (which returns an option, indicating it will gracefully handle failure) panics in a worker. This makes it hard to gracefully handle and is why my approach here was to implement some new bindings with wasm-bindgen.
I plan to see what we can do to contribute more upstream in the future but I wanted to fix this here to begin with.
Overview
We were panicking when trying to access the locale in a worker. This is because unfortunately,
window
isn't defined, soNavigator
has to be accessed usingself
instead. This PR implements that to make sure we can reliably use sys-locale in both normal workers as well as service workers.Additional thoughts
Ideally I'd like to see this sort of thing solved at a lower level, but in general the ecosystem seems to be somewhat immature in its support for anything other than page contexts. As an example - calling
web_sys::window()
(which returns an option, indicating it will gracefully handle failure) panics in a worker. This makes it hard to gracefully handle and is why my approach here was to implement some new bindings with wasm-bindgen.I plan to see what we can do to contribute more upstream in the future but I wanted to fix this here to begin with.
How to test
sys-locale
branchmake
In the console, you should see the current locale printed from within a worker.