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
feat: Introduce localization helper and integrate localization across API aggregates, handlers, and endpoints, while updating ETag responses to TypedResults.
@@ -4,247 +4,159 @@ This guide explains how to configure and use localization in the BookStore API.
4
4
5
5
## Overview
6
6
7
-
The BookStore API supports multiple languages for localized content (e.g., category names). The API automatically detects the client's preferred language from the `Accept-Language` HTTP header and returns localized content accordingly.
7
+
The BookStore API supports multiple languages for localized content (category names, book descriptions, author biographies). The API automatically detects the client's preferred language from the `Accept-Language` HTTP header and returns localized content accordingly.
8
8
9
9
## Configuration
10
10
11
-
Localization is configured in `appsettings.json` under the `Localization` section:
11
+
### Two-Letter Language Codes
12
12
13
-
```json
14
-
{
15
-
"Localization": {
16
-
"DefaultCulture": "en-US",
17
-
"SupportedCultures": ["en-US"]
18
-
}
19
-
}
20
-
```
21
-
22
-
### Configuration Options
23
-
24
-
#### `DefaultCulture`
25
-
-**Type**: `string`
26
-
-**Default**: `"en-US"`
27
-
-**Description**: The default culture to use when the client's preferred language is not supported
The API uses a **four-tier fallback strategy** to find the best translation:
62
+
## Fallback Strategy
129
63
130
-
1.**Full culture code**: First tries the complete culture identifier (e.g., `"pt-PT"`)
131
-
2.**Two-letter ISO language code**: If not found, uses `CultureInfo.TwoLetterISOLanguageName` to extract the language code (e.g., `"pt"` from `"pt-PT"`)
132
-
3.**English fallback**: If still not found, tries to use the English (`"en"`) translation
133
-
4.**First available**: As a last resort, uses the first available translation in the dictionary
64
+
The API uses a 6-step fallback to find the best translation:
134
65
135
-
**Example**:
136
-
- Client requests `Accept-Language: pt-PT`
137
-
- Category has translations for `"en"` and `"pt"` (but not `"pt-PT"`)
138
-
- API tries `"pt-PT"` → not found
139
-
- API creates `CultureInfo("pt-PT")` and gets `TwoLetterISOLanguageName` → `"pt"` → **found!** Returns Portuguese translation
140
-
- If `"pt"` wasn't found → tries `"en"` → returns English translation
141
-
- If `"en"` wasn't found → returns first available translation
66
+
1. Exact preferred culture (e.g., `"pt-PT"`)
67
+
2. Two-letter preferred code (e.g., `"pt"`)
68
+
3. Exact default culture (e.g., `"en"`)
69
+
4. Two-letter default code (e.g., `"en"`)
70
+
5. First available translation
71
+
6. Default value (empty string)
142
72
143
-
This ensures maximum compatibility even when exact culture matches aren't available, and handles edge cases correctly using .NET's built-in culture handling.
73
+
**Example**: Request with `Accept-Language: pt-BR`
74
+
- Tries `"pt-BR"` → `"pt"` → `"en"` → first available → default
75
+
- With hybrid translations above, returns `"Programação (Brasil)"`
144
76
145
-
### Fallback Behavior
146
-
If a translation is not available for the requested language:
147
-
1. The API falls back through the strategy above (full code → two-letter → English → first available)
148
-
2. No error is thrown
149
-
3. The response is still valid
77
+
## Usage
150
78
151
-
##Environment-Specific Configuration
79
+
### Making Requests
152
80
153
-
You can configure different languages for different environments:
81
+
Include the `Accept-Language` header in your HTTP requests:
0 commit comments