QuiiBz / next-international

Type-safe internationalization (i18n) for Next.js
https://next-international.vercel.app
MIT License
1.19k stars 52 forks source link

What should be the locale type #381

Closed addlistener closed 3 months ago

addlistener commented 3 months ago

What should be the locale type here?

// pages/_app.tsx
import { I18nProvider } from '../locales'
import { AppProps } from 'next/app'

export default function App({ Component, pageProps }: AppProps) {
  return (
    <I18nProvider locale={pageProps.locale}>
      <Component {...pageProps} />
    </I18nProvider>
  )
}

I tried using typeof en but there's type mismatch. Even though all the locales are the same.

// locales/en.ts
export default {
  "product slogan": "xxxx",

  "dash": {
    "leftnav": {
      "sites": "xxxx",
      "account": "xxxx"
    }
  }
} as const

I don't know it's FlattenLocale or as const. Still thinking...

The deducted type

image

My type

image
QuiiBz commented 3 months ago

It's a complex type: to avoid manually writing it, you can use:

ComponentProps<typeof I18nProvider>['locale']
addlistener commented 3 months ago

Thank you! It would be great this could be included in the official examples