Skip to content

Releases: QuiiBz/next-international

1.0.0

13 Sep 14:06
2985fb7
Compare
Choose a tag to compare

next-international 1.0.0 is the first production-ready release, with feature parity across App and Pages Router. We have a brand new documentation website, Static Rendering for the App Router, new options, and improved APIs!

Upgrade now by installing the latest version:

pnpm install next-international@latest
  • 🎉 New features
    • New documentation and website (App & Pages Router)
    • Static Rendering / Static Export support (App Router)
    • New rewriteDefault strategy (App Router)
    • Fallback locale on the server (App Router)
    • Allow catching not found locale errors (App Router)
  • ⚠️ Breaking changes
    • Improved Middleware API (App Router)
    • Improved config API (App Router)

New documentation and website (App & Pages Router)

We now have a proper documentation website that you can browse at https://next-international.vercel.app! The documentation itself has also been improved to make it easier to understand and highlight code changes:

Screenshot 2023-08-21 at 08 33 20

Static Rendering / Static Export support (App Router)

next-international now supports Static Rendering, meaning your pages can be rendered at build time and then be served statically from CDNs, resulting in faster TTFB.

See setup

Export getStaticParams from createI18nServer:

// locales/server.ts
export const {
  getStaticParams,
  ...
} = createI18nServer({
  ...
})

Inside all pages that you want to be statically rendered, call this setStaticParamsLocale function by giving it the locale page param:

// app/[locale]/page.tsx and any other page
import { setStaticParamsLocale } from 'next-international/server'

export default function Page({ params: { locale } }: { params: { locale: string } }) {
  setStaticParamsLocale(locale)

  return (
    ...
  )
}

And export a new generateStaticParams function. If all your pages should be rendered statically, you can also move this to the root layout:

// app/[locale]/page.tsx and any other page, or in the root layout
import { getStaticParams } from '../../locales/server'

export function generateStaticParams() {
  return getStaticParams()
}

New rewriteDefault strategy (App Router)

You can now choose to only rewrite the URL for the default locale, and keep others locale in the URL (e.g use /products instead of /en/products, but keep /fr/products) using the new rewriteDefault strategy:

const I18nMiddleware = createI18nMiddleware({
  locales: ['en', 'fr'],
  defaultLocale: 'en',
  urlMappingStrategy: 'rewriteDefault'
})

Fallback locale on the server (App Router)

This release adds missing support for fallback locales on the server with the App Router, similar to the fallbackLocale prop of I18nProviderClient. Navigate to locales/server.ts and add a new fallbackLocale option:

// locales/server.ts
import en from './en'

export const {
  ...
} = createI18nServer({
  ...
}, {
  fallbackLocale: en
})

Allow catching not found locale errors (App Router)

When a locale can't be found, we now use notFound() instead of throwing an un-catchable error. This allows to render a not-found.ts file easily.

Improved Middleware API (App Router)

⚠️ BREAKING (App Router)

Improve the middleware API for the App Router by using a single object argument and removing the need for as const for the locales:

See changes

Before:

const I18nMiddleware = createI18nMiddleware(['en', 'fr'] as const, 'fr')

// With all options:
const I18nMiddleware = createI18nMiddleware(['en', 'fr'] as const, 'fr', {
  urlMappingStrategy: 'rewrite',
  resolveLocaleFromRequest: request => {
    // Do your logic here to resolve the locale
    return 'fr'
  }
})

After:

const I18nMiddleware = createI18nMiddleware({
  locales: ['en', 'fr'],
  defaultLocale: 'en'
})

// With all options:
const I18nMiddleware = createI18nMiddleware({
  locales: ['en', 'fr'],
  defaultLocale: 'en',
  urlMappingStrategy: 'rewrite',
  resolveLocaleFromRequest: request => {
    // Do your logic here to resolve the locale
    return 'fr'
  }
})

Improved config API (App Router)

⚠️ BREAKING (App Router)

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.

See changes for basePath and segmentName

basePath config

Before:

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

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

After:

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

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

segmentName config

Before:

// locales/client.ts
export 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
export 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()
}

What's Changed

Full Changelog: 0.9.5...1.0.0

0.9.5

02 Sep 07:43
Compare
Choose a tag to compare

What's Changed

  • fix(next-international): infinite redirect with rewrite strategy by @QuiiBz in #146

Full Changelog: 0.9.4...0.9.5

0.9.4

30 Aug 06:34
Compare
Choose a tag to compare

What's Changed

  • fix(next-international): changeLocale with subsequent navigations by @QuiiBz in #140

Full Changelog: 0.9.3...0.9.4

0.9.3

23 Aug 15:41
Compare
Choose a tag to compare

What's Changed

  • feat(next-international): allow configuring segment name in getStaticParams by @QuiiBz in #124
  • feat: add license field to package.json by @QuiiBz in #130
  • chore: remove minify on build by @QuiiBz in #135
  • feat(next-international): remove locale for useChangeLocale with rewrite strate by @QuiiBz in #137

Full Changelog: 0.9.2...0.9.3

0.9.2

18 Aug 16:37
Compare
Choose a tag to compare

What's Changed

  • chore: update deps by @QuiiBz in #116
  • fix(next-international): update middleware matcher to work with images by @QuiiBz in #119
  • fix(next-international): type of useCurrentLocale by @QuiiBz in #123

Full Changelog: 0.9.1...0.9.2

0.9.1

13 Aug 07:43
Compare
Choose a tag to compare

What's Changed

  • chore: add codesandbox example by @QuiiBz in #112
  • fix(next-international): pathname starting with locale error by @QuiiBz in #113

Full Changelog: 0.9.0...0.9.1

0.9.0

12 Aug 06:49
Compare
Choose a tag to compare

App Router

Override the user's locale resolution

If needed, you can override the resolution of a locale from a Request, which by default will try to extract it from the Accept-Language header. This can be useful to force the use of a specific locale regardless of the Accept-Language header. Note that this function will only be called if the user doesn't already have a Next-Locale cookie.

Navigate to the middleware.ts file and implement a new resolveLocaleFromRequest function:

// middleware.ts
const I18nMiddleware = createI18nMiddleware(['en', 'fr'] as const, 'fr', {
  resolveLocaleFromRequest: request => {
    // Do your logic here to resolve the locale
    return 'fr'
  }
})

What's Changed

  • chore(international-types): update examples by @QuiiBz in #105
  • chore: add sponsors to readme by @QuiiBz in #107
  • feat(next-international): override locale resolution by @QuiiBz in #111

Full Changelog: 0.8.2...0.9.0

0.8.2

07 Aug 16:24
Compare
Choose a tag to compare

What's Changed

  • chore: update middleware regex by @QuiiBz in #98
  • fix(next-international): use default locale when Accept-Language is unknown by @QuiiBz in #101

Full Changelog: 0.8.0...0.8.2

0.8.0

03 Aug 06:47
Compare
Choose a tag to compare

App Router

Rewrite the URL to hide the locale

You might have noticed that by default, next-international redirects and shows the locale in the URL (e.g /en/products). This is helpful for users, but you can transparently rewrite the URL to hide the locale (e.g /products).

Navigate to the middleware.ts file and set the urlMappingStrategy to rewrite (the default is redirect):

// middleware.ts
const I18nMiddleware = createI18nMiddleware(['en', 'fr'] as const, 'fr', {
    urlMappingStrategy: 'rewrite'
})

useChangeLocale with basePath

When using useChangeLocale and if you have set a basePath option inside next.config.js, you'll also need to set it here:

const changeLocale = useChangeLocale({
  basePath: '/your-base-path'
})

What's Changed

  • fix(international-types): plural tags by @QuiiBz in #80
  • chore: improve README by @QuiiBz in #82
  • feat(next-international): useParams & rewrite strategy to hide locale from URL by @EdmundKorley in #83
  • fix(international-types): plurals params with scope by @QuiiBz in #88
  • fix(next-international): useChangeLocale with basePath by @QuiiBz in #90
  • fix(next-international): derive plurals from fallback locale by @ArmanAryanpour in #79
  • fix(next-international): plural with scopes by @QuiiBz in #91

New Contributors

Full Changelog: 0.7.0...0.8.0

0.7.0

28 Jul 06:58
Compare
Choose a tag to compare

Plurals

Plural translations work out of the box without any external dependencies, using the Intl.PluralRules API, which is supported in all browsers and Node.js.

To declare plural translations, append # followed by zero, one, two, few, many or other:

// locales/en.ts
export default {
  'cows#one': 'A cow',
  'cows#other': '{count} cows'
} as const

The correct translation will then be determined automatically using a mandatory count parameter. This works with the Pages Router, App Router in both Client and Server Components, and with scoped translations:

export default function Page() {
  const t = useI18n()

  return (
    <div>
      {/* Output: A cow */}
      <p>{t('cows', { count: 1 })}</p>
      {/* Output: 3 cows */}
      <p>{t('cows', { count: 3 })}</p>
    </div>
  )
}

What's Changed

  • chore(next-international): remove peer dependencies by @QuiiBz in #76
  • feat: add plurals support by @QuiiBz in #75
  • feat: typesafe plural count by @QuiiBz in #78

Full Changelog: 0.6.4...0.7.0