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

Conversation

ichiwa
Copy link
Contributor

@ichiwa ichiwa commented Oct 19, 2023

Overview

Implemented a dedicated error message on the client side when an undefined locale is passed, using Next.js and App router.

Like this:
スクリーンショット 2023-10-19 14 18 47

close #251

@vercel
Copy link

vercel bot commented Oct 19, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-international ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 23, 2023 3:04pm

@@ -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

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

Copy link
Owner

@QuiiBz QuiiBz left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@QuiiBz QuiiBz merged commit fc4e60f into QuiiBz:main Oct 23, 2023
5 of 6 checks passed
@ichiwa ichiwa deleted the feature/enhance-locale-error-message-in-client branch October 24, 2023 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve error messaging for unknown locale key
2 participants