From ee82286da510799afc2b956b292741b84a4f9f77 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sun, 14 Apr 2019 14:42:58 -0700 Subject: [PATCH] Add translatable strings support to formatted strings (WIP). --- babybuddy/models.py | 3 ++- babybuddy/views.py | 11 +++++++---- core/models.py | 15 ++++++++------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/babybuddy/models.py b/babybuddy/models.py index 070db1a9..94d515e2 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.text import format_lazy from django.utils.translation import gettext_lazy as _ from rest_framework.authtoken.models import Token @@ -31,7 +32,7 @@ class Settings(models.Model): ]) def __str__(self): - return '{}\'s Settings'.format(self.user) + return str(format_lazy(_('{user}\'s Settings'), user=self.user)) def api_key(self, reset=False): """ diff --git a/babybuddy/views.py b/babybuddy/views.py index 428f0163..691832a8 100644 --- a/babybuddy/views.py +++ b/babybuddy/views.py @@ -7,7 +7,8 @@ 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.utils.text import format_lazy +from django.utils.translation import gettext as _, gettext_lazy from django.views.generic import View from django.views.generic.base import TemplateView, RedirectView from django.views.generic.edit import CreateView, UpdateView, DeleteView @@ -52,7 +53,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 = gettext_lazy('User %(username)s added!') class UserUpdate(StaffOnlyMixin, PermissionRequired403Mixin, @@ -62,7 +63,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 = gettext_lazy('User %(username)s updated.') class UserDelete(StaffOnlyMixin, PermissionRequired403Mixin, @@ -73,7 +74,9 @@ class UserDelete(StaffOnlyMixin, PermissionRequired403Mixin, success_url = reverse_lazy('babybuddy:user-list') def delete(self, request, *args, **kwargs): - success_message = 'User {} deleted.'.format(self.get_object()) + success_message = format_lazy(gettext_lazy( + 'User {user} deleted.'), user=self.get_object() + ) messages.success(request, success_message) return super(UserDelete, self).delete(request, *args, **kwargs) diff --git a/core/models.py b/core/models.py index 0656b0ad..30d69cee 100644 --- a/core/models.py +++ b/core/models.py @@ -6,6 +6,7 @@ from django.core.exceptions import ValidationError from django.db import models from django.template.defaultfilters import slugify from django.utils import timezone +from django.utils.text import format_lazy from django.utils.translation import gettext_lazy as _ @@ -129,7 +130,7 @@ class DiaperChange(models.Model): verbose_name_plural = _('Diaper Changes') def __str__(self): - return 'Diaper Change' + return str(_('Diaper Change')) def attributes(self): attributes = [] @@ -181,7 +182,7 @@ class Feeding(models.Model): verbose_name_plural = _('Feedings') def __str__(self): - return 'Feeding' + return str(_('Feeding')) def save(self, *args, **kwargs): if self.start and self.end: @@ -222,7 +223,7 @@ class Note(models.Model): verbose_name_plural = _('Notes') def __str__(self): - return 'Note' + return str(_('Note')) class NapsManager(models.Manager): @@ -253,7 +254,7 @@ class Sleep(models.Model): verbose_name_plural = _('Sleep') def __str__(self): - return 'Sleep' + return str(_('Sleep')) @property def nap(self): @@ -299,7 +300,7 @@ class Timer(models.Model): verbose_name_plural = _('Timers') def __str__(self): - return self.name or 'Timer #{}'.format(self.id) + return self.name or str(format_lazy(_('Timer #{id}'), id=self.id)) @classmethod def from_db(cls, db, field_names, values): @@ -361,7 +362,7 @@ class TummyTime(models.Model): verbose_name_plural = _('Tummy Time') def __str__(self): - return 'Tummy Time' + return str(_('Tummy Time')) def save(self, *args, **kwargs): if self.start and self.end: @@ -396,7 +397,7 @@ class Weight(models.Model): verbose_name_plural = _('Weight') def __str__(self): - return 'Weight' + return str(_('Weight')) def clean(self): validate_date(self.date, 'date')