From 61e1fd7ea0703c9e2096f6fae310df4cdb659c57 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@cs.tu-darmstadt.de>
Date: Sun, 20 Oct 2019 11:25:11 +0200
Subject: [PATCH] Introduce i18n support to frontend

Adapt settings
Add i18n urls to central url conf
Add language chooser to base.html
Adapt readme and utils
---
 AKPlanning/locale/de_DE/LC_MESSAGES/django.po | 26 ++++++++++++++
 AKPlanning/settings.py                        |  9 ++++-
 AKPlanning/urls.py                            |  1 +
 README.md                                     |  2 ++
 Utils/setup.sh                                |  4 +++
 Utils/update.sh                               |  2 ++
 templates/base.html                           | 34 ++++++++++++++++++-
 7 files changed, 76 insertions(+), 2 deletions(-)
 create mode 100644 AKPlanning/locale/de_DE/LC_MESSAGES/django.po

diff --git a/AKPlanning/locale/de_DE/LC_MESSAGES/django.po b/AKPlanning/locale/de_DE/LC_MESSAGES/django.po
new file mode 100644
index 00000000..97d38e3a
--- /dev/null
+++ b/AKPlanning/locale/de_DE/LC_MESSAGES/django.po
@@ -0,0 +1,26 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2019-10-20 08:43+0000\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: settings.py:124
+msgid "German"
+msgstr "Deutsch"
+
+#: settings.py:125
+msgid "English"
+msgstr "Englisch"
diff --git a/AKPlanning/settings.py b/AKPlanning/settings.py
index 0c97a248..7e52bedb 100644
--- a/AKPlanning/settings.py
+++ b/AKPlanning/settings.py
@@ -11,6 +11,8 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
 """
 
 import os
+from django.utils.translation import gettext_lazy as _
+
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -108,7 +110,7 @@ AUTH_PASSWORD_VALIDATORS = [
 # Internationalization
 # https://docs.djangoproject.com/en/2.2/topics/i18n/
 
-LANGUAGE_CODE = 'en-us'
+LANGUAGE_CODE = 'en-US'
 
 TIME_ZONE = 'UTC'
 
@@ -118,6 +120,11 @@ USE_L10N = True
 
 USE_TZ = True
 
+LANGUAGES = [
+    ('de', _('German')),
+    ('en', _('English')),
+]
+
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/2.2/howto/static-files/
 
diff --git a/AKPlanning/urls.py b/AKPlanning/urls.py
index 959681e6..44811231 100644
--- a/AKPlanning/urls.py
+++ b/AKPlanning/urls.py
@@ -19,4 +19,5 @@ from django.urls import path, include
 urlpatterns = [
     path('admin/', admin.site.urls),
     path('', include('AKSubmission.urls', namespace='submit')),
+    path('i18n/', include('django.conf.urls.i18n')),
 ]
diff --git a/README.md b/README.md
index 48eb11ec..79d82835 100644
--- a/README.md
+++ b/README.md
@@ -52,6 +52,8 @@ Python requirements are listed in ``requirements.txt``. They can be installed wi
 1. activate virtualenv ``source env/bin/activate``
 1. install python requirements ``pip install -r requirements.txt``
 1. setup necessary database tables etc. ``python manage.py migrate``
+1. prepare static files (can be omitted for dev setups) ``python manage.py collectstatic``
+1. compile translations ``python manage.py compilemessages``
 1. create a priviledged user, credentials are entered interactively on CLI ``python manage.py createsuperuser``
 1. deactivate virtualenv ``deactivate``
 
diff --git a/Utils/setup.sh b/Utils/setup.sh
index 7e9d7a2a..ba0a5aa0 100755
--- a/Utils/setup.sh
+++ b/Utils/setup.sh
@@ -18,6 +18,10 @@ pip install -r requirements.txt
 # Setup database
 python manage.py migrate
 
+# Prepare static files and translations
+python manage.py collectstatic --noinput
+python manage.py compilemessages
+
 # Create superuser
 # Credentials are entered interactively on CLI
 python manage.py createsuperuser
diff --git a/Utils/update.sh b/Utils/update.sh
index 24076742..e1568893 100755
--- a/Utils/update.sh
+++ b/Utils/update.sh
@@ -24,4 +24,6 @@ if [ "$1" = "--prod" ]; then
 fi
 
 ./manage.py migrate
+./manage.py collectstatic --noinput
+./manage.py compilemessages
 touch AKPlanning/wsgi.py
diff --git a/templates/base.html b/templates/base.html
index 669bd806..8ff7b8a6 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -1,4 +1,5 @@
 {% load static %}
+{% load i18n %}
 {% load bootstrap4 %}
 {% load fontawesome %}
 
@@ -15,8 +16,39 @@
 
 </head>
 <body>
+    {% block language-switcher %}
+        <!-- language switcher -->
+        <div class="container" style="margin-top:20px">
+            <form   action="{% url 'set_language' %}"
+                    method="post"
+                    class="form-inline"
+                    name="language-form"
+                    style="flex-direction: column;">
+                {% csrf_token %}
 
-    <div class="container" style="margin-top:20px;margin-bottom: 30px;">
+                <input name="next" type="hidden" value="{{ request.path }}" />
+
+                {% get_current_language as LANGUAGE_CODE %}
+                {% get_available_languages as LANGUAGES %}
+                {% get_language_info_list for LANGUAGES as languages %}
+
+                <div style="align-self: end;">
+                    {% for language in languages %}
+                        &nbsp;&nbsp;
+                        <button type="submit"
+                                class="btn {% if language.code == LANGUAGE_CODE %}btn-info{% else %}btn-outline-info{% endif %}"
+                                name="language"
+                                value="{{ language.code }}"
+                                style="align-self: end;">
+                            {{ language.code | upper }}
+                        </button>
+                    {% endfor %}
+                </div>
+            </form>
+        </div>
+    {% endblock %}
+
+    <div class="container" style="margin-top:15px;margin-bottom: 30px;">
         <ol class="breadcrumb">
             {% block breadcrumbs %}
             {% endblock %}
-- 
GitLab