Skip to content

Commit

Permalink
Moved set_lang to settings app
Browse files Browse the repository at this point in the history
  • Loading branch information
cofob committed Jan 2, 2022
1 parent ba3b005 commit 088afde
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
1 change: 0 additions & 1 deletion index/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
urlpatterns = [
path("", views.index, name="index"),
path("statistics", views.stats, name="stats"),
path("set_lang/<lang_code>", views.set_lang, name="set_lang"),
]
23 changes: 0 additions & 23 deletions index/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
from hashlib import sha256
from json import dumps, loads
from binascii import unhexlify
from django.http.response import JsonResponse
from django.utils import translation
from django.conf import settings


def index(request):
Expand Down Expand Up @@ -106,23 +103,3 @@ def stats(request):
"description": "View some interesting stats",
},
)


def set_lang(request, lang_code):
resp = {}
lang_code = lang_code.lower()
if len(lang_code) != 2:
return JsonResponse(
{"ok": False, "error": "Language code can be only 2 characters length."}
)
if lang_code not in settings.LANGUAGE_CODES:
lang = "en"
resp["warn"] = "Language not found, using default."
else:
lang = lang_code
translation.activate(lang)
resp["ok"] = True
resp["lang"] = lang
response = JsonResponse(resp)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang)
return response
1 change: 1 addition & 0 deletions settings_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

urlpatterns = [
path("", views.index, name="settings"),
path("set_lang/<lang_code>", views.set_lang, name="set_lang"),
]
23 changes: 23 additions & 0 deletions settings_app/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
from django.shortcuts import render
from django.http.response import JsonResponse
from django.utils import translation
from django.conf import settings


def index(request):
return render(request, "settings/index.html")


def set_lang(request, lang_code):
resp = {}
lang_code = lang_code.lower()
if len(lang_code) != 2:
return JsonResponse(
{"ok": False, "error": "Language code can be only 2 characters length."}
)
if lang_code not in settings.LANGUAGE_CODES:
lang = "en"
resp["warn"] = "Language not found, using default."
else:
lang = lang_code
translation.activate(lang)
resp["ok"] = True
resp["lang"] = lang
response = JsonResponse(resp)
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang)
return response
2 changes: 1 addition & 1 deletion static/js/all.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 088afde

Please sign in to comment.