Skip to content

Commit 5717cab

Browse files
committed
feat: Add localized language name to book DTOs and endpoints, removing raw language from search projections.
1 parent f9fe9cc commit 5717cab

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/ApiService/BookStore.ApiService/Endpoints/BookEndpoints.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BookStore.ApiService.Infrastructure;
12
using BookStore.ApiService.Models;
23
using BookStore.ApiService.Projections;
34
using Marten;
@@ -83,6 +84,7 @@ public static RouteGroupBuilder MapBookEndpoints(this RouteGroupBuilder group)
8384
book.Title,
8485
book.Isbn,
8586
book.Language,
87+
LocalizeLanguageName(book.Language, language),
8688
LocalizeDescription(book, language),
8789
book.PublicationDate,
8890
Helpers.BookHelpers.IsPreRelease(book.PublicationDate),
@@ -159,6 +161,7 @@ static async Task<IResult> GetBook(
159161
book.Title,
160162
book.Isbn,
161163
book.Language,
164+
LocalizeLanguageName(book.Language, language),
162165
LocalizeDescription(book, language),
163166
book.PublicationDate,
164167
Helpers.BookHelpers.IsPreRelease(book.PublicationDate),
@@ -296,4 +299,28 @@ static Models.CategoryDto LocalizeCategory(CategoryProjection category, string l
296299
// Last resort: use first available biography
297300
return author.Translations.Values.FirstOrDefault()?.Biography;
298301
}
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+
}
299326
}

src/ApiService/BookStore.ApiService/Models/BookDto.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public record BookDto(
88
string Title,
99
string? Isbn,
1010
string Language,
11+
string LanguageName,
1112
string? Description,
1213
PartialDate? PublicationDate,
1314
bool IsPreRelease,

src/ApiService/BookStore.ApiService/Projections/BookSearchProjection.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ static void UpdateSearchText(BookSearchProjection projection)
164164
$"{projection.Title} " +
165165
$"{allDescriptions} " +
166166
$"{projection.Isbn ?? string.Empty} " +
167-
$"{projection.Language} " +
168167
$"{projection.PublisherName ?? string.Empty} " +
169168
$"{projection.AuthorNames}".Trim();
170169
}

0 commit comments

Comments
 (0)