From c36451fc8e8d8f9c2f31ff3e3ad26ecc63ccbedb Mon Sep 17 00:00:00 2001
From: "Christopher C. Wells"
Date: Sat, 13 Apr 2019 21:50:35 -0700
Subject: [PATCH] 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 %}
- Permission Denied
+ {% trans "Permission Denied" %}
{% endblock %}
{% block content %}
- You do not have permission to access this resource. Contact a site
- administrator for assistance.
+ {% blocktrans %}You do not have permission to access this resource.
+ Contact a site administrator for assistance.{% endblocktrans %}
{% 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 %}
- Home
+ {% trans "Home" %}
{% block breadcrumbs %}{% endblock %}
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 %}
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 %}
{% endfor %}
- Submit
+ {% trans "Submit" %}
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 %}
- Error: {{ error }}
+ {% blocktrans %}Error: {{ error }}{% endblocktrans %}
{% endfor %}
{% elif form.errors %}
- Error: Some fields have errors. See below for details.
+ {% blocktrans %}Error: Some fields have errors. See below for details. {% endblocktrans %}
{% 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 %}
@@ -23,32 +23,38 @@
@@ -66,7 +72,8 @@
- Dashboard
+
+ {% trans "Dashboard" %}
@@ -76,40 +83,51 @@
href="#"
data-toggle="dropdown"
aria-haspopup="true"
- aria-expanded="false"> Children
+ aria-expanded="false">
+ {% trans "Children" %}
+
@@ -121,43 +139,61 @@
href="#"
data-toggle="dropdown"
aria-haspopup="true"
- aria-expanded="false"> Activities
+ aria-expanded="false">
+ {% trans "Activities" %}
+
@@ -179,29 +215,29 @@
aria-expanded="false"> {{ request.user }}
diff --git a/babybuddy/templates/babybuddy/paginator.html b/babybuddy/templates/babybuddy/paginator.html
index 0690beff..bf266a96 100644
--- a/babybuddy/templates/babybuddy/paginator.html
+++ b/babybuddy/templates/babybuddy/paginator.html
@@ -1,4 +1,4 @@
-{% load babybuddy_tags %}
+{% load i18n babybuddy_tags %}
{% if is_paginated %}
@@ -8,7 +8,7 @@
- Previous
+ {% trans "Previous" %}
{% endif %}
@@ -25,7 +25,7 @@
- Next
+ {% trans "Next" %}
{% endif %}
diff --git a/babybuddy/templates/babybuddy/user_confirm_delete.html b/babybuddy/templates/babybuddy/user_confirm_delete.html
index ed14ed47..be1a2b71 100644
--- a/babybuddy/templates/babybuddy/user_confirm_delete.html
+++ b/babybuddy/templates/babybuddy/user_confirm_delete.html
@@ -1,19 +1,19 @@
{% extends 'babybuddy/page.html' %}
-{% load widget_tweaks %}
+{% load i18n widget_tweaks %}
-{% block title %}Delete User{% endblock %}
+{% block title %}{% trans "Delete User" %}{% endblock %}
{% block breadcrumbs %}
- Users
+ {% trans "Users" %}
{{ object }}
- Delete
+ {% trans "Delete" %}
{% endblock %}
{% block content %}
{% endblock %}
\ No newline at end of file
diff --git a/babybuddy/templates/babybuddy/user_form.html b/babybuddy/templates/babybuddy/user_form.html
index 58e87180..fd38ad86 100644
--- a/babybuddy/templates/babybuddy/user_form.html
+++ b/babybuddy/templates/babybuddy/user_form.html
@@ -1,28 +1,29 @@
{% extends 'babybuddy/page.html' %}
+{% load i18n %}
{% block title %}
{% if object %}
{{ object }}
{% else %}
- Create User
+ {% trans "Create User" %}
{% endif %}
{% endblock %}
{% block breadcrumbs %}
- Users
+ {% trans "Users" %}
{% if object %}
{{ object }}
- Update
+ {% trans "Update" %}
{% else %}
- Create User
+ {% trans "Create User" %}
{% endif %}
{% endblock %}
{% block content %}
{% if object %}
- Update {{ object }}
+ {% blocktrans %}Update {{ object }} {% endblocktrans %}
{% else %}
- Create User
+ {% trans "Create User" %}
{% endif %}
{% include 'babybuddy/form.html' %}
{% endblock %}
diff --git a/babybuddy/templates/babybuddy/user_list.html b/babybuddy/templates/babybuddy/user_list.html
index 2e7e0aa8..aed6f57d 100644
--- a/babybuddy/templates/babybuddy/user_list.html
+++ b/babybuddy/templates/babybuddy/user_list.html
@@ -1,10 +1,10 @@
{% extends 'babybuddy/page.html' %}
-{% load bootstrap widget_tweaks %}
+{% load bootstrap i18n widget_tweaks %}
-{% block title %}Users{% endblock %}
+{% block title %}{% trans "Users" %}{% endblock %}
{% block breadcrumbs %}
- Users
+ {% trans "Users" %}
{% endblock %}
{% block content %}
@@ -14,13 +14,13 @@
- User
- First Name
- Last Name
- Email
- Staff
- Active
- Actions
+ {% trans "User" %}
+ {% trans "First Name" %}
+ {% trans "Last Name" %}
+ {% trans "Email" %}
+ {% trans "Staff" %}
+ {% trans "Active" %}
+ {% trans "Actions" %}
@@ -52,7 +52,7 @@
{% empty %}
- No users found.
+ {% trans "No users found." %}
{% endfor %}
@@ -62,7 +62,7 @@
{% if perms.admin.add_user %}
- Create User
+ {% trans "Create User" %}
{% endif %}
diff --git a/babybuddy/templates/babybuddy/user_password_form.html b/babybuddy/templates/babybuddy/user_password_form.html
index c5684ebb..3b96e379 100644
--- a/babybuddy/templates/babybuddy/user_password_form.html
+++ b/babybuddy/templates/babybuddy/user_password_form.html
@@ -1,14 +1,14 @@
{% extends 'babybuddy/page.html' %}
-{% load widget_tweaks %}
+{% load i18n widget_tweaks %}
-{% block title %}Change Password{% endblock %}
+{% block title %}{% trans "Change Password" %}{% endblock %}
{% block breadcrumbs %}
- User
- Change Password
+ {% trans "User" %}
+ {% trans "Change Password" %}
{% endblock %}
{% block content %}
- Change Password
+ {% trans "Change Password" %}
{% include 'babybuddy/form.html' %}
{% endblock %}
diff --git a/babybuddy/templates/babybuddy/user_settings_form.html b/babybuddy/templates/babybuddy/user_settings_form.html
index c8f188ea..a4e535fb 100644
--- a/babybuddy/templates/babybuddy/user_settings_form.html
+++ b/babybuddy/templates/babybuddy/user_settings_form.html
@@ -1,31 +1,31 @@
{% extends 'babybuddy/page.html' %}
{% load i18n widget_tweaks %}
-{% block title %}User Settings{% endblock %}
+{% block title %}{% trans "User Settings" %}{% endblock %}
{% block breadcrumbs %}
- User
- Settings
+ {% trans "User" %}
+ {% trans "Settings" %}
{% endblock %}
{% block content %}
- User Settings
+ {% trans "User Settings" %}
@@ -37,7 +37,7 @@
-
Sleep
+ {% trans "Sleep" %}
@@ -45,23 +45,23 @@
-
Tummy Time
+ {% trans "Tummy Time" %}
- As the amount of entries grows, Baby Buddy will help
+ {% blocktrans %}As the amount of entries grows, Baby Buddy will help
parents and caregivers to identify small patterns in baby's habits
using the dashboard and graphs. Baby Buddy is mobile-friendly and
uses a dark theme to help weary moms and dads with 2AM feedings and
changings. To get started, just click the button below to add your
- first (or second, third, etc.) child!
+ first (or second, third, etc.) child!{% endblocktrans %}
{% if perms.core.add_child %}
- Add a Child
+ {% trans "Add a Child" %}
{% endif %}
diff --git a/babybuddy/templates/registration/login.html b/babybuddy/templates/registration/login.html
index e3158977..2a6a23c0 100644
--- a/babybuddy/templates/registration/login.html
+++ b/babybuddy/templates/registration/login.html
@@ -1,5 +1,5 @@
{% extends "registration/base.html" %}
-{% load static widget_tweaks %}
+{% load i18n static widget_tweaks %}
{% block title %}Login{% endblock %}
{% block content %}
@@ -29,12 +29,12 @@
- Login
+ {% trans "Login" %}
- Forgot your password?
+ {% trans "Forgot your password?" %}
{% endblock %}
\ No newline at end of file
diff --git a/babybuddy/templates/registration/password_reset_complete.html b/babybuddy/templates/registration/password_reset_complete.html
index 0915f701..0c1441f4 100644
--- a/babybuddy/templates/registration/password_reset_complete.html
+++ b/babybuddy/templates/registration/password_reset_complete.html
@@ -1,11 +1,12 @@
{% extends "registration/base.html" %}
+{% load i18n %}
-{% block title %}Password Reset Successfully!{% endblock %}
+{% block title %}{% trans "Password Reset Successfully!" %}{% endblock %}
{% block content %}
-
Your password has been set. You may go ahead and log in now.
-
Log in
+
{% trans "Your password has been set. You may go ahead and log in now." %}
+
{% trans "Log in" %}
{% endblock %}
\ No newline at end of file
diff --git a/babybuddy/templates/registration/password_reset_confirm.html b/babybuddy/templates/registration/password_reset_confirm.html
index 310269ea..d6e752b7 100644
--- a/babybuddy/templates/registration/password_reset_confirm.html
+++ b/babybuddy/templates/registration/password_reset_confirm.html
@@ -1,7 +1,7 @@
{% extends "registration/base.html" %}
-{% load static widget_tweaks %}
+{% load i18n static widget_tweaks %}
-{% block title %}Password Reset{% endblock %}
+{% block title %}{% trans "Password Reset" %}{% endblock %}
{% block content %}
diff --git a/babybuddy/templates/registration/password_reset_done.html b/babybuddy/templates/registration/password_reset_done.html
index c70e2f32..c1f6c3be 100644
--- a/babybuddy/templates/registration/password_reset_done.html
+++ b/babybuddy/templates/registration/password_reset_done.html
@@ -1,15 +1,16 @@
{% extends "registration/base.html" %}
+{% load i18n %}
-{% block title %}Reset Email Sent{% endblock %}
+{% block title %}{% trans "Reset Email Sent" %}{% endblock %}
{% block content %}
-
We've emailed you instructions for setting your
+ {% blocktrans %}
We've emailed you instructions for setting your
password, if an account exists with the email you entered. You
should receive them shortly.
If you don't receive an email, please make sure you've
entered the address you registered with, and check your spam
- folder.
+ folder.{% endblocktrans %}
{% endblock %}
\ No newline at end of file
diff --git a/babybuddy/templates/registration/password_reset_form.html b/babybuddy/templates/registration/password_reset_form.html
index 071187ca..9f08f82f 100644
--- a/babybuddy/templates/registration/password_reset_form.html
+++ b/babybuddy/templates/registration/password_reset_form.html
@@ -1,13 +1,13 @@
{% extends "registration/base.html" %}
-{% load static widget_tweaks %}
+{% load i18n static widget_tweaks %}
-{% block title %}Forgot Password{% endblock %}
+{% block title %}{% trans "Forgot Password" %}{% endblock %}
{% block content %}
-
Enter your account email address in the form below. If
- the address is valid, you will receive instructions for resetting your
- password.
+ {% blocktrans %}
Enter your account email address in the
+ form below. If the address is valid, you will receive instructions for
+ resetting your password.
{% endblocktrans %}
diff --git a/babybuddy/views.py b/babybuddy/views.py
index 304baad3..428f0163 100644
--- a/babybuddy/views.py
+++ b/babybuddy/views.py
@@ -7,6 +7,7 @@ from django.contrib.auth.models import User
from django.contrib.messages.views import SuccessMessageMixin
from django.shortcuts import redirect, render
from django.urls import reverse, reverse_lazy
+from django.utils.translation import gettext as _
from django.views.generic import View
from django.views.generic.base import TemplateView, RedirectView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
@@ -51,7 +52,7 @@ class UserAdd(StaffOnlyMixin, PermissionRequired403Mixin, SuccessMessageMixin,
permission_required = ('admin.add_user',)
form_class = forms.UserAddForm
success_url = reverse_lazy('babybuddy:user-list')
- success_message = 'User %(username)s added!'
+ success_message = _('User %(username)s added!')
class UserUpdate(StaffOnlyMixin, PermissionRequired403Mixin,
@@ -61,7 +62,7 @@ class UserUpdate(StaffOnlyMixin, PermissionRequired403Mixin,
permission_required = ('admin.change_user',)
form_class = forms.UserUpdateForm
success_url = reverse_lazy('babybuddy:user-list')
- success_message = 'User %(username)s updated.'
+ success_message = _('User %(username)s updated.')
class UserDelete(StaffOnlyMixin, PermissionRequired403Mixin,
@@ -94,7 +95,7 @@ class UserPassword(LoginRequiredMixin, View):
if form.is_valid():
user = form.save()
update_session_auth_hash(request, user)
- messages.success(request, 'Password updated.')
+ messages.success(request, _('Password updated.'))
return render(request, self.template_name, {'form': form})
@@ -104,7 +105,7 @@ class UserResetAPIKey(LoginRequiredMixin, View):
"""
def get(self, request):
request.user.settings.api_key(reset=True)
- messages.success(request, 'User API key regenerated.')
+ messages.success(request, _('User API key regenerated.'))
return redirect('babybuddy:user-settings')
@@ -137,7 +138,7 @@ class UserSettings(LoginRequiredMixin, View):
user.settings = user_settings
user.save()
set_language(request)
- messages.success(request, 'Settings saved!')
+ messages.success(request, _('Settings saved!'))
return redirect('babybuddy:user-settings')
return render(request, self.template_name, {
'user_form': form_user,