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
# 🌍 Server-Side Translations and Locale Information in Nuxt I18n Micro
6
6
7
7
## 📖 Overview
8
8
9
-
Nuxt I18n Micro supports server-side translations, allowing you to translate content on the server and return it as part of the response. This is particularly useful for APIs or server-rendered applications where localization is needed before reaching the client.
9
+
Nuxt I18n Micro supports server-side translations and locale information, allowing you to translate content and access locale details on the server. This is particularly useful for APIs or server-rendered applications where localization is needed before reaching the client.
10
10
11
-
The translations use locale messages defined in the Nuxt I18n configuration and are dynamically resolved based on the detected locale.
11
+
The translations use locale messages defined in the Nuxt I18n configuration and are dynamically resolved based on the detected locale.
12
12
13
13
To clarify, the translations used by Nuxt I18n Micro in server-side handling are only sourced from the root-level translation files. Any nested translation files or subdirectories are not utilized in this context. The system will only retrieve translations from the root folder, ensuring a consistent and manageable approach to server-side translation retrieval.
14
14
15
-
---
15
+
## 🛠️ Setting Up Server-Side Middleware
16
16
17
-
## 🛠️ Setting Up Server-Side Translations
17
+
To enable server-side translations and locale information, use the provided middleware functions:
18
18
19
-
To enable server-side translations, use the provided middleware to handle locale detection and return translations dynamically. The `useTranslationServerMiddleware` function is designed for this purpose.
19
+
-`useTranslationServerMiddleware` - for translating content
20
+
-`useLocaleServerMiddleware` - for accessing locale information
20
21
21
-
---
22
+
Both middleware functions are automatically available in all server routes.
23
+
24
+
The locale detection logic is shared between both middleware functions through the `detectCurrentLocale` utility function to ensure consistency across the module.
22
25
23
26
## ✨ Using Translations in Server Handlers
24
27
25
-
You can seamlessly translate content in any `eventHandler` by using the middleware.
28
+
You can seamlessly translate content in any `eventHandler` by using the translation middleware.
26
29
27
-
### Example: Basic Usage
30
+
### Example: Basic Translation Usage
28
31
```typescript
29
32
import { defineEventHandler } from'h3'
30
33
@@ -40,11 +43,83 @@ In this example:
40
43
- The user's locale is detected automatically from query parameters, cookies, or headers.
41
44
- The `t` function retrieves the appropriate translation for the detected locale.
Both middleware functions automatically determine the user's locale using the following priority order:
147
+
148
+
1.**URL Parameters**: `?locale=ru`
149
+
2.**Route Parameters**: `/ru/api/endpoint`
150
+
3.**Cookies**: `user-locale` cookie
151
+
4.**HTTP Headers**: `Accept-Language` header
152
+
5.**Fallback Locale**: As configured in module options
153
+
6.**Default Locale**: As configured in module options
154
+
7.**Hardcoded Fallback**: `'en'`
155
+
156
+
> **Note**: The locale detection logic is shared between `useLocaleServerMiddleware` and `useTranslationServerMiddleware` to ensure consistency across the module.
0 commit comments