Created
September 3, 2019 13:02
-
-
Save fwojciec/b9513ba66740711f3afdd13d83744565 to your computer and use it in GitHub Desktop.
How to build a multilingual website in Next.js #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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