You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This SDK provides comprehensive SSR support through middleware-based locale detection and server components. Here's how to implement end-to-end server-side translation:
120
+
This SDK provides reliable SSR support with automatic locale detection and server-side translation. The new approach is simple, predictable, and SEO-friendly.
121
121
122
-
### Middleware Setup for language detection
122
+
### Middleware Setup for Dynamic Locale Detection
123
123
124
-
Create a middleware file to detect user's locale from request headers or URL parameters:
124
+
Create a middleware file to detect user's locale and set up dynamic routing:
125
125
126
-
```tsx:/src/middleware.ts
126
+
```tsx
127
+
// src/middleware.ts
127
128
import { NextResponse } from"next/server";
128
129
importtype { NextRequest } from"next/server";
129
130
130
-
const defaultLocale ="en";
131
-
132
131
exportfunction middleware(request:NextRequest) {
133
-
const { searchParams } =newURL(request.url);
134
-
const localeParam =searchParams.get("locale");
135
-
132
+
const { pathname } =request.nextUrl;
133
+
134
+
// Skip middleware for API routes, static files, and Next.js internals or anything you want
### Initialize Translation Service (Singleton Pattern)
172
+
### Server Component Implementation
152
173
153
-
The SDK uses a singleton pattern for the TranslationService to ensure efficient caching and batch processing. Create a utility file to manage the translator instance:
174
+
The `withServerTranslation` HOC provides the cleanest server-side translation experience with zero string duplication. Here are the most common use cases:
<p>{t("This content is automatically translated")}</p>
192
+
{tf(
193
+
<>
194
+
Experience <strong>powerful</strong> and <em>reliable</em> translations!
195
+
</>
196
+
)}
197
+
<p>Current language: {locale}</p>
198
+
</div>
199
+
));
200
+
201
+
exportdefaultasyncfunction Page({
202
+
params,
203
+
}: {
204
+
params:Promise<{ locale:string }>;
205
+
}) {
206
+
const { locale } =awaitparams;
207
+
return <HomePagelocale={locale} />;
168
208
}
169
209
```
170
210
171
-
### Server Component Implementation
172
-
173
-
Create server components that utilize the detected locale:
174
-
175
-
> **Note**: For server-side rendering, all translations must be completed before sending the response to the client. This requires a two-step process: first mark texts for translation using t() , then execute all translations in a single batch with execute() . This ensures all translations are ready before rendering occurs.
title: translations["My Awesome App - Best Solution for Your Business"],
272
+
description:
273
+
translations[
274
+
"Discover the most powerful tools to grow your business online"
275
+
],
276
+
};
238
277
}
239
-
240
-
exportdefaultFormattedServerComponent;
241
278
```
242
279
243
-
### SEO Considerations
244
-
245
-
While our SDK currently supports server-side rendering of translated content, achieving full locale-specific visibility in search engine results requires additional implementation. We're working on this step by step example and welcome community contributions to:
246
-
247
-
- Implement canonical URL handling for localized content
248
-
- Develop locale-specific sitemap generation
249
-
- Show hreflang tag implementation
250
-
251
-
If you'd like to contribute examples or implementations for these features, please submit a Pull Request!
252
-
253
280
## Locale Format
254
281
255
282
The locale format follows the ISO 639-1 language code standard, optionally combined with an ISO 3166-1 country code:
| cacheTTL | number | No | Cache validity period in hours (default: 24) |
346
+
347
+
**Tips**: When `sourceLocale` === `targetLocale` no translation requests will be sent.
348
+
302
349
### Persist for Editing
303
350
304
351
The 'persist' means the string will be persisted so that you can review and edit in the [dashboard](https://dashboard.autolocalise.com), default is true, if the content is dynamic or you don't want to see in the dashboard, pass 'false'.
305
352
306
-
**Note**: Server-side rendering only works with persist = true by default.
0 commit comments