Replies: 1 comment 3 replies
-
Seems it works out of the box: from starlette.applications import Starlette
from starlette.responses import Response
from starlette.routing import Mount, Route
from starlette.middleware import Middleware
def index(request):
return Response(request.state.lang) # get value set in the middleware
class LangMiddleware:
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
language_from_path = scope['path_params']['lang']
scope.setdefault('state', {})
scope['state']['lang'] = language_from_path # make it available as request.state.lang
# activate_translation(language_from_path) # activate translations
await self.app(scope, receive, send)
app = Starlette(
debug=True,
routes=[
Mount(
'/{lang}',
routes=[Route('/login', index)],
middleware=[Middleware(LangMiddleware)],
)
],
) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi
I'd like to reailize i18n language prefix to url (like in Django) solution.
That is every url will have a language prefix like "/en/auth/user", "/fr/auth/user".
It could be done perfect with double Mount, which is not mentioned in the documentation, but tested.
That is the top level Mount("/{language:user_language}") determines language,
and the lower Mounts includes routes from every library added to the project.
And it works! But the idea is to add a middleware which could take a param_path["language'] and activate translation language somehow.
But a middleware doesn't see param_paths as, according to @tomchristie "..the routing only occurs after the middleware handling."
Could anybody help me with the idea, how to do it nice way, please?
Igor
Beta Was this translation helpful? Give feedback.
All reactions