- 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>
95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
package locale_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/food-ai/backend/internal/locale"
|
|
)
|
|
|
|
func TestParse(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
acceptLang string
|
|
want string
|
|
}{
|
|
{
|
|
name: "empty header returns default",
|
|
acceptLang: "",
|
|
want: locale.Default,
|
|
},
|
|
{
|
|
name: "exact match",
|
|
acceptLang: "ru",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "region subtag stripped",
|
|
acceptLang: "ru-RU",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "full browser header picks first supported",
|
|
acceptLang: "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "unsupported first falls through to supported",
|
|
acceptLang: "xx-XX,ru;q=0.8",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "completely unsupported returns default",
|
|
acceptLang: "xx-XX,yy-YY",
|
|
want: locale.Default,
|
|
},
|
|
{
|
|
name: "chinese region subtag",
|
|
acceptLang: "zh-CN,zh;q=0.9",
|
|
want: "zh",
|
|
},
|
|
{
|
|
name: "case insensitive",
|
|
acceptLang: "RU-RU",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "whitespace around tag",
|
|
acceptLang: " ru ",
|
|
want: "ru",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := locale.Parse(tc.acceptLang)
|
|
if got != tc.want {
|
|
t.Errorf("Parse(%q) = %q, want %q", tc.acceptLang, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWithLangAndFromContext(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
if got := locale.FromContext(ctx); got != locale.Default {
|
|
t.Errorf("FromContext on empty ctx = %q, want %q", got, locale.Default)
|
|
}
|
|
|
|
ctx = locale.WithLang(ctx, "ru")
|
|
if got := locale.FromContext(ctx); got != "ru" {
|
|
t.Errorf("FromContext after WithLang(ru) = %q, want %q", got, "ru")
|
|
}
|
|
}
|
|
|
|
func TestFromRequest(t *testing.T) {
|
|
req, _ := http.NewRequest(http.MethodGet, "/", nil)
|
|
req.Header.Set("Accept-Language", "es-ES,es;q=0.9")
|
|
|
|
if got := locale.FromRequest(req); got != "es" {
|
|
t.Errorf("FromRequest = %q, want %q", got, "es")
|
|
}
|
|
}
|