|
| 1 | +using BookStore.ApiService.Infrastructure; |
1 | 2 | using BookStore.ApiService.Models; |
2 | 3 | using BookStore.ApiService.Projections; |
3 | 4 | using Marten; |
@@ -83,6 +84,7 @@ public static RouteGroupBuilder MapBookEndpoints(this RouteGroupBuilder group) |
83 | 84 | book.Title, |
84 | 85 | book.Isbn, |
85 | 86 | book.Language, |
| 87 | + LocalizeLanguageName(book.Language, language), |
86 | 88 | LocalizeDescription(book, language), |
87 | 89 | book.PublicationDate, |
88 | 90 | Helpers.BookHelpers.IsPreRelease(book.PublicationDate), |
@@ -159,6 +161,7 @@ static async Task<IResult> GetBook( |
159 | 161 | book.Title, |
160 | 162 | book.Isbn, |
161 | 163 | book.Language, |
| 164 | + LocalizeLanguageName(book.Language, language), |
162 | 165 | LocalizeDescription(book, language), |
163 | 166 | book.PublicationDate, |
164 | 167 | Helpers.BookHelpers.IsPreRelease(book.PublicationDate), |
@@ -296,4 +299,28 @@ static Models.CategoryDto LocalizeCategory(CategoryProjection category, string l |
296 | 299 | // Last resort: use first available biography |
297 | 300 | return author.Translations.Values.FirstOrDefault()?.Biography; |
298 | 301 | } |
| 302 | + |
| 303 | + // Helper method to get localized language name |
| 304 | + static string LocalizeLanguageName(string bookLanguageCode, string userLanguage) |
| 305 | + { |
| 306 | + // Validate both culture codes first to avoid exceptions |
| 307 | + if (!CultureCache.IsValidCultureCode(bookLanguageCode)) |
| 308 | + { |
| 309 | + return bookLanguageCode.ToUpperInvariant(); |
| 310 | + } |
| 311 | + |
| 312 | + var bookCulture = new System.Globalization.CultureInfo(bookLanguageCode); |
| 313 | + |
| 314 | + // If user language is invalid, fall back to the book's native name |
| 315 | + if (!CultureCache.IsValidCultureCode(userLanguage)) |
| 316 | + { |
| 317 | + return bookCulture.NativeName; |
| 318 | + } |
| 319 | + |
| 320 | + var userCulture = new System.Globalization.CultureInfo(userLanguage); |
| 321 | + |
| 322 | + // Get the display name of the book's language in the user's language |
| 323 | + // For example: if book is in "en" and user prefers "pt", this returns "Inglês" |
| 324 | + return userCulture.TextInfo.ToTitleCase(bookCulture.DisplayName); |
| 325 | + } |
299 | 326 | } |
0 commit comments