From cba50489bea8d0f0e64de8805a38b5d52335d085 Mon Sep 17 00:00:00 2001 From: dbastrikin Date: Mon, 23 Mar 2026 16:42:57 +0200 Subject: [PATCH] fix: generate recipes in the user's language, not always English MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit buildRecipePrompt ignored req.Lang entirely and hardcoded two English instructions in the prompt. Added targetLang resolution using the existing langNames map (from recognition.go) and threaded it into both "Generate N recipes in …" and "All text fields … MUST be in …" instructions. The image_query field remains English since it is passed to the Pexels photo API. Fixes recommendations and menu recipes being returned in English regardless of the Accept-Language header. Co-Authored-By: Claude Sonnet 4.6 --- backend/internal/adapters/openai/recipe.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/internal/adapters/openai/recipe.go b/backend/internal/adapters/openai/recipe.go index 3ac61ab..989bbf1 100644 --- a/backend/internal/adapters/openai/recipe.go +++ b/backend/internal/adapters/openai/recipe.go @@ -16,6 +16,7 @@ var goalNames = map[string]string{ "gain": "muscle gain", } + // GenerateRecipes generates recipes using the OpenAI API. // Retries up to maxRetries times only when the response is not valid JSON. // API-level errors (rate limits, auth, etc.) are returned immediately. @@ -61,6 +62,11 @@ func buildRecipePrompt(req ai.RecipeRequest) string { goal = "weight maintenance" } + targetLang := langNames[req.Lang] + if targetLang == "" { + targetLang = "English" + } + restrictions := "none" if len(req.Restrictions) > 0 { restrictions = strings.Join(req.Restrictions, ", ") @@ -87,7 +93,7 @@ func buildRecipePrompt(req ai.RecipeRequest) string { strings.Join(req.AvailableProducts, "\n") + "\n" } - return fmt.Sprintf(`You are a chef and nutritionist. Generate %d recipes in English. + return fmt.Sprintf(`You are a chef and nutritionist. Generate %d recipes in %s. User profile: - Goal: %s @@ -101,7 +107,7 @@ Requirements for each recipe: - Include approximate macros per serving IMPORTANT: -- All text fields (title, description, ingredient names, units, step descriptions, tags) MUST be in English. +- All text fields (title, description, ingredient names, units, step descriptions, tags) MUST be in %s. - The "image_query" field MUST always be in English (it is used for stock-photo search). Return ONLY a valid JSON array without markdown or extra text: @@ -118,7 +124,7 @@ Return ONLY a valid JSON array without markdown or extra text: "steps": [{"number": 1, "description": "...", "timer_seconds": null}], "tags": ["..."], "nutrition_per_serving": {"calories": 420, "protein_g": 48, "fat_g": 12, "carbs_g": 18} -}]`, count, goal, req.DailyCalories, restrictions, cuisines, productsSection, perMealCalories) +}]`, count, targetLang, goal, req.DailyCalories, restrictions, cuisines, productsSection, perMealCalories, targetLang) } func parseRecipesJSON(text string) ([]ai.Recipe, error) {