DEV Community

Antoine for Itself Tools

Posted on

Unlocking Internationalization in Next.js with serverSideTranslations

In our journey at itselftools.com, having developed over 30 applications using the powerful combination of Next.js and Firebase, we've gained a notable amount of insights pertaining to the internationalization (i18n) of applications. Today, we're excited to share one of the key components that aid in deploying multilingual support for Next.js apps: a custom implementation of server-side translations.

Understanding the Code

Let's dive into the code snippet that showcases our approach:

serverSideTranslations: async (locale, namespacesRequired) => {
  return (await Promise.all(
    namespacesRequired.map(ns =>
      (await import(`../locales/${locale}/${ns}.json`)).default
    )
  )).reduce((obj, cur, i) => {
    obj[namespacesRequired[i]] = cur;
    return obj;
  }, {});
}
Enter fullscreen mode Exit fullscreen mode

What Does This Code Do?

This function, serverSideTranslations, facilitates the dynamic loading of namespace-specific translation files based on the locale provided. Here’s a stepwise breakdown of its operations:

  1. Dynamic Import: The code dynamically imports translation files for a given locale and the specified namespaces. Using template literals and the dynamic import() statement, it loads JSON files corresponding to each namespace. This is crucial for supporting multiple languages and regional settings.

  2. Promise.all for Concurrency: It uses Promise.all to handle concurrent promises, which are generated by mapping over namespacesRequired. This ensures that all translations are loaded simultaneously, thus optimizing the load time.

  3. Combining Results: Once all promises resolve, a reduce function accumulates the results into a single object. Each namespace's content is assigned to a corresponding property in the resultant object, making it easy to access any part of the translations.

Practical Benefits

This methodology offers a few clear benefits for Next.js applications:

  • Performance Boost: By asynchronously loading only necessary translation files, server performance and client load times are significantly improved.

  • Scalability: It easily scales for applications requiring support for multiple languages and can handle new namespaces or locales with minimal changes.

  • Maintainability: Organizing translations by namespaces within specific locale directories simplifies the management of translation assets.

In Conclusion

The serverSideTranslations function is a testament to our continuous pursuit of making web applications more accessible and user-friendly. To see this code in action, we invite you to visit some of our applications such as Test Your Mic Online, Explore Language Pronunciations, and Record Your Voice Online.

These implementations not only encourage best practices in Next.js but also demonstrate our commitment to delivering high-quality, efficient, and inclusive digital products.

Top comments (0)