From e1fbe7b1a2452f5dd2ebc936a62f9d8ea04b6b54 Mon Sep 17 00:00:00 2001 From: dbastrikin Date: Tue, 10 Mar 2026 23:08:10 +0200 Subject: [PATCH] feat: move supported languages to DB table, expose via GET /languages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - migration 013: create languages table (code PK, native_name, english_name, is_active, sort_order) with all 12 existing languages seeded - locale: add Language struct, Languages []Language, LoadFromDB() — queries languages table at startup and populates both Supported map and Languages slice; existing Parse/FromContext/FromRequest unchanged - main.go: call locale.LoadFromDB after pool is ready - gemini/recipe.go: remove hardcoded langNames map, use locale.Languages linear lookup for English name in prompt - language/handler.go: new package with GET /languages handler returning active languages list (no auth required) - server.go: register GET /languages as public route - Flutter: add LanguageRepository + languageRepositoryProvider that fetches /languages from backend - language_provider.dart: replace const supportedLanguages map with supportedLanguagesProvider (FutureProvider) backed by LanguageRepository - profile_provider.dart: remove supportedLanguages.containsKey validation — backend is source of truth; sync any non-empty language from preferences - profile_screen.dart: use supportedLanguagesProvider for display name and dropdown (async with loading/error states) Co-Authored-By: Claude Sonnet 4.6 --- backend/cmd/server/main.go | 6 +++ backend/internal/gemini/recipe.go | 27 +++------- backend/internal/language/handler.go | 28 ++++++++++ backend/internal/locale/locale.go | 53 +++++++++++++++---- backend/internal/server/server.go | 2 + backend/migrations/013_create_languages.sql | 24 +++++++++ client/lib/core/api/language_repository.dart | 22 ++++++++ client/lib/core/locale/language_provider.dart | 23 +++----- .../features/profile/profile_provider.dart | 3 +- .../lib/features/profile/profile_screen.dart | 31 ++++++----- 10 files changed, 161 insertions(+), 58 deletions(-) create mode 100644 backend/internal/language/handler.go create mode 100644 backend/migrations/013_create_languages.sql create mode 100644 client/lib/core/api/language_repository.dart diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index 90db562..5462ef7 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -17,6 +17,7 @@ import ( "github.com/food-ai/backend/internal/gemini" "github.com/food-ai/backend/internal/home" "github.com/food-ai/backend/internal/ingredient" + "github.com/food-ai/backend/internal/locale" "github.com/food-ai/backend/internal/menu" "github.com/food-ai/backend/internal/middleware" "github.com/food-ai/backend/internal/pexels" @@ -72,6 +73,11 @@ func run() error { defer pool.Close() slog.Info("connected to database") + if err := locale.LoadFromDB(ctx, pool); err != nil { + return fmt.Errorf("load languages: %w", err) + } + slog.Info("languages loaded", "count", len(locale.Languages)) + // Firebase auth firebaseAuth, err := auth.NewFirebaseAuthOrNoop(cfg.FirebaseCredentialsFile) if err != nil { diff --git a/backend/internal/gemini/recipe.go b/backend/internal/gemini/recipe.go index f8a0baf..8a6e299 100644 --- a/backend/internal/gemini/recipe.go +++ b/backend/internal/gemini/recipe.go @@ -5,6 +5,8 @@ import ( "encoding/json" "fmt" "strings" + + "github.com/food-ai/backend/internal/locale" ) // RecipeGenerator generates recipes using the Gemini AI. @@ -63,22 +65,6 @@ type NutritionInfo struct { Approximate bool `json:"approximate"` } -// langNames maps ISO 639-1 codes to English language names used in the prompt. -var langNames = map[string]string{ - "en": "English", - "ru": "Russian", - "es": "Spanish", - "de": "German", - "fr": "French", - "it": "Italian", - "pt": "Portuguese", - "zh": "Chinese (Simplified)", - "ja": "Japanese", - "ko": "Korean", - "ar": "Arabic", - "hi": "Hindi", -} - // goalNames maps internal goal codes to English descriptions used in the prompt. var goalNames = map[string]string{ "lose": "weight loss", @@ -130,9 +116,12 @@ func buildRecipePrompt(req RecipeRequest) string { if lang == "" { lang = "en" } - langName, ok := langNames[lang] - if !ok { - langName = "English" + langName := "English" + for _, l := range locale.Languages { + if l.Code == lang { + langName = l.EnglishName + break + } } goal := goalNames[req.UserGoal] diff --git a/backend/internal/language/handler.go b/backend/internal/language/handler.go new file mode 100644 index 0000000..02ffa09 --- /dev/null +++ b/backend/internal/language/handler.go @@ -0,0 +1,28 @@ +package language + +import ( + "encoding/json" + "net/http" + + "github.com/food-ai/backend/internal/locale" +) + +type languageItem struct { + Code string `json:"code"` + NativeName string `json:"native_name"` + SortOrder int `json:"sort_order"` +} + +// List handles GET /languages — returns the active language list loaded from DB. +func List(w http.ResponseWriter, r *http.Request) { + items := make([]languageItem, 0, len(locale.Languages)) + for i, l := range locale.Languages { + items = append(items, languageItem{ + Code: l.Code, + NativeName: l.NativeName, + SortOrder: i + 1, + }) + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{"languages": items}) +} diff --git a/backend/internal/locale/locale.go b/backend/internal/locale/locale.go index 69f43fc..4a1e087 100644 --- a/backend/internal/locale/locale.go +++ b/backend/internal/locale/locale.go @@ -2,8 +2,11 @@ package locale import ( "context" + "fmt" "net/http" "strings" + + "github.com/jackc/pgx/v5/pgxpool" ) // Default is the fallback language when no supported language is detected. @@ -11,19 +14,49 @@ const Default = "en" // Supported is the set of language codes the application currently handles. // Keys are ISO 639-1 two-letter codes (lower-case). +// Populated by LoadFromDB at server startup. 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, +} + +// Language is a supported language record loaded from the DB. +type Language struct { + Code string + NativeName string + EnglishName string +} + +// Languages is the ordered list of active languages. +// Populated by LoadFromDB at server startup. +var Languages []Language + +// LoadFromDB queries the languages table and updates both Supported and Languages. +// Must be called once at startup before the server begins accepting requests. +func LoadFromDB(ctx context.Context, pool *pgxpool.Pool) error { + rows, err := pool.Query(ctx, + `SELECT code, native_name, english_name FROM languages WHERE is_active ORDER BY sort_order`) + if err != nil { + return fmt.Errorf("load languages from db: %w", err) + } + defer rows.Close() + + newSupported := map[string]bool{} + var newLanguages []Language + for rows.Next() { + var l Language + if err := rows.Scan(&l.Code, &l.NativeName, &l.EnglishName); err != nil { + return err + } + newSupported[l.Code] = true + newLanguages = append(newLanguages, l) + } + if err := rows.Err(); err != nil { + return err + } + Supported = newSupported + Languages = newLanguages + return nil } type contextKey struct{} diff --git a/backend/internal/server/server.go b/backend/internal/server/server.go index eb0f443..081d75a 100644 --- a/backend/internal/server/server.go +++ b/backend/internal/server/server.go @@ -8,6 +8,7 @@ import ( "github.com/food-ai/backend/internal/diary" "github.com/food-ai/backend/internal/home" "github.com/food-ai/backend/internal/ingredient" + "github.com/food-ai/backend/internal/language" "github.com/food-ai/backend/internal/menu" "github.com/food-ai/backend/internal/middleware" "github.com/food-ai/backend/internal/product" @@ -45,6 +46,7 @@ func NewRouter( // Public r.Get("/health", healthCheck(pool)) + r.Get("/languages", language.List) r.Route("/auth", func(r chi.Router) { r.Post("/login", authHandler.Login) r.Post("/refresh", authHandler.Refresh) diff --git a/backend/migrations/013_create_languages.sql b/backend/migrations/013_create_languages.sql new file mode 100644 index 0000000..82e2350 --- /dev/null +++ b/backend/migrations/013_create_languages.sql @@ -0,0 +1,24 @@ +-- +goose Up +CREATE TABLE languages ( + code VARCHAR(10) PRIMARY KEY, + native_name TEXT NOT NULL, + english_name TEXT NOT NULL, + is_active BOOLEAN NOT NULL DEFAULT true, + sort_order SMALLINT NOT NULL DEFAULT 0 +); +INSERT INTO languages (code, native_name, english_name, sort_order) VALUES + ('en', 'English', 'English', 1), + ('ru', 'Русский', 'Russian', 2), + ('es', 'Español', 'Spanish', 3), + ('de', 'Deutsch', 'German', 4), + ('fr', 'Français', 'French', 5), + ('it', 'Italiano', 'Italian', 6), + ('pt', 'Português', 'Portuguese', 7), + ('zh', '中文', 'Chinese (Simplified)', 8), + ('ja', '日本語', 'Japanese', 9), + ('ko', '한국어', 'Korean', 10), + ('ar', 'العربية', 'Arabic', 11), + ('hi', 'हिन्दी', 'Hindi', 12); + +-- +goose Down +DROP TABLE IF EXISTS languages; diff --git a/client/lib/core/api/language_repository.dart b/client/lib/core/api/language_repository.dart new file mode 100644 index 0000000..288cfd7 --- /dev/null +++ b/client/lib/core/api/language_repository.dart @@ -0,0 +1,22 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../auth/auth_provider.dart'; +import 'api_client.dart'; + +class LanguageRepository { + final ApiClient _api; + LanguageRepository(this._api); + + Future> fetchLanguages() async { + final response = await _api.dio.get('/languages'); + final List items = response.data['languages'] as List; + return { + for (final item in items) + item['code'] as String: item['native_name'] as String, + }; + } +} + +final languageRepositoryProvider = Provider( + (ref) => LanguageRepository(ref.watch(apiClientProvider)), +); diff --git a/client/lib/core/locale/language_provider.dart b/client/lib/core/locale/language_provider.dart index 5d4d757..22b4005 100644 --- a/client/lib/core/locale/language_provider.dart +++ b/client/lib/core/locale/language_provider.dart @@ -1,21 +1,12 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; -/// Supported ISO 639-1 language codes with their native names. -/// Must match the backend locale.Supported map. -const supportedLanguages = { - 'en': 'English', - 'ru': 'Русский', - 'es': 'Español', - 'de': 'Deutsch', - 'fr': 'Français', - 'it': 'Italiano', - 'pt': 'Português', - 'zh': '中文', - 'ja': '日本語', - 'ko': '한국어', - 'ar': 'العربية', - 'hi': 'हिन्दी', -}; +import '../api/language_repository.dart'; + +/// Fetches and caches the supported languages from the backend. +/// Returns a map of code → native name (e.g. {'en': 'English', 'ru': 'Русский'}). +final supportedLanguagesProvider = FutureProvider>((ref) { + return ref.watch(languageRepositoryProvider).fetchLanguages(); +}); /// Current app language (ISO 639-1 code). /// Synced from user.preferences['language'] after the profile loads or is updated. diff --git a/client/lib/features/profile/profile_provider.dart b/client/lib/features/profile/profile_provider.dart index 1d60a77..f2bbbc1 100644 --- a/client/lib/features/profile/profile_provider.dart +++ b/client/lib/features/profile/profile_provider.dart @@ -4,6 +4,7 @@ import '../../core/locale/language_provider.dart'; import '../../shared/models/user.dart'; import 'profile_service.dart'; + class ProfileNotifier extends StateNotifier> { final ProfileService _service; final void Function(String) _setLanguage; @@ -33,7 +34,7 @@ class ProfileNotifier extends StateNotifier> { // Propagates the user's preferred language to the global languageProvider. void _syncLanguage() { final lang = state.valueOrNull?.preferences['language'] as String?; - if (lang != null && supportedLanguages.containsKey(lang)) { + if (lang != null && lang.isNotEmpty) { _setLanguage(lang); } } diff --git a/client/lib/features/profile/profile_screen.dart b/client/lib/features/profile/profile_screen.dart index 1a62ffe..9a3dc58 100644 --- a/client/lib/features/profile/profile_screen.dart +++ b/client/lib/features/profile/profile_screen.dart @@ -123,7 +123,8 @@ class _ProfileBody extends StatelessWidget { _InfoCard(children: [ _InfoRow( 'Язык', - supportedLanguages[user.preferences['language'] as String? ?? 'ru'] ?? + ref.watch(supportedLanguagesProvider).valueOrNull?[ + user.preferences['language'] as String? ?? 'ru'] ?? 'Русский', ), ]), @@ -590,17 +591,23 @@ class _EditProfileSheetState extends ConsumerState { const SizedBox(height: 20), // Language - DropdownButtonFormField( - value: _language, - decoration: - const InputDecoration(labelText: 'Язык интерфейса'), - items: supportedLanguages.entries - .map((e) => DropdownMenuItem( - value: e.key, - child: Text(e.value), - )) - .toList(), - onChanged: (v) => setState(() => _language = v), + ref.watch(supportedLanguagesProvider).when( + data: (languages) => DropdownButtonFormField( + value: _language, + decoration: const InputDecoration( + labelText: 'Язык интерфейса'), + items: languages.entries + .map((e) => DropdownMenuItem( + value: e.key, + child: Text(e.value), + )) + .toList(), + onChanged: (v) => setState(() => _language = v), + ), + loading: () => const Center( + child: CircularProgressIndicator()), + error: (_, __) => + const Text('Не удалось загрузить языки'), ), const SizedBox(height: 32),