Commit 5ca2e13e authored by Julien Palard's avatar Julien Palard
Browse files

Use django-modeltranslation.

parent 3c370adc
......@@ -26,20 +26,13 @@ celery -A hkis worker
## Translations
Templates are translated using django `makemessages` and `compilemessages` commands:
$ django-admin makemessages -a
# Edit the .po files
$ django-admin compilemessages
To translate exercises you'll need to fetch them via the API first:
$ pip install requests
$ mkdir -p locale/fr/LC_MESSAGES/
$ python scripts/ --endpoint
$ python scripts/ -o locale/fr/LC_MESSAGES/django.pot
$ cd locale/fr/LC_MESSAGES
$ if [ -f django.po ] ; then msgmerge -U django.po django.pot; else cp django.pot django.po; fi
# msgfmt django.po -o
Exercises (title and wording) are translated via the admin (or the
API), we use django-modeltranslation.
......@@ -39,6 +39,7 @@ INSTALLED_APPS = [
......@@ -161,7 +162,7 @@ INTERNAL_IPS = {""}
LANGUAGES = [("fr", "Français"), ("en", "English")]
LANGUAGES = [("en", "English"), ("fr", "Français")]
TIME_ZONE = "Europe/Paris"
# This file is distributed under the same license as the PACKAGE package.
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-07-22 09:00+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: scripts/
#, python-format
msgid ""
"pygettext -- Python equivalent of xgettext(1)\n"
"Many systems (Solaris, Linux, Gnu) provide extensive tools that ease the\n"
"internationalization of C programs. Most of these tools are independent of\n"
"the programming language and can be used from within Python programs.\n"
"Martin von Loewis' work[1] helps considerably in this regard.\n"
"There's one problem though; xgettext is the program that scans source code\n"
"looking for message strings, but it groks only C (or C++). Python\n"
"introduces a few wrinkles, such as dual quoting characters, triple quoted\n"
"strings, and raw strings. xgettext understands none of this.\n"
"Enter pygettext, which uses Python's standard tokenize module to scan\n"
"Python source code, generating .pot files identical to what GNU xgettext[2]\n"
"generates for C and C++ code. From there, the standard GNU tools can be\n"
"A word about marking Python strings as candidates for translation. GNU\n"
"xgettext recognizes the following keywords: gettext, dgettext, dcgettext,\n"
"and gettext_noop. But those can be a lot of text to include all over your\n"
"code. C and C++ have a trick: they use the C preprocessor. Most\n"
"internationalized C source includes a #define for gettext() to _() so that\n"
"what has to be written in the source is much less. Thus these are both\n"
"translatable strings:\n"
" gettext(\"Translatable String\")\n"
" _(\"Translatable String\")\n"
"Python of course has no preprocessor so this doesn't work so well. Thus,\n"
"pygettext searches only for _() by default, but see the -k/--keyword flag\n"
"below for how to augment this.\n"
" [1]\n"
" [2]\n"
"NOTE: pygettext attempts to be option and feature compatible with GNU\n"
"xgettext where ever possible. However some options are still missing or are\n"
"not fully implemented. Also, xgettext's use of command line switches with\n"
"option arguments is broken, and in these cases, pygettext just defines\n"
"additional switches.\n"
"Usage: pygettext [options] inputfile ...\n"
" -a\n"
" --extract-all\n"
" Extract all strings.\n"
" -d name\n"
" --default-domain=name\n"
" Rename the default output file from messages.pot to name.pot.\n"
" -E\n"
" --escape\n"
" Replace non-ASCII characters with octal escape sequences.\n"
" -D\n"
" --docstrings\n"
" Extract module, class, method, and function docstrings. These do\n"
" not need to be wrapped in _() markers, and in fact cannot be for\n"
" Python to consider them docstrings. (See also the -X option).\n"
" -h\n"
" --help\n"
" Print this help message and exit.\n"
" -k word\n"
" --keyword=word\n"
" Keywords to look for in addition to the default set, which are:\n"
" You can have multiple -k flags on the command line.\n"
" -K\n"
" --no-default-keywords\n"
" Disable the default set of keywords (see above). Any keywords\n"
" explicitly added with the -k/--keyword option are still recognized.\n"
" --no-location\n"
" Do not write filename/lineno location comments.\n"
" -n\n"
" --add-location\n"
" Write filename/lineno location comments indicating where each\n"
" extracted string is found in the source. These lines appear before\n"
" each msgid. The style of comments is controlled by the -S/--style\n"
" option. This is the default.\n"
" -o filename\n"
" --output=filename\n"
" Rename the default output file from messages.pot to filename. If\n"
" filename is `-' then the output is sent to standard out.\n"
" -p dir\n"
" --output-dir=dir\n"
" Output files will be placed in directory dir.\n"
" -S stylename\n"
" --style stylename\n"
" Specify which style to use for location comments. Two styles are\n"
" supported:\n"
" Solaris # File: filename, line: line-number\n"
" GNU #: filename:line\n"
" The style name is case insensitive. GNU style is the default.\n"
" -v\n"
" --verbose\n"
" Print the names of the files being processed.\n"
" -V\n"
" --version\n"
" Print the version of pygettext and exit.\n"
" -w columns\n"
" --width=columns\n"
" Set width of output to columns.\n"
" -x filename\n"
" --exclude-file=filename\n"
" Specify a file that contains a list of strings that are not be\n"
" extracted from the input files. Each string to be excluded must\n"
" appear on a line by itself in the file.\n"
" -X filename\n"
" --no-docstrings=filename\n"
" Specify a file that contains a list of files (one per line) that\n"
" should not have their docstrings extracted. This is only useful in\n"
" conjunction with the -D option above.\n"
"If `inputfile' is -, standard input is read.\n"
msgstr ""
#: scripts/
#, python-format
msgid ""
"# Copyright (C) YEAR ORGANIZATION\n"
"msgid \"\"\n"
"msgstr \"\"\n"
"\"Project-Id-Version: PACKAGE VERSION\\n\"\n"
"\"POT-Creation-Date: %(time)s\\n\"\n"
"\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n"
"\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"\n"
"\"Language-Team: LANGUAGE <>\\n\"\n"
"\"MIME-Version: 1.0\\n\"\n"
"\"Content-Type: text/plain; charset=%(charset)s\\n\"\n"
"\"Content-Transfer-Encoding: %(encoding)s\\n\"\n"
"\"Generated-By: %(version)s\\n\"\n"
msgstr ""
#: scripts/
#, python-format
msgid "*** %(file)s:%(lineno)s: Seen unexpected token \"%(token)s\""
msgstr ""
#: scripts/
#, python-format
msgid "# File: %(filename)s, line: %(lineno)d"
msgstr ""
#: scripts/
#, python-format
msgid " %(filename)s:%(lineno)d"
msgstr ""
#: scripts/
#, python-format
msgid "Invalid value for --style: %s"
msgstr ""
#: scripts/
#, python-format
msgid " (xgettext for Python) %s"
msgstr ""
#: scripts/
#, python-format
msgid "--width argument must be an integer: %s"
msgstr ""
#: scripts/
#, python-format
msgid "Can't read --exclude-file: %s"
msgstr ""
#: scripts/
msgid "Reading standard input"
msgstr ""
#: scripts/
#, python-format
msgid "Working on %s"
msgstr ""
#: scripts/
#, python-format
msgid "*** Seen unexpected token \"%(token)s\""
msgstr ""
#: scripts/
msgid "morethanonestring"
msgstr ""
#: templates/admin/base_site.html:3
msgid "Django site admin"
msgstr ""
......@@ -13,6 +13,7 @@ django-extensions
......@@ -26,8 +26,9 @@ django-debug-toolbar==2.2 # via -r
django-extensions==3.0.3 # via -r
django-filter==2.3.0 # via -r
django-model-utils==4.0.0 # via -r
django-modeltranslation==0.15.2 # via -r
django-registration-redux==2.8 # via -r
django==3.0.8 # via -r, channels, django-ace, django-debug-toolbar, django-filter, django-model-utils, djangorestframework
django==3.0.8 # via -r, channels, django-ace, django-debug-toolbar, django-filter, django-model-utils, django-modeltranslation, djangorestframework
djangorestframework==3.11.0 # via -r
future==0.18.2 # via celery
gunicorn==20.0.4 # via -r
......@@ -49,7 +50,7 @@ pyparsing==2.4.7 # via packaging
pytz==2020.1 # via celery, django
redis==3.5.3 # via celery
service-identity==18.1.0 # via twisted
six==1.15.0 # via automat, bleach, cryptography, django-extensions, packaging, pyopenssl
six==1.15.0 # via automat, bleach, cryptography, django-extensions, django-modeltranslation, packaging, pyopenssl
sqlparse==0.3.1 # via django, django-debug-toolbar
twisted[tls]==20.3.0 # via daphne
txaio==20.4.1 # via autobahn
......@@ -41,12 +41,9 @@ def main():
if "results" not in exercises:
translatables = []
for exercise in exercises["results"]:
path = Path("exercises") / exercise["slug"]
path.mkdir(exist_ok=True, parents=True)
for file in "", "", "", "":
(path / (file)).write_text(
......@@ -54,9 +51,6 @@ def main():
del exercise[file.split(".")[0]]
(path / "meta").write_text(json.dumps(exercise, indent=4))
next_exercise_page = exercises["next"]
with open("", "w", encoding="UTF-8") as to_translate:
for string in translatables:
if __name__ == "__main__":
This diff is collapsed.
......@@ -6,6 +6,8 @@ from django.urls import reverse
from django.utils.safestring import mark_safe
from django_ace import AceWidget
from modeltranslation.admin import TranslationAdmin
from website.models import Answer, Exercise, Snippet
......@@ -53,7 +55,7 @@ class AnswerExerciseForm(forms.ModelForm):
class ExerciseAdmin(admin.ModelAdmin):
class ExerciseAdmin(TranslationAdmin):
def get_queryset(self, request):
return super().get_queryset(request).with_weekly_stats()
# Generated by Django 3.0.8 on 2020-09-27 14:03
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('website', '0015_exercise_author'),
operations = [
field=models.CharField(max_length=255, null=True),
field=models.CharField(max_length=255, null=True),
from modeltranslation.translator import translator, TranslationOptions
from .models import Exercise
class ExerciseTranslationOptions(TranslationOptions):
fields = ("title", "wording")
translator.register(Exercise, ExerciseTranslationOptions)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment