-
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Update error message for clarity on defined locales #252
Changes from 1 commit
1bb5c4f
860e13c
6435a19
41d5b2b
def2237
c2efdba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,30 +3,30 @@ import type { Context, ReactNode } from 'react'; | |
import React, { Suspense, use, useMemo } from 'react'; | ||
import { flattenLocale } from '../../common/flatten-locale'; | ||
import type { LocaleContext } from '../../types'; | ||
|
||
type I18nProviderProps = Omit<I18nProviderWrapperProps, 'fallback'>; | ||
|
||
type I18nProviderWrapperProps = { | ||
locale: string; | ||
fallback?: ReactNode; | ||
children: ReactNode; | ||
}; | ||
|
||
export const localesCache = new Map<string, Record<string, unknown>>(); | ||
|
||
export function createI18nProviderClient<Locale extends BaseLocale>( | ||
I18nClientContext: Context<LocaleContext<Locale> | null>, | ||
locales: ImportedLocales, | ||
fallbackLocale?: Record<string, unknown>, | ||
) { | ||
function I18nProvider({ locale, children }: I18nProviderProps) { | ||
let clientLocale: any = localesCache.get(locale); | ||
|
||
if (!clientLocale) { | ||
clientLocale = use(locales[locale as keyof typeof locales]()).default; | ||
const newLocale = locales[locale as keyof typeof locales]; | ||
if (!newLocale) { | ||
throw new Error( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about throwing directly an error - we could log it and try to fallback to another locale instead of crashing the page. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. I wasn't too concerned originally since a runtime error was occurring. That's nice idea to use fallback locale. However, if the fallback locale isn't provided since it's optional, which locale should we default to ? For now, I've implemented it to call notFound like useCurrentLocale. What do you think? 6435a19 |
||
`The locale '${locale}' is not supported. Defined locales are: [${Object.keys(locales).join(', ')}].`, | ||
); | ||
} | ||
clientLocale = use(newLocale()).default; | ||
localesCache.set(locale, clientLocale); | ||
} | ||
|
||
const value = useMemo( | ||
() => ({ | ||
localeContent: flattenLocale<Locale>(clientLocale), | ||
|
@@ -35,10 +35,8 @@ export function createI18nProviderClient<Locale extends BaseLocale>( | |
}), | ||
[clientLocale, locale], | ||
); | ||
|
||
return <I18nClientContext.Provider value={value}>{children}</I18nClientContext.Provider>; | ||
} | ||
|
||
return function I18nProviderWrapper({ locale, fallback, children }: I18nProviderWrapperProps) { | ||
return ( | ||
<Suspense fallback={fallback}> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you revert the deletion of empty lines? It makes it harder to read the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, it seems the formatter ran automatically. I'll fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed by 860e13c