How to setup Tolgee with Next.js App Router
Aug 11, 2023
·
1 min read

With the introduction of Next.js 13 and the app router featuring React server components, there has been a strong push to enable Tolgee to work within this new paradigm.
Considering the app router is still in beta and Next.js API adjustments might occur, we opted for this article (instead of creating an npm package right away) to explain how Tolgee can work with the server.
This article is outdated in technical details, read the new documentation.
Server components are a stripped-down version of regular components without React hooks but with async capabilities. Let's see, how we can set them up with Tolgee.
How Tolgee In-Context Works
Tolgee's unique method of enabling users to directly edit translations within the app is based on inserting invisible characters next to the actual translations. This creates a kind of
watermark
for each translation, making it detectable in the DOM and allowing precise localization. This approach offers a non-intrusive way to empower users to translate in the context of their app.
The General Plan
For server components, the approach involves including a complete key name within the invisible watermark
characters. Additionally, the development translation files need to be loaded on the server to facilitate rendering. On the client side, the SDK will recognize the server-rendered watermarks
and enable the in-context translation functionality.
In Development mode, data will be fetched directly from Tolgee platform with each request to the server. In Production mode, we'll include the locale data directly into the bundle, so no fetching is necessary.
Setting Up the Configuration
To initiate a new project, we will create a fresh Next.js 13 project (with the app directory enabled):
As the i18n support is currently limited for the app directory, we'll use an external library called next-intl to assist with routing and locale management.
For now, we need to install a beta version (updated as of the writing date) and the latest version of @tolgee/react:
Configuring next-intl
and Tolgee
The folder structure needs to be adjusted to resemble the following:
The app structure and middleware play essential roles in the next-intl setup. The Tolgee folder serves for both client and server configurations with shared properties. The i18n folder contains the static files (exported from the Tolgee platform or left empty for now).
Below is the configuration for the middleware
file:
To gain a comprehensive understanding of how
next-intl
operates, check their docs. We are only utilizing the necessary setup for proper routing, hence not all the listed configurations are required.
Now, let's establish a shared configuration that will apply to both the client and server.
For this to work, create a project in tolgee platform, get the api key in integration section. Also I assume you have your exported language files in
i18n
folder (as is visible in the file structure above).
The client part remains largely unchanged from the pages
directory setup. It serves the purpose of translating client components and also enables in-context functionality for server-rendered components.
The only significant change is the listener for the permanentChange
event. This event triggers when a translation is updated through an in-context dialog, allowing for a server component refresh with router.refresh
.
Now, let's proceed to the server part. As server components don't support React hooks, we need to recreate similar abstractions to those found in @tolgee/react
ourselves. Fortunately, this process is relatively straightforward; we can essentially utilize vanilla Tolgee, with proper configuration and caching of the instance.
Re-creation of
T
component is a bit more complicated, because we need to copy some code from@tolgee/react
, but you check how to do it in the example repo.
Let's set up the provider
Here is how we apply the TolgeeNextProvider
in the layout.tsx
This is an equivalent of getInitialProps
(or its related methods) because this is the place where we load pick relevant locale already on the backend and supply it to the client component through the props.
If you want to provide each page with different namespace, you can move the provider to the page files, however this example provides the translations globally
How to use it
Let's see how we can localize server components:
If everything is set up correctly, the 'page-example-title' should be alt + click
able. Make sure you've defined your project credentials in the .env.development.local
file.
For client components, you can use the regular React
integration:
Switching Languages
For switching languages use the following code:
If you encounter any issues making it work, you can clone the example app and kickstart your journey from there.
Limitations of Server Components
Although in-context translation works with server components, there are some limitations compared to client components. Since the Tolgee cache on the server is separate, Tolgee can't automatically change the translation when creating a screenshot (unlike with client components, which swap the content if you've modified it in a dialog).
Furthermore, if you're using the Tolgee plugin, it won't affect the server's transition to dev mode. As a result, only the client switches, leaving server components non-editable in this mode.
Conclusion and future steps
Given that server components exist in a completely distinct environment, I might create a separate next.js package in the future.
Moreover, the standard usage of Server components is yet to be fully determined. It's possible they might only be utilized for data fetching, but the future holds the answer.
If you've got any suggestions or ideas for improvements, feel free to share them on our slack or open a Github issue. And if you're fond of Tolgee, don't forget to give us a Github star.