QuiiBz / next-international

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

feat(next-international): improve config API #125

Closed QuiiBz closed 1 year ago

QuiiBz commented 1 year ago

Improved config API

⚠️ BREAKING

Improve the API for configuring the App Router behaviors by moving all configurations from specific functions to createI18n* functions. This avoids duplicating multiple times the same configuration, and will allow for more configuration options in the future.

These changes are only affecting you if you were using these options.

basePath config

Before:

// locales/client.ts
const { useChangeLocale } = createI18nClient({
  en: () => import('./en'),
  fr: () => import('./fr')
})

// In a Client Component
const changeLocale = useChangeLocale({
  basePath: '/base'
})

After:

// locales/client.ts
const { useChangeLocale } = createI18nClient({
  en: () => import('./en'),
  fr: () => import('./fr')
}, {
  basePath: '/base'
})

// In a Client Component
const changeLocale = useChangeLocale()

segmentName config

Before:

// locales/client.ts
const { useCurrentLocale } = createI18nClient({
  en: () => import('./en'),
  fr: () => import('./fr')
})

// in a Client Component
const currentLocale = useCurrentLocale({
  segmentName: 'locale'
})

// locales/server.ts
export const { getStaticParams } = createI18nServer({
    en: () => import('./en'),
    fr: () => import('./fr')
})

// in a page
export function generateStaticParams() {
  return getStaticParams({
    segmentName: 'locale'
  })
}

After:

// locales/client.ts
const { useCurrentLocale } = createI18nClient({
  en: () => import('./en'),
  fr: () => import('./fr')
}, {
  segmentName: 'locale'
})

// in a Client Component
const currentLocale = useCurrentLocale()

// locales/server.ts
export const { getStaticParams } = createI18nServer({
    en: () => import('./en'),
    fr: () => import('./fr')
}, {
  segmentName: 'locale'
})

// in a page
export function generateStaticParams() {
  return getStaticParams()
}