Created
February 20, 2021 00:38
-
-
Save rririanto/03268031f6822386530633a965e9ff5d to your computer and use it in GitHub Desktop.
Set Translation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urls.py | |
============================ | |
from django.urls import path | |
# Your path of views | |
from apps.main.views import main, set_language_from_url | |
urlpatterns = [ | |
... | |
path("set_language/<str:user_language>/", set_language_from_url, name="set_language_from_url") | |
] | |
views.py | |
============================ | |
def set_language_from_url(request, user_language): | |
translation.activate(user_language) | |
request.session[translation.LANGUAGE_SESSION_KEY] = user_language | |
# I use HTTP_REFERER to direct them back to previous path | |
return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
file.html | |
============================ | |
<a href="{% url 'main:set_language_from_url' 'en' %}"><img src="{% static 'assets/img/gis/en.png' %}" alt="en language" /></a> | |
<a href="{% url 'main:set_language_from_url' 'vi' %}"><img src="{% static 'assets/img/gis/vi.png' %}" alt="vietnames language" /></a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment