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:
69
backend/internal/locale/locale.go
Normal file
69
backend/internal/locale/locale.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package locale
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Default is the fallback language when no supported language is detected.
|
||||
const Default = "en"
|
||||
|
||||
// Supported is the set of language codes the application currently handles.
|
||||
// Keys are ISO 639-1 two-letter codes (lower-case).
|
||||
var Supported = map[string]bool{
|
||||
"en": true,
|
||||
"ru": true,
|
||||
"es": true,
|
||||
"de": true,
|
||||
"fr": true,
|
||||
"it": true,
|
||||
"pt": true,
|
||||
"zh": true,
|
||||
"ja": true,
|
||||
"ko": true,
|
||||
"ar": true,
|
||||
"hi": true,
|
||||
}
|
||||
|
||||
type contextKey struct{}
|
||||
|
||||
// Parse returns the best-matching supported language from an Accept-Language
|
||||
// header value. It iterates through the comma-separated list in preference
|
||||
// order and returns the first entry whose primary subtag is in Supported.
|
||||
// Returns Default when the header is empty or no match is found.
|
||||
func Parse(acceptLang string) string {
|
||||
if acceptLang == "" {
|
||||
return Default
|
||||
}
|
||||
for part := range strings.SplitSeq(acceptLang, ",") {
|
||||
// Strip quality value (e.g. ";q=0.9").
|
||||
tag := strings.TrimSpace(strings.SplitN(part, ";", 2)[0])
|
||||
// Use only the primary subtag (e.g. "ru" from "ru-RU").
|
||||
lang := strings.ToLower(strings.SplitN(tag, "-", 2)[0])
|
||||
if Supported[lang] {
|
||||
return lang
|
||||
}
|
||||
}
|
||||
return Default
|
||||
}
|
||||
|
||||
// WithLang returns a copy of ctx carrying the given language code.
|
||||
func WithLang(ctx context.Context, lang string) context.Context {
|
||||
return context.WithValue(ctx, contextKey{}, lang)
|
||||
}
|
||||
|
||||
// FromContext returns the language stored in ctx.
|
||||
// Returns Default when no language has been set.
|
||||
func FromContext(ctx context.Context) string {
|
||||
if lang, ok := ctx.Value(contextKey{}).(string); ok && lang != "" {
|
||||
return lang
|
||||
}
|
||||
return Default
|
||||
}
|
||||
|
||||
// FromRequest extracts the preferred language from the request's
|
||||
// Accept-Language header.
|
||||
func FromRequest(r *http.Request) string {
|
||||
return Parse(r.Header.Get("Accept-Language"))
|
||||
}
|
||||
Reference in New Issue
Block a user