feat: implement backend localization infrastructure

- Add internal/locale package: Parse(Accept-Language), FromContext/WithLang helpers, 12 supported languages
- Add Language middleware that reads Accept-Language header and stores lang in context
- Register Language middleware globally in server router (after CORS)

Database migrations:
- 009: create recipe_translations, saved_recipe_translations, ingredient_translations tables; migrate existing _ru data
- 010: drop legacy _ru columns (title_ru, description_ru, canonical_name_ru); update FTS index

Models: remove all _ru fields (TitleRu, DescriptionRu, NameRu, UnitRu, CanonicalNameRu)

Repositories:
- recipe: Upsert drops _ru params; GetByID does LEFT JOIN COALESCE on recipe_translations; ListMissingTranslation(lang); UpsertTranslation
- ingredient: same pattern with ingredient_translations; Search now queries translated names/aliases
- savedrecipe: List/GetByID LEFT JOIN COALESCE on saved_recipe_translations; UpsertTranslation

Gemini:
- RecipeRequest/MenuRequest gain Lang field
- buildRecipePrompt rewritten in English with target-language content instruction; image_query always in English
- GenerateMenu propagates Lang to GenerateRecipes

Handlers:
- recommendation/menu: pass locale.FromContext(ctx) as Lang
- recognition: saveClassification stores Russian translation via UpsertTranslation instead of _ru column

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-02-27 23:17:34 +02:00
parent ea4a6301ea
commit c0cf1b38ea
18 changed files with 718 additions and 273 deletions

View File

@@ -7,6 +7,7 @@ import (
"encoding/json"
"testing"
"github.com/food-ai/backend/internal/locale"
"github.com/food-ai/backend/internal/testutil"
)
@@ -65,7 +66,6 @@ func TestIngredientRepository_Upsert_ConflictUpdates(t *testing.T) {
t.Fatalf("first upsert: %v", err)
}
// Update with same spoonacular_id
cal := 89.0
second := &IngredientMapping{
CanonicalName: "banana_updated",
@@ -137,7 +137,7 @@ func TestIngredientRepository_GetBySpoonacularID_NotFound(t *testing.T) {
}
}
func TestIngredientRepository_ListUntranslated(t *testing.T) {
func TestIngredientRepository_ListMissingTranslation(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := NewRepository(pool)
ctx := context.Background()
@@ -145,7 +145,7 @@ func TestIngredientRepository_ListUntranslated(t *testing.T) {
cat := "produce"
unit := "g"
// Insert 3 without translation
// Insert 3 without any translation.
for i, name := range []string{"carrot", "onion", "garlic"} {
id := 4000 + i
_, err := repo.Upsert(ctx, &IngredientMapping{
@@ -160,45 +160,38 @@ func TestIngredientRepository_ListUntranslated(t *testing.T) {
}
}
// Insert 1 with translation (shouldn't appear in untranslated list)
// Insert 1 and add a Russian translation should not appear in the result.
id := 4100
ruName := "помидор"
withTranslation := &IngredientMapping{
CanonicalName: "tomato",
CanonicalNameRu: &ruName,
SpoonacularID: &id,
Aliases: json.RawMessage(`[]`),
Category: &cat,
DefaultUnit: &unit,
}
saved, err := repo.Upsert(ctx, withTranslation)
saved, err := repo.Upsert(ctx, &IngredientMapping{
CanonicalName: "tomato",
SpoonacularID: &id,
Aliases: json.RawMessage(`[]`),
Category: &cat,
DefaultUnit: &unit,
})
if err != nil {
t.Fatalf("upsert with translation: %v", err)
t.Fatalf("upsert tomato: %v", err)
}
// The upsert doesn't set canonical_name_ru because the UPDATE clause doesn't include it
// We need to manually set it after
if err := repo.UpdateTranslation(ctx, saved.ID, "помидор", []string{"помидор", "томат"}); err != nil {
t.Fatalf("update translation: %v", err)
if err := repo.UpsertTranslation(ctx, saved.ID, "ru", "помидор", json.RawMessage(`["помидор","томат"]`)); err != nil {
t.Fatalf("upsert translation: %v", err)
}
untranslated, err := repo.ListUntranslated(ctx, 10, 0)
missing, err := repo.ListMissingTranslation(ctx, "ru", 10, 0)
if err != nil {
t.Fatalf("list untranslated: %v", err)
t.Fatalf("list missing translation: %v", err)
}
// Should return the 3 without translation (carrot, onion, garlic)
// The translated tomato should not appear
for _, m := range untranslated {
for _, m := range missing {
if m.CanonicalName == "tomato" {
t.Error("translated ingredient should not appear in ListUntranslated")
t.Error("translated ingredient should not appear in ListMissingTranslation")
}
}
if len(untranslated) < 3 {
t.Errorf("expected at least 3 untranslated, got %d", len(untranslated))
if len(missing) < 3 {
t.Errorf("expected at least 3 untranslated, got %d", len(missing))
}
}
func TestIngredientRepository_UpdateTranslation(t *testing.T) {
func TestIngredientRepository_UpsertTranslation(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := NewRepository(pool)
ctx := context.Background()
@@ -218,33 +211,29 @@ func TestIngredientRepository_UpdateTranslation(t *testing.T) {
t.Fatalf("upsert: %v", err)
}
err = repo.UpdateTranslation(ctx, saved.ID, "куриная грудка",
[]string{"куриная грудка", "куриное филе"})
err = repo.UpsertTranslation(ctx, saved.ID, "ru", "куриная грудка",
json.RawMessage(`["куриная грудка","куриное филе"]`))
if err != nil {
t.Fatalf("update translation: %v", err)
t.Fatalf("upsert translation: %v", err)
}
got, err := repo.GetByID(ctx, saved.ID)
// Retrieve with Russian context — CanonicalName should be the Russian name.
ruCtx := locale.WithLang(ctx, "ru")
got, err := repo.GetByID(ruCtx, saved.ID)
if err != nil {
t.Fatalf("get by id: %v", err)
}
if got.CanonicalNameRu == nil || *got.CanonicalNameRu != "куриная грудка" {
t.Errorf("expected canonical_name_ru='куриная грудка', got %v", got.CanonicalNameRu)
if got.CanonicalName != "куриная грудка" {
t.Errorf("expected CanonicalName='куриная грудка', got %q", got.CanonicalName)
}
var aliases []string
if err := json.Unmarshal(got.Aliases, &aliases); err != nil {
t.Fatalf("unmarshal aliases: %v", err)
// Retrieve with English context (default) — CanonicalName should be the English name.
enCtx := locale.WithLang(ctx, "en")
gotEn, err := repo.GetByID(enCtx, saved.ID)
if err != nil {
t.Fatalf("get by id (en): %v", err)
}
hasRu := false
for _, a := range aliases {
if a == "куриное филе" {
hasRu = true
break
}
}
if !hasRu {
t.Errorf("Russian alias not found in aliases: %v", aliases)
if gotEn.CanonicalName != "chicken_breast" {
t.Errorf("expected English CanonicalName='chicken_breast', got %q", gotEn.CanonicalName)
}
}