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

Checking if translation exists #448

Closed
chemicstry opened this issue Aug 10, 2023 · 1 comment
Closed

Checking if translation exists #448

chemicstry opened this issue Aug 10, 2023 · 1 comment
Labels
enhancement New feature or request unconfirmed Needs triage.

Comments

@chemicstry
Copy link

Is your feature request related to a problem? Please describe.

Currently when translation is not available, the return value is just the translation key, which makes checking for translation existence hard.

Describe the solution you'd like

It would be nice to have an option, which would return null when translation is not found instead of translation key.

For example:

// Without specifying options, keyAsDefault is true, which is the current behavior
const t = useTranslations('Error', { keyAsDefault: false });

const error: string = t('myError')!; // I know this must exist
const errorHelp: string | null = t('myErrorHelpText'); // Optional help text, which might not be available

The suggested API is not ideal, maybe someone has a more convenient idea.

Describe alternatives you've considered

The only alternative I could think of is checking if returned string matches key, which is very verbose:

const t = useTranslations('Error');

const error: string = t('myError');
const errorHelp: string | null = t('myErrorHelpText') === 'Error.myErrorHelpText' ? null : t('myErrorHelpText');
@chemicstry chemicstry added enhancement New feature or request unconfirmed Needs triage. labels Aug 10, 2023
@amannn
Copy link
Owner

amannn commented Aug 10, 2023

This mostly looks like a duplicate of #88.

Note however that the message fallback can be customized, that should likely work for your use case: https://next-intl-docs.vercel.app/docs/configuration#error-handling

@amannn amannn closed this as completed Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

2 participants