Skip to content

Commit

Permalink
Merge pull request #34 from Anbarryprojects/v1
Browse files Browse the repository at this point in the history
[FIX] dependency injection and examples has been fixed.
  • Loading branch information
Legopapurida authored Feb 25, 2024
2 parents 5a29366 + 3926b22 commit 882d146
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/with_jinja/babel.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[python: **.py]
[python: **.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_
12 changes: 11 additions & 1 deletion examples/with_jinja/full.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from contextvars import ContextVar
from typing import Annotated, Callable
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

from fastapi_babel import _ # noqa
from fastapi import Depends

# from fastapi_babel import _ # noqa
from fastapi_babel import Babel, BabelConfigs, BabelMiddleware
from fastapi_babel.core import make_gettext


app = FastAPI()
Expand All @@ -20,6 +25,11 @@
app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/")
async def index(_: Annotated[Callable[[str], str], Depends(make_gettext)]):
return {"text": _("Hello World")}


@app.get("/items/{id}", response_class=HTMLResponse)
async def read_item(request: Request, id: str):
return templates.TemplateResponse("item.html", {"request": request, "id": id})
Expand Down
4 changes: 2 additions & 2 deletions fastapi_babel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from subprocess import run
from typing import Callable, Optional

from fastapi import FastAPI, Request, Depends
from fastapi import Request
from fastapi.templating import Jinja2Templates

from .helpers import check_click_import, check_jinja_import
Expand Down Expand Up @@ -92,7 +92,7 @@ def __repr__(self) -> str:
return _(self.message)


def make_gettext(request: Request = Depends()) -> Callable[[str], str]:
def make_gettext(request: Request) -> Callable[[str], str]:
"""translate the message and retrieve message from .PO and .MO depends on
`Babel.locale` locale.
Expand Down

0 comments on commit 882d146

Please sign in to comment.