Last active
January 16, 2023 05:14
-
-
Save n4bb12/26ae983f1ed6bd07f0d7c3eb1657a9a2 to your computer and use it in GitHub Desktop.
Simple React localization pattern
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
export const locales = ["de", "en"] as const | |
export type Locale = typeof locales[number] | |
export function useLocale(): Locale { | |
const router = useRouter() | |
return (router.locale as Locale) || locales[0] | |
} | |
export type LocalizedTexts<T> = Record<Locale, T> | |
export function useLocalizedText<T>(texts: LocalizedTexts<T>) { | |
const locale = useLocale() | |
return texts[locale] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment