Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 183 additions & 66 deletions back/app/inventory_routes.py

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions back/app/language_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Language service for normalizing language codes and managing translations.
"""

from fastapi import Query, Header

SUPPORTED_LANGUAGES = [
"en", # English
"es", # Spanish
Expand Down Expand Up @@ -50,3 +52,38 @@ def normalize_language_code(lang: str) -> str | None:
def get_supported_languages() -> list[str]:
"""Return list of supported language codes."""
return SUPPORTED_LANGUAGES.copy()


def get_requested_language(
lang: str | None = Query(None, description="Language code (e.g. en, es, zh-CN)"),
accept_language: str | None = Header(
None, alias="Accept-Language", description="Accept-Language header"
),
) -> str:
"""
Determine the requested language based on query param or Accept-Language header.
Returns normalized language code or 'en' as fallback.
"""
# 1. Check explicit lang query parameter
if lang:
normalized = normalize_language_code(lang)
if normalized:
return normalized

# 2. Parse Accept-Language header
if accept_language:
# Simple parsing - take the first language with highest quality
# Format: "en-US,en;q=0.9,es;q=0.8"
languages = []
for part in accept_language.split(","):
lang_part = part.strip().split(";")[0].strip()
if lang_part:
normalized = normalize_language_code(lang_part)
if normalized:
languages.append(normalized)

if languages:
return languages[0] # Return highest priority

# 3. Fallback to English
return "en"
Loading