You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
12 Hrs
24 Min
14 Seg

Internacionalizaci贸n avanzada: rutas dinamicas y middlewares

10/24
Resources

Internationalization in web applications is crucial to ensure a smooth experience for users of different languages. Here we explore how to implement internationalization from scratch in Next.js, using middleware and native JavaScript, to redirect users according to their configured language without relying on external libraries.

How to intercept and process requests in Next.js with middleware?

  • The middleware in Next.js allows intercepting requests between the client and the server.
  • We can use the request.nextUrl object to parse the requested URL and make decisions such as redirects or blocks.
  • The logic includes:
    • Ignoring requests outside the subpage intended for internationalization(/i18n in this case).
    • Allow direct pass-through if the URL already contains a supported language identifier (e.g., /i18n/en or /i18n/en).
    • Redirect requests without a language identifier to the most appropriate language according to the user's configuration.

How to determine the user's preferred language?

  • We use the HTTP Accept-Language header, which browsers send indicating the user's preferred languages.
  • With the negotiator library and format.js, you can:
    • Extract the languages accepted by the browser.
    • Compare with the languages supported by the application(es, en).
    • Select the default language(en) in case there is no match.

How is the logic for redirects structured in the middleware?

  1. Ignore irrelevant paths:

    • Check if the requested path starts with /i18n.
    • If not, let the request continue unchanged.
  2. Allow routes with configured language:

    • Analyze whether the URL contains a supported language.
    • If yes, allow the request to continue.
  3. Redirect routes with no language configured:

    • Determine the language using the Accept-Language header.
    • Modify the request pathname to include the language.
    • Perform the redirect based on this new URL.

How to test the implementation?

  • Change the preferred language in the browser settings.
  • Access the root page and verify the automatic redirection to the corresponding language.
  • Test with unsupported languages to ensure that the default language is assigned.

Contributions 2

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Donde se coloca este archivo ? Use the file `middleware.ts` (or `.js`) in the root of your project to define Middleware. For example, at the same level as `pages` or `app`, or inside `src` if applicable. <https://nextjs.org/docs/app/building-your-application/routing/middleware#convention>
Buenisimo, otro buen ejemplo es manejar el acceso a secciones en especifico de la pagina y quieres que un tipo de usuario sea el unico que pueda verlas.