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

feat!: typed i18n #6343

Merged
merged 16 commits into from
May 14, 2024
Merged

feat!: typed i18n #6343

merged 16 commits into from
May 14, 2024

Conversation

AlessioGr
Copy link
Member

@AlessioGr AlessioGr commented May 13, 2024

Description

BREAKING: all i18n functions are now strongly typed, which will create errors if you have custom translations. If you do, you will have to pass a generic to i18n containing the types of your custom translations

Example of adding your own translations to the default translations type on the client:

const linkTranslations = {
  en: {
    lexical: {
      link: {
        custom: 'Custom',
        internal: 'Internal',
      },
    },
  },
}

type LinkTranslationKeys = NestedKeysStripped<typeof linkTranslations.en>

type MyI18n = I18nClient<typeof linkTranslations.en, LinkTranslationKeys>

const myI18n: MyI18n

myI18n.t('lexical:link:custom') // custom translations have been ADDED to the default client translations

Example for the useTranslation hook within a component:

const linkTranslations = {
  en: {
    lexical: {
      link: {
        custom: 'Custom',
        internal: 'Internal',
      },
    },
  },
}

type LinkTranslationKeys = NestedKeysStripped<typeof linkTranslations.en>

export const MyComponent: React.FC = () => {
  const { i18n, t } = useTranslation<typeof linkTranslations.en, LinkTranslationKeys>()

  return t('lexical:link:custom')
}
  • I have read and understand the CONTRIBUTING.md document in this repository.

Type of change

  • Chore (non-breaking change which does not add functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Change to the templates directory (does not affect core functionality)
  • Change to the examples directory (does not affect core functionality)
  • This change requires a documentation update

Checklist:

  • I have added tests that prove my fix is effective or that my feature works
  • Existing test suite passes locally with my changes
  • I have made corresponding changes to the documentation

packages/next/src/utilities/getNextRequestI18n.ts Outdated Show resolved Hide resolved
packages/translations/src/languages/ar.ts Show resolved Hide resolved
packages/translations/src/types.ts Show resolved Hide resolved
Copy link
Member

@denolfe denolfe left a comment

Choose a reason for hiding this comment

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

Let's be sure to regenerate the keys in a subsequent PR in order to remove the @ts-nocheck comments.

@denolfe denolfe merged commit f716122 into beta May 14, 2024
35 checks passed
@denolfe denolfe deleted the feat/i18n-types branch May 14, 2024 15:10
paulpopus pushed a commit that referenced this pull request May 14, 2024
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.

None yet

2 participants