Skip to content

Instantly share code, notes, and snippets.

@fwojciec
Created September 3, 2019 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwojciec/b9513ba66740711f3afdd13d83744565 to your computer and use it in GitHub Desktop.
Save fwojciec/b9513ba66740711f3afdd13d83744565 to your computer and use it in GitHub Desktop.
How to build a multilingual website in Next.js #2
// translations/getInitialLocale.ts
import { defaultLocale } from './config'
import { Locale, isLocale } from './types'
export function getInitialLocale(): Locale {
// preference from the previous session
const localSetting = localStorage.getItem('locale')
if (localSetting && isLocale(localSetting)) {
return localSetting
}
// the language setting of the browser
const [browserSetting] = navigator.language.split('-')
if (isLocale(browserSetting)) {
return browserSetting
}
return defaultLocale
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment