From 8f0c2f3159b10ae2fffc2133ceeec0a53236173e Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 13 Apr 2019 19:13:14 -0700 Subject: [PATCH 01/27] Add locale middleware. --- babybuddy/settings/base.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/babybuddy/settings/base.py b/babybuddy/settings/base.py index c49e19c8..7969f6d4 100644 --- a/babybuddy/settings/base.py +++ b/babybuddy/settings/base.py @@ -53,10 +53,9 @@ INSTALLED_APPS = [ MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', - 'whitenoise.middleware.WhiteNoiseMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', @@ -120,6 +119,10 @@ USE_L10N = True USE_TZ = True +LOCALE_PATHS = [ + os.path.join(BASE_DIR, "locale"), +] + # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ From 0e4d80dab06e64e5f9bbceede1b433a7cfc0358d Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 13 Apr 2019 20:07:46 -0700 Subject: [PATCH 02/27] Add cookie-based language selection to user settings. This will likely be refactored to use a database column instead of dropping a cookie. --- .../babybuddy/user_settings_form.html | 19 ++++++++++++++++++- babybuddy/urls.py | 1 + babybuddy/views.py | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/babybuddy/templates/babybuddy/user_settings_form.html b/babybuddy/templates/babybuddy/user_settings_form.html index 26b73c01..c8f188ea 100644 --- a/babybuddy/templates/babybuddy/user_settings_form.html +++ b/babybuddy/templates/babybuddy/user_settings_form.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load widget_tweaks %} +{% load i18n widget_tweaks %} {% block title %}User Settings{% endblock %} @@ -41,6 +41,23 @@ {% include 'babybuddy/form_field.html' %} {% endwith %} +
+ +
+ +
+
Dashboard diff --git a/babybuddy/urls.py b/babybuddy/urls.py index 542333d7..a18f2a11 100644 --- a/babybuddy/urls.py +++ b/babybuddy/urls.py @@ -53,6 +53,7 @@ urlpatterns = [ path('admin/', admin.site.urls), path('', include('api.urls', namespace='api')), path('', include((app_patterns, 'babybuddy'), namespace='babybuddy')), + path('user/lang', include('django.conf.urls.i18n')), path('', include('core.urls', namespace='core')), path('', include('dashboard.urls', namespace='dashboard')), path('', include('reports.urls', namespace='reports')), diff --git a/babybuddy/views.py b/babybuddy/views.py index f540f9e0..304baad3 100644 --- a/babybuddy/views.py +++ b/babybuddy/views.py @@ -10,6 +10,7 @@ from django.urls import reverse, reverse_lazy from django.views.generic import View from django.views.generic.base import TemplateView, RedirectView from django.views.generic.edit import CreateView, UpdateView, DeleteView +from django.views.i18n import set_language from django_filters.views import FilterView @@ -135,6 +136,7 @@ class UserSettings(LoginRequiredMixin, View): user_settings = form_settings.save(commit=False) user.settings = user_settings user.save() + set_language(request) messages.success(request, 'Settings saved!') return redirect('babybuddy:user-settings') return render(request, self.template_name, { From d52694e93e1d83aef778b0e8cbf5443fc142970c Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 13 Apr 2019 20:09:55 -0700 Subject: [PATCH 03/27] Set languages list manually. --- babybuddy/settings/base.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/babybuddy/settings/base.py b/babybuddy/settings/base.py index 7969f6d4..7299d5c4 100644 --- a/babybuddy/settings/base.py +++ b/babybuddy/settings/base.py @@ -1,5 +1,6 @@ import os +from django.utils.translation import gettext_lazy as _ from dotenv import load_dotenv, find_dotenv # Build paths inside the project like this: os.path.join(BASE_DIR, ...) @@ -123,6 +124,11 @@ LOCALE_PATHS = [ os.path.join(BASE_DIR, "locale"), ] +LANGUAGES = [ + ('en', _('English')), + ('fr', _('French')), +] + # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ From ad1cc1ee94c89b71027e109755cc8993dfec3e4e Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 13 Apr 2019 20:50:36 -0700 Subject: [PATCH 04/27] Add gulp commands for make/compilemessages. --- gulpfile.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gulpfile.js b/gulpfile.js index 7f419e5f..6276e493 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -171,6 +171,18 @@ gulp.task('makemigrations', function(cb) { spawn('pipenv', command, { stdio: 'inherit' }).on('exit', cb); }); +gulp.task('makemessages', function(cb) { + var command = ['run', 'python', 'manage.py', 'makemessages']; + command = command.concat(process.argv.splice(3)); + spawn('pipenv', command, { stdio: 'inherit' }).on('exit', cb); +}); + +gulp.task('compilemessages', function(cb) { + var command = ['run', 'python', 'manage.py', 'compilemessages']; + command = command.concat(process.argv.splice(3)); + spawn('pipenv', command, { stdio: 'inherit' }).on('exit', cb); +}); + gulp.task('reset', function(cb) { spawn( 'pipenv', From c36451fc8e8d8f9c2f31ff3e3ad26ecc63ccbedb Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 13 Apr 2019 21:50:35 -0700 Subject: [PATCH 05/27] Add translateable strings support in babybuddy app. --- babybuddy/admin.py | 6 +- babybuddy/models.py | 25 +++-- babybuddy/templates/403.html | 10 +- babybuddy/templates/babybuddy/base.html | 4 +- babybuddy/templates/babybuddy/filter.html | 8 +- babybuddy/templates/babybuddy/form.html | 4 +- babybuddy/templates/babybuddy/messages.html | 6 +- .../templates/babybuddy/nav-dropdown.html | 106 ++++++++++++------ babybuddy/templates/babybuddy/paginator.html | 6 +- .../babybuddy/user_confirm_delete.html | 12 +- babybuddy/templates/babybuddy/user_form.html | 13 ++- babybuddy/templates/babybuddy/user_list.html | 24 ++-- .../babybuddy/user_password_form.html | 10 +- .../babybuddy/user_settings_form.html | 26 ++--- babybuddy/templates/babybuddy/welcome.html | 26 ++--- babybuddy/templates/registration/login.html | 6 +- .../registration/password_reset_complete.html | 7 +- .../registration/password_reset_confirm.html | 13 +-- .../registration/password_reset_done.html | 7 +- .../registration/password_reset_form.html | 12 +- babybuddy/views.py | 11 +- 21 files changed, 193 insertions(+), 149 deletions(-) diff --git a/babybuddy/admin.py b/babybuddy/admin.py index ee7e4fd0..0eb69815 100644 --- a/babybuddy/admin.py +++ b/babybuddy/admin.py @@ -2,16 +2,18 @@ from django.contrib import admin from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.models import User +from django.utils.translation import gettext_lazy as _ from babybuddy import models class SettingsInline(admin.StackedInline): model = models.Settings - verbose_name_plural = 'Settings' + verbose_name = _('Settings') + verbose_name_plural = _('Settings') can_delete = False fieldsets = ( - ('Dashboard', { + (_('Dashboard'), { 'fields': ('dashboard_refresh_rate',) }), ) diff --git a/babybuddy/models.py b/babybuddy/models.py index 1c36ed9b..070db1a9 100644 --- a/babybuddy/models.py +++ b/babybuddy/models.py @@ -4,6 +4,7 @@ from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver from django.utils.timezone import timedelta +from django.utils.translation import gettext_lazy as _ from rest_framework.authtoken.models import Token @@ -11,22 +12,22 @@ from rest_framework.authtoken.models import Token class Settings(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) dashboard_refresh_rate = models.DurationField( - verbose_name='Refresh rate', - help_text='This setting will only be used when a browser does not ' - 'support refresh on focus.', + verbose_name=_('Refresh rate'), + help_text=_('This setting will only be used when a browser does not ' + 'support refresh on focus.'), blank=True, null=True, default=timedelta(minutes=1), choices=[ - (None, 'disabled'), - (timedelta(minutes=1), '1 min.'), - (timedelta(minutes=2), '2 min.'), - (timedelta(minutes=3), '3 min.'), - (timedelta(minutes=4), '4 min.'), - (timedelta(minutes=5), '5 min.'), - (timedelta(minutes=10), '10 min.'), - (timedelta(minutes=15), '15 min.'), - (timedelta(minutes=30), '30 min.'), + (None, _('disabled')), + (timedelta(minutes=1), _('1 min.')), + (timedelta(minutes=2), _('2 min.')), + (timedelta(minutes=3), _('3 min.')), + (timedelta(minutes=4), _('4 min.')), + (timedelta(minutes=5), _('5 min.')), + (timedelta(minutes=10), _('10 min.')), + (timedelta(minutes=15), _('15 min.')), + (timedelta(minutes=30), _('30 min.')), ]) def __str__(self): diff --git a/babybuddy/templates/403.html b/babybuddy/templates/403.html index 0eb0d4d5..530950a0 100644 --- a/babybuddy/templates/403.html +++ b/babybuddy/templates/403.html @@ -1,15 +1,15 @@ {% extends 'babybuddy/page.html' %} -{% load widget_tweaks %} +{% load i18n widget_tweaks %} -{% block title %}403 Permission Denied{% endblock %} +{% block title %}403 {% trans "Permission Denied" %}{% endblock %} {% block breadcrumbs %} - + {% endblock %} {% block content %} {% endblock %} \ No newline at end of file diff --git a/babybuddy/templates/babybuddy/base.html b/babybuddy/templates/babybuddy/base.html index 04878c35..a17188aa 100644 --- a/babybuddy/templates/babybuddy/base.html +++ b/babybuddy/templates/babybuddy/base.html @@ -1,4 +1,4 @@ -{% load static %} +{% load i18n static %} @@ -31,7 +31,7 @@ {% block breadcrumb_nav %} diff --git a/babybuddy/templates/babybuddy/filter.html b/babybuddy/templates/babybuddy/filter.html index 0379d6a9..84765a40 100644 --- a/babybuddy/templates/babybuddy/filter.html +++ b/babybuddy/templates/babybuddy/filter.html @@ -1,4 +1,4 @@ -{% load widget_tweaks %} +{% load i18n widget_tweaks %}
@@ -15,8 +15,8 @@
{% endfor %}
- - Reset + + {% trans "Reset" %}
@@ -29,6 +29,6 @@ role="button" aria-expanded="false" aria-controls="filter_form"> - Filters + {% trans "Filters" %}

diff --git a/babybuddy/templates/babybuddy/form.html b/babybuddy/templates/babybuddy/form.html index 83dc2555..25bf20dc 100644 --- a/babybuddy/templates/babybuddy/form.html +++ b/babybuddy/templates/babybuddy/form.html @@ -1,4 +1,4 @@ -{% load widget_tweaks %} +{% load i18n widget_tweaks %}
@@ -8,6 +8,6 @@ {% include 'babybuddy/form_field.html' %}
{% endfor %} - +
diff --git a/babybuddy/templates/babybuddy/messages.html b/babybuddy/templates/babybuddy/messages.html index 45627ffe..c751a753 100644 --- a/babybuddy/templates/babybuddy/messages.html +++ b/babybuddy/templates/babybuddy/messages.html @@ -1,3 +1,5 @@ +{% load i18n %} + {% block messages %} {% if messages %} {% for message in messages %} @@ -13,12 +15,12 @@ {% if form.non_field_errors %} {% for error in form.non_field_errors %} {% endfor %} {% elif form.errors %} {% endif %} {% endif %} diff --git a/babybuddy/templates/babybuddy/nav-dropdown.html b/babybuddy/templates/babybuddy/nav-dropdown.html index 43d4013b..1bace63c 100644 --- a/babybuddy/templates/babybuddy/nav-dropdown.html +++ b/babybuddy/templates/babybuddy/nav-dropdown.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/base.html' %} -{% load babybuddy_tags static timers %} +{% load babybuddy_tags i18n static timers %} {% block nav %}