From 6be36d5c004479f64b371f384734277d7399b424 Mon Sep 17 00:00:00 2001 From: arnaud Date: Fri, 3 Nov 2023 14:36:26 +0100 Subject: [PATCH] fix(i18n): prevent error "ReferenceError: navigator is not defined" --- app/tools/i18n/intl-provider.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/tools/i18n/intl-provider.tsx b/app/tools/i18n/intl-provider.tsx index 4b4a6cc..78350b4 100644 --- a/app/tools/i18n/intl-provider.tsx +++ b/app/tools/i18n/intl-provider.tsx @@ -1,7 +1,7 @@ 'use client'; import React, { - ReactElement, ReactNode, useContext, useState, + ReactElement, ReactNode, useContext, useEffect, useState, } from 'react'; import { FormattedMessage, @@ -36,7 +36,12 @@ export const getLocale = (preferredLocale: string) => { export default function IntlProvider( { children, overriddenTranslations = {} }:{ children: ReactElement, overriddenTranslations?: Partial }, ) { - const [locale, setLocale] = useState(getLocale(new Intl.Locale(navigator.language).language)); + const [locale, setLocale] = useState('en'); + + useEffect(() => { + setLocale(getLocale(new Intl.Locale(navigator.language).language)); + }, []); + const switchLanguage = (chosenLocale: string) => setLocale(chosenLocale); const translations : Record = {