'fastapi-localization not translationg my api response
I have installed and implemented fastapi-localization just like the documentation says in pypi.org documentation page of fastapi-localization here.But it is not translating the response accordingly. Can anyone please suggest me how I can fix this or any other better way to translate API response in various languages? Thanks in advance.
My code is exactly like the documentation says-
from fastapi_localization import TranslateJsonResponse
from fastapi_localization import TranslatableStringField
from typing import List
from pydantic import BaseModel
class LanguageTranslatableSchema(BaseModel):
code: str
title: TranslatableStringField
@pr.get(
'/language',
response_class=TranslateJsonResponse,
response_model=List[LanguageTranslatableSchema],
status_code=status.HTTP_200_OK
)
async def languages():
# return "hello"
return [{'code': 'ru', 'title': 'Russia'},
{'code': 'en', 'title': 'English'}]
My curl while sending request is:
curl --request GET \
--url http://localhost:8888/api/v1/public/language \
--header 'Accept-Language: ru'
I should be getting the response like this:
[{"code":"ru","title":"Русский"},{"code":"en","title":"Английский"}]
Instead I am getting a not translated response like this:
[{"code":"ru","title":"Russia"},{"code":"en","title":"English"}]
Solution 1:[1]
It is not that clear in their documentation, but you would need to add the middleware, exception handlers, etc., that take care of the translation , as demonstrated in the example provided here, and as shown below. In addiition, you would need to provide translations, as shown here.
from typing import List
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from pydantic import BaseSettings, BaseModel
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.exceptions import HTTPException as StarletteHTTPException
from pydantic import (
EmailStr,
)
from fastapi_localization import (
SystemLocalizationMiddleware,
http_exception_handler,
validation_exception_handler,
LocalizationRoute,
TranslatableStringField,
TranslateJsonResponse,
)
from fastapi_localization import lazy_gettext as _
class Settings(BaseSettings):
localization_dir: str = 'locales'
localization_domain: str = 'base'
settings = Settings()
app = FastAPI()
# register localization middleware
localization_middleware = SystemLocalizationMiddleware(
domain=settings.localization_domain,
translation_dir=settings.localization_dir,
)
app.add_middleware(BaseHTTPMiddleware, dispatch=localization_middleware)
# register error handlers for localization errors
app.add_exception_handler(StarletteHTTPException, http_exception_handler)
app.add_exception_handler(RequestValidationError, validation_exception_handler)
app.router.route_class = LocalizationRoute
class LanguageTranslatableSchema(BaseModel):
code: str
title: TranslatableStringField
@app.get(
'/language',
response_class=TranslateJsonResponse,
response_model=List[LanguageTranslatableSchema])
async def languages():
return [{'code': 'ru', 'title': 'Russia'},
{'code': 'en', 'title': 'English'}]
@app.get(
'/country',
response_class=TranslateJsonResponse)
async def countries():
return [{'code': 'ru', 'title': _('Russia')},
{'code': 'us', 'title': 'USA'}]
class InputSchema(BaseModel):
email = EmailStr()
@app.post(
'/input',
response_class=TranslateJsonResponse)
async def countries(value: InputSchema):
return value
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
