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

@@ -40,18 +40,25 @@ type RecipeGenerator interface {
GenerateRecipes(ctx context.Context, req ai.RecipeRequest) ([]ai.Recipe, error)
}
// RecipeTranslator translates a slice of English recipes into a target language.
type RecipeTranslator interface {
TranslateRecipes(ctx context.Context, recipes []ai.Recipe, targetLang string) ([]ai.Recipe, error)
}
// Handler handles GET /recommendations.
type Handler struct {
recipeGenerator RecipeGenerator
translator RecipeTranslator
pexels PhotoSearcher
userLoader UserLoader
productLister ProductLister
}
// NewHandler creates a new Handler.
func NewHandler(recipeGenerator RecipeGenerator, pexels PhotoSearcher, userLoader UserLoader, productLister ProductLister) *Handler {
func NewHandler(recipeGenerator RecipeGenerator, translator RecipeTranslator, pexels PhotoSearcher, userLoader UserLoader, productLister ProductLister) *Handler {
return &Handler{
recipeGenerator: recipeGenerator,
translator: translator,
pexels: pexels,
userLoader: userLoader,
productLister: productLister,
@@ -111,6 +118,20 @@ func (h *Handler) GetRecommendations(w http.ResponseWriter, r *http.Request) {
}
wg.Wait()
// Translate text fields into the requested language.
// The generation prompt always produces English; translations are applied
// in-memory here since recommendations are not persisted to the database.
lang := locale.FromContext(r.Context())
if lang != "en" {
translated, translateError := h.translator.TranslateRecipes(r.Context(), recipes, lang)
if translateError != nil {
slog.WarnContext(r.Context(), "translate recommendations", "lang", lang, "err", translateError)
// Fall back to English rather than failing the request.
} else {
recipes = translated
}
}
writeJSON(w, http.StatusOK, recipes)
}