fastapi-babel icon indicating copy to clipboard operation
fastapi-babel copied to clipboard

Lazy gettext with FastAPI response_model

Open phamvanvuhb1999 opened this issue 10 months ago • 1 comments

Hi, I'm facing some errors related to lazy gettext with FastAPI response_model, It seem like lazytext not be evaluated correctly. ` error_message = _("Some error message")

@app.get("/multilingual/template", response_class=JSONResponse) async def multilingual_template(request: Request): raise HTTPException(status_code=400, detail=error_message)

@app.get("/multilingual/template", response_class=JSONResponse) async def multilingual_template(request: Request): return {"data": error_message} ` then both 2 endpoints raises "Lazytext is not Jsonable".

As I know, lazytext be evaluated (translated to string) when it needed, But how to force to do the translation in the view itself.

phamvanvuhb1999 avatar Mar 11 '25 02:03 phamvanvuhb1999

@phamvanvuhb1999

I will fix this issue by forcing the lazy gettext object to be evaluated as a string (using str()) before returning it in FastAPI responses or exceptions.

Legopapurida avatar Aug 23 '25 05:08 Legopapurida