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

Allow t() to return falsy result instead of translation key or provide a way to add a fallback value #580

Closed
wscourge opened this issue Oct 26, 2023 · 4 comments
Labels
enhancement New feature or request unconfirmed Needs triage.

Comments

@wscourge
Copy link
Contributor

wscourge commented Oct 26, 2023

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

For Next.js 13 Metadata, I want to do the following:

export async function generateMetadata({ params: { locale }) {
  const t = await getTranslator(locale, "home")

  return {
    title: t("title"),
    openGraph: {
      title: t("seo.og.title") || t("seo.title") || t("title"),
    }
  }
}

Describe the solution you'd like

Either will do:

  1. t.optional('home') that'd to return "" or other fals-y value. Similar to t.rich().
  2. t('seo.og.title', null, t('seo.title', null, t('title'))), where the 3rd arguments is a fallback value.

Describe alternatives you've considered

As of now, I'm doing it with my customized version of the t() function:

export const t = async (locale: Locale, at: string, paths: Array<string>) => {
  const t = await getTranslator(locale, at)
  return t(paths.find((path: string) => !t(path).startsWith(`${at}.`)))
}

// usage
t(locale, "home", ["seo.og.title", "seo.title", "title"])

It works, but results in the pretty annoying, irrelevant error:

IntlError: MISSING_MESSAGE: Could not resolve seo.og.title in site.home.
at getFallbackFromErrorAndNotify (webpack-internal:///(rsc)/./node_modules/use-intl/dist/development/createIntl-dfba4462.js:131:23)
at translateBaseFn (webpack-internal:///(rsc)/./node_modules/use-intl/dist/development/createIntl-dfba4462.js:153:20)
at translateFn (webpack-internal:///(rsc)/./node_modules/use-intl/dist/development/createIntl-dfba4462.js:222:24)
at base (webpack-internal:///(rsc)/./node_modules/use-intl/dist/development/core.js:24:16)
at eval (webpack-internal:///(rsc)/./lib/i18n.ts:26:34)
at Array.find ()
at t (webpack-internal:///(rsc)/./lib/i18n.ts:26:20)
at async Promise.all (index 4)
at async localizedPageMetadata (webpack-internal:///(rsc)/./lib/seo.ts:44:82) {
code: 'MISSING_MESSAGE',
originalMessage: 'Could not resolve seo.tw.title in site.home.'
}

@wscourge wscourge added enhancement New feature or request unconfirmed Needs triage. labels Oct 26, 2023
@amannn
Copy link
Owner

amannn commented Oct 26, 2023

Thanks for the request, this seems to be a duplicate of #88. I'll close this ticket in favor of the existing one.

@amannn amannn closed this as completed Oct 26, 2023
@wscourge
Copy link
Contributor Author

Sure, sorry for the dupe

@wscourge
Copy link
Contributor Author

Is there currently a way to suppress the error tho?

@amannn
Copy link
Owner

amannn commented Oct 26, 2023

Yep, please see the alternatives section in the linked issue.

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