Skip to content
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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Copy link
Owner

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed by 860e13c

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(
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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),
Expand All @@ -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}>
Expand Down