From b9d49d53f2c5728c2566a18c7ad5cc001bdfcd42 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 16 Nov 2020 00:29:27 +0100 Subject: [PATCH] Export JS translation files as static files --- README.md | 7 +- ansible/roles/7-postinstall/tasks/main.yml | 18 ++- apps/scripts | 2 +- entrypoint.sh | 1 + note_kfet/static/js/jsi18n/_default.js | 134 +++++++++++++++++++++ note_kfet/static/js/jsi18n/de.js | 1 + note_kfet/static/js/jsi18n/es.js | 1 + note_kfet/static/js/jsi18n/fr.js | 1 + note_kfet/templates/base.html | 2 +- note_kfet/urls.py | 4 - 10 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 note_kfet/static/js/jsi18n/_default.js create mode 120000 note_kfet/static/js/jsi18n/de.js create mode 120000 note_kfet/static/js/jsi18n/es.js create mode 120000 note_kfet/static/js/jsi18n/fr.js diff --git a/README.md b/README.md index b5cda05d..b7c937c8 100644 --- a/README.md +++ b/README.md @@ -272,12 +272,13 @@ Il faut penser à ignorer les dossiers ne contenant pas notre code, dont le virt De plus, il faut aussi extraire les variables des fichiers JavaScript. ```bash -django-admin makemessages -i env -django-admin makemessages -i env -e js -d djangojs +python3 manage.py makemessages -i env +python3 manage.py makemessages -i env -e js -d djangojs ``` Une fois les fichiers édités, vous pouvez compiler les nouvelles traductions avec ```bash -django-admin compilemessages +python3 manage.py compilemessages +python3 manage.py compilejsmessages ``` diff --git a/ansible/roles/7-postinstall/tasks/main.yml b/ansible/roles/7-postinstall/tasks/main.yml index 6f3017a5..604327e6 100644 --- a/ansible/roles/7-postinstall/tasks/main.yml +++ b/ansible/roles/7-postinstall/tasks/main.yml @@ -1,4 +1,10 @@ --- +- name: Collect static files + command: /var/www/note_kfet/env/bin/python manage.py collectstatic --noinput + args: + chdir: /var/www/note_kfet + become_user: www-data + - name: Migrate Django database command: /var/www/note_kfet/env/bin/python manage.py migrate args: @@ -11,14 +17,14 @@ chdir: /var/www/note_kfet become_user: www-data +- name: Compile JavaScript messages + command: /var/www/note_kfet/env/bin/python manage.py compilejsmessages + args: + chdir: /var/www/note_kfet + become_user: www-data + - name: Install initial fixtures command: /var/www/note_kfet/env/bin/python manage.py loaddata initial args: chdir: /var/www/note_kfet become_user: postgres - -- name: Collect static files - command: /var/www/note_kfet/env/bin/python manage.py collectstatic --noinput - args: - chdir: /var/www/note_kfet - become_user: www-data diff --git a/apps/scripts b/apps/scripts index 7e27c3b7..dbe7bf65 160000 --- a/apps/scripts +++ b/apps/scripts @@ -1 +1 @@ -Subproject commit 7e27c3b71b04af0867d5fbe4916e2d1278637599 +Subproject commit dbe7bf65917df40b0ce476f357d04726e20b406f diff --git a/entrypoint.sh b/entrypoint.sh index dfd68977..f04bf16d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -14,6 +14,7 @@ fi # Set up Django project python3 manage.py collectstatic --noinput python3 manage.py compilemessages +python3 manage.py compilejsmessages python3 manage.py migrate if [ "$1" ]; then diff --git a/note_kfet/static/js/jsi18n/_default.js b/note_kfet/static/js/jsi18n/_default.js new file mode 100644 index 00000000..97775e9d --- /dev/null +++ b/note_kfet/static/js/jsi18n/_default.js @@ -0,0 +1,134 @@ +/* +* You should never see this file. +* It is here only for compatibility reasons in case of the command `compilejsmessages` was never executed. +* Please execute this command to generate translation strings. +*/ + +(function(globals) { + + var django = globals.django || (globals.django = {}); + + + django.pluralidx = function(n) { + var v=(n != 1); + if (typeof(v) == 'boolean') { + return v ? 1 : 0; + } else { + return v; + } + }; + + + /* gettext library */ + + django.catalog = django.catalog || {}; + + + if (!django.jsi18n_initialized) { + django.gettext = function(msgid) { + var value = django.catalog[msgid]; + if (typeof(value) == 'undefined') { + return msgid; + } else { + return (typeof(value) == 'string') ? value : value[0]; + } + }; + + django.ngettext = function(singular, plural, count) { + var value = django.catalog[singular]; + if (typeof(value) == 'undefined') { + return (count == 1) ? singular : plural; + } else { + return value.constructor === Array ? value[django.pluralidx(count)] : value; + } + }; + + django.gettext_noop = function(msgid) { return msgid; }; + + django.pgettext = function(context, msgid) { + var value = django.gettext(context + '\x04' + msgid); + if (value.indexOf('\x04') != -1) { + value = msgid; + } + return value; + }; + + django.npgettext = function(context, singular, plural, count) { + var value = django.ngettext(context + '\x04' + singular, context + '\x04' + plural, count); + if (value.indexOf('\x04') != -1) { + value = django.ngettext(singular, plural, count); + } + return value; + }; + + django.interpolate = function(fmt, obj, named) { + if (named) { + return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])}); + } else { + return fmt.replace(/%s/g, function(match){return String(obj.shift())}); + } + }; + + + /* formatting library */ + + django.formats = { + "DATETIME_FORMAT": "j \\d\\e F \\d\\e Y \\a \\l\\a\\s H:i", + "DATETIME_INPUT_FORMATS": [ + "%d/%m/%Y %H:%M:%S", + "%d/%m/%Y %H:%M:%S.%f", + "%d/%m/%Y %H:%M", + "%d/%m/%y %H:%M:%S", + "%d/%m/%y %H:%M:%S.%f", + "%d/%m/%y %H:%M", + "%Y-%m-%d %H:%M:%S", + "%Y-%m-%d %H:%M:%S.%f", + "%Y-%m-%d %H:%M", + "%Y-%m-%d" + ], + "DATE_FORMAT": "j \\d\\e F \\d\\e Y", + "DATE_INPUT_FORMATS": [ + "%d/%m/%Y", + "%d/%m/%y", + "%Y-%m-%d" + ], + "DECIMAL_SEPARATOR": ",", + "FIRST_DAY_OF_WEEK": 1, + "MONTH_DAY_FORMAT": "j \\d\\e F", + "NUMBER_GROUPING": 3, + "SHORT_DATETIME_FORMAT": "d/m/Y H:i", + "SHORT_DATE_FORMAT": "d/m/Y", + "THOUSAND_SEPARATOR": ".", + "TIME_FORMAT": "H:i", + "TIME_INPUT_FORMATS": [ + "%H:%M:%S", + "%H:%M:%S.%f", + "%H:%M" + ], + "YEAR_MONTH_FORMAT": "F \\d\\e Y" + }; + + django.get_format = function(format_type) { + var value = django.formats[format_type]; + if (typeof(value) == 'undefined') { + return format_type; + } else { + return value; + } + }; + + /* add to global namespace */ + globals.pluralidx = django.pluralidx; + globals.gettext = django.gettext; + globals.ngettext = django.ngettext; + globals.gettext_noop = django.gettext_noop; + globals.pgettext = django.pgettext; + globals.npgettext = django.npgettext; + globals.interpolate = django.interpolate; + globals.get_format = django.get_format; + + django.jsi18n_initialized = true; + } + +}(this)); + diff --git a/note_kfet/static/js/jsi18n/de.js b/note_kfet/static/js/jsi18n/de.js new file mode 120000 index 00000000..6fff6d1a --- /dev/null +++ b/note_kfet/static/js/jsi18n/de.js @@ -0,0 +1 @@ +_default.js \ No newline at end of file diff --git a/note_kfet/static/js/jsi18n/es.js b/note_kfet/static/js/jsi18n/es.js new file mode 120000 index 00000000..6fff6d1a --- /dev/null +++ b/note_kfet/static/js/jsi18n/es.js @@ -0,0 +1 @@ +_default.js \ No newline at end of file diff --git a/note_kfet/static/js/jsi18n/fr.js b/note_kfet/static/js/jsi18n/fr.js new file mode 120000 index 00000000..6fff6d1a --- /dev/null +++ b/note_kfet/static/js/jsi18n/fr.js @@ -0,0 +1 @@ +_default.js \ No newline at end of file diff --git a/note_kfet/templates/base.html b/note_kfet/templates/base.html index d77e1fa8..43f66b79 100644 --- a/note_kfet/templates/base.html +++ b/note_kfet/templates/base.html @@ -39,7 +39,7 @@ SPDX-License-Identifier: GPL-3.0-or-later {# Translation in javascript files #} - + {# If extra ressources are needed for a form, load here #} {% if form.media %} diff --git a/note_kfet/urls.py b/note_kfet/urls.py index a8136c85..ae6bf3db 100644 --- a/note_kfet/urls.py +++ b/note_kfet/urls.py @@ -6,7 +6,6 @@ from django.conf.urls.static import static from django.urls import path, include from django.views.defaults import bad_request, permission_denied, page_not_found, server_error from django.views.generic import RedirectView -from django.views.i18n import JavaScriptCatalog from member.views import CustomLoginView @@ -35,9 +34,6 @@ urlpatterns = [ # Make coffee path('coffee/', include('django_htcpcp_tea.urls')), - - # Translate js - path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'), ] # During development, serve media files