Nuxt/examples/use-state/plugins/locale.server.ts
Sébastien Chopin bf0933a2f0
docs: improve useState docs and add example (#1961)
Co-authored-by: pooya parsa <pyapar@gmail.com>
2021-11-16 14:17:52 +01:00

14 lines
625 B
TypeScript

/**
* Nuxt will automatically read the files in your plugins directory and load them.
* You can use .server or .client in the file name to load a plugin just on server- or client-side.
* https://v3.nuxtjs.org/docs/directory-structure/plugins
*/
export default defineNuxtPlugin((nuxtApp) => {
const locale = useLocale()
// Learn more about the nuxtApp interface on https://v3.nuxtjs.org/docs/usage/nuxt-app#nuxtapp-interface-advanced
const req = nuxtApp.ssrContext?.req
// Set default locale based request headers (browser locale)
locale.value = locale.value ?? req.headers['accept-language']?.split(',')[0]
})