-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from CentreForDigitalHumanities/feature/i18n
Added localization and language switch
- Loading branch information
Showing
41 changed files
with
543 additions
and
186 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v14.21.2 | ||
v18.20.3 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GRANT ALL ON SCHEMA public to {{cookiecutter.database_user}}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
create user {{cookiecutter.database_user}} with createdb password '{{cookiecutter.database_password}}'; | ||
create database {{cookiecutter.database_name}}; | ||
grant all on database {{cookiecutter.database_name}} to {{cookiecutter.database_user}}; | ||
GRANT ALL ON SCHEMA public to {{cookiecutter.database_user}}; |
43 changes: 43 additions & 0 deletions
43
{{cookiecutter.slug}}/backend/{{cookiecutter.slug}}/common_settings.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
INSTALLED_APPS = [ | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'livereload', | ||
'django.contrib.staticfiles', | ||
'rest_framework', | ||
'revproxy', | ||
'example' | ||
] | ||
|
||
MIDDLEWARE = [ | ||
'django.middleware.security.SecurityMiddleware', | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.locale.LocaleMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
{% if cookiecutter.frontend == "backend" %} | ||
'livereload.middleware.LiveReloadScript', | ||
{% endif %} | ||
] | ||
|
||
# Internationalization | ||
# https://docs.djangoproject.com/en/3.0/topics/i18n/ | ||
LANGUAGES = [ | ||
{% set localizations = cookiecutter.localizations.split(',') %} | ||
{%- for loc in localizations %} | ||
{%- set code, name = loc.split(':') %} | ||
('{{code}}', '{{name}}'), | ||
{%- endfor %} | ||
] | ||
LANGUAGE_CODE = '{{cookiecutter.default_localization}}' | ||
|
||
TIME_ZONE = 'Europe/Amsterdam' | ||
|
||
USE_I18N = True | ||
|
||
USE_TZ = True |
19 changes: 19 additions & 0 deletions
19
{{cookiecutter.slug}}/backend/{{cookiecutter.slug}}/i18n.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from rest_framework.decorators import api_view | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
from django.conf import settings | ||
from django.utils import translation | ||
|
||
|
||
@api_view(["GET", "POST"]) | ||
def i18n(request: Request): | ||
response = Response() | ||
if request.method == "POST": | ||
language = request.data["language"] | ||
translation.activate(language) | ||
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language) | ||
else: | ||
language = request.LANGUAGE_CODE | ||
|
||
response.data = {"current": language, "supported": settings.LANGUAGES} | ||
return response |
18 changes: 13 additions & 5 deletions
18
{{cookiecutter.slug}}/backend/{{cookiecutter.slug}}/index.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
from django.http import Http404, HttpResponse | ||
from os import path | ||
from django.http import HttpRequest, HttpResponse | ||
from django.contrib.staticfiles import finders | ||
from django.views.decorators.csrf import ensure_csrf_cookie | ||
import mimetypes | ||
|
||
|
||
@ensure_csrf_cookie | ||
def index(request): | ||
""" Thin wrapper for the static index.html that adds the CSRF cookie.""" | ||
return HttpResponse(content=open(finders.find('index.html'))) | ||
def index(request: HttpRequest): | ||
"""Thin wrapper for the static index.html that adds the CSRF cookie.""" | ||
language = request.LANGUAGE_CODE | ||
page = request.path[1:].split("/", 1)[0] | ||
# pre-rendered version available? | ||
location = finders.find(path.join(language, page, "index.html")) | ||
if not location: | ||
location = finders.find(path.join(language, "index.html")) | ||
|
||
return HttpResponse(content=open(location)) |
This file contains 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
This file contains 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
Oops, something went wrong.