feat: translate recommendations and menu dishes into user language

- Generate recipes in English (reverted prompt to English-only)
- Add TranslateRecipes to OpenAI client (translate.go) — sends compact
  JSON payload of translatable fields, merges back into original recipes
- recommendation/handler.go: translate recipes in-memory before response
  when lang != "en"; falls back to English on error
- dish/repository.go: Create() now returns (dishID, recipeID, err) so
  callers can upsert dish_translations after saving
- menu/handler.go: saveRecipes returns savedRecipeEntry slice with dishID;
  saveDishTranslations calls TranslateRecipes then UpsertTranslation for
  each dish when the request locale is not English
- savedrecipe/repository.go: updated to ignore dishID from Create()
- init.go: wire openaiClient as RecipeTranslator and dishRepository as
  DishTranslator for menu.NewHandler

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-23 17:40:19 +02:00
parent cba50489be
commit bffeb05a43
7 changed files with 278 additions and 55 deletions

View File

@@ -35,9 +35,19 @@ type ProductLister interface {
ListForPromptByIDs(ctx context.Context, userID string, ids []string) ([]string, error)
}
// RecipeSaver creates a dish+recipe and returns the new recipe ID.
// RecipeSaver creates a dish+recipe and returns the dish ID and recipe ID.
type RecipeSaver interface {
Create(ctx context.Context, req dish.CreateRequest) (string, error)
Create(ctx context.Context, req dish.CreateRequest) (dishID, recipeID string, err error)
}
// DishTranslator upserts a translated dish name into the translation table.
type DishTranslator interface {
UpsertTranslation(ctx context.Context, dishID, lang, name string) error
}
// RecipeTranslator translates English recipe text fields into a target language.
type RecipeTranslator interface {
TranslateRecipes(ctx context.Context, recipes []ai.Recipe, targetLang string) ([]ai.Recipe, error)
}
// MenuGenerator generates a 7-day meal plan via an AI provider.
@@ -47,30 +57,36 @@ type MenuGenerator interface {
// Handler handles menu and shopping-list endpoints.
type Handler struct {
repo *Repository
menuGenerator MenuGenerator
pexels PhotoSearcher
userLoader UserLoader
productLister ProductLister
recipeSaver RecipeSaver
repo *Repository
menuGenerator MenuGenerator
translator RecipeTranslator
dishTranslator DishTranslator
pexels PhotoSearcher
userLoader UserLoader
productLister ProductLister
recipeSaver RecipeSaver
}
// NewHandler creates a new Handler.
func NewHandler(
repo *Repository,
menuGenerator MenuGenerator,
translator RecipeTranslator,
dishTranslator DishTranslator,
pexels PhotoSearcher,
userLoader UserLoader,
productLister ProductLister,
recipeSaver RecipeSaver,
) *Handler {
return &Handler{
repo: repo,
menuGenerator: menuGenerator,
pexels: pexels,
userLoader: userLoader,
productLister: productLister,
recipeSaver: recipeSaver,
repo: repo,
menuGenerator: menuGenerator,
translator: translator,
dishTranslator: dishTranslator,
pexels: pexels,
userLoader: userLoader,
productLister: productLister,
recipeSaver: recipeSaver,
}
}
@@ -172,13 +188,18 @@ func (h *Handler) GenerateMenu(w http.ResponseWriter, r *http.Request) {
h.fetchImages(r.Context(), days)
planItems, saveError := h.saveRecipes(r.Context(), days)
savedEntries, saveError := h.saveRecipes(r.Context(), days)
if saveError != nil {
writeError(w, r, http.StatusInternalServerError, "failed to save recipes")
return
}
planID, txError := h.repo.SaveMenuInTx(r.Context(), userID, weekStart, planItems)
lang := locale.FromContext(r.Context())
if lang != "en" {
h.saveDishTranslations(r.Context(), savedEntries, lang)
}
planID, txError := h.repo.SaveMenuInTx(r.Context(), userID, weekStart, planItemsFrom(savedEntries))
if txError != nil {
slog.ErrorContext(r.Context(), "save menu plan", "err", txError)
writeError(w, r, http.StatusInternalServerError, "failed to save menu plan")
@@ -246,13 +267,18 @@ func (h *Handler) generateForDates(w http.ResponseWriter, r *http.Request, userI
h.fetchImages(r.Context(), days)
planItems, saveError := h.saveRecipes(r.Context(), days)
savedEntries, saveError := h.saveRecipes(r.Context(), days)
if saveError != nil {
writeError(w, r, http.StatusInternalServerError, "failed to save recipes")
return
}
planID, upsertError := h.repo.UpsertItemsInTx(r.Context(), userID, weekStart, planItems)
requestLang := locale.FromContext(r.Context())
if requestLang != "en" {
h.saveDishTranslations(r.Context(), savedEntries, requestLang)
}
planID, upsertError := h.repo.UpsertItemsInTx(r.Context(), userID, weekStart, planItemsFrom(savedEntries))
if upsertError != nil {
slog.ErrorContext(r.Context(), "upsert menu items", "week", weekStart, "err", upsertError)
writeError(w, r, http.StatusInternalServerError, "failed to save menu plan")
@@ -310,24 +336,69 @@ func (h *Handler) fetchImages(ctx context.Context, days []ai.DayPlan) {
}
}
// saveRecipes persists all recipes as dish+recipe rows and returns a PlanItem list.
func (h *Handler) saveRecipes(ctx context.Context, days []ai.DayPlan) ([]PlanItem, error) {
planItems := make([]PlanItem, 0, len(days)*6)
// savedRecipeEntry pairs a persisted dish ID and plan item with the original English recipe.
type savedRecipeEntry struct {
DishID string
Recipe ai.Recipe
PlanItem PlanItem
}
// saveRecipes persists all recipes as dish+recipe rows and returns plan items
// alongside dish IDs needed for translation upserts.
func (h *Handler) saveRecipes(ctx context.Context, days []ai.DayPlan) ([]savedRecipeEntry, error) {
entries := make([]savedRecipeEntry, 0, len(days)*6)
for _, day := range days {
for _, meal := range day.Meals {
recipeID, createError := h.recipeSaver.Create(ctx, recipeToCreateRequest(meal.Recipe))
dishID, recipeID, createError := h.recipeSaver.Create(ctx, recipeToCreateRequest(meal.Recipe))
if createError != nil {
slog.ErrorContext(ctx, "save recipe for menu", "title", meal.Recipe.Title, "err", createError)
return nil, createError
}
planItems = append(planItems, PlanItem{
DayOfWeek: day.Day,
MealType: meal.MealType,
RecipeID: recipeID,
entries = append(entries, savedRecipeEntry{
DishID: dishID,
Recipe: meal.Recipe,
PlanItem: PlanItem{
DayOfWeek: day.Day,
MealType: meal.MealType,
RecipeID: recipeID,
},
})
}
}
return planItems, nil
return entries, nil
}
// planItemsFrom extracts the PlanItem slice from savedRecipeEntries.
func planItemsFrom(entries []savedRecipeEntry) []PlanItem {
items := make([]PlanItem, len(entries))
for i, entry := range entries {
items[i] = entry.PlanItem
}
return items
}
// saveDishTranslations translates recipe titles and upserts them into dish_translations.
// Errors are logged but do not fail the request (translations are best-effort).
func (h *Handler) saveDishTranslations(ctx context.Context, entries []savedRecipeEntry, lang string) {
recipes := make([]ai.Recipe, len(entries))
for i, entry := range entries {
recipes[i] = entry.Recipe
}
translated, translateError := h.translator.TranslateRecipes(ctx, recipes, lang)
if translateError != nil {
slog.WarnContext(ctx, "translate menu dish names", "lang", lang, "err", translateError)
return
}
for i, entry := range entries {
if i >= len(translated) {
break
}
if upsertError := h.dishTranslator.UpsertTranslation(ctx, entry.DishID, lang, translated[i].Title); upsertError != nil {
slog.WarnContext(ctx, "upsert dish translation", "dish_id", entry.DishID, "lang", lang, "err", upsertError)
}
}
}
// groupDatesByWeek groups YYYY-MM-DD date strings by their ISO week's Monday date.