Add translatable strings support to formatted strings (WIP).

This commit is contained in:
Christopher C. Wells 2019-04-14 14:42:58 -07:00
parent 8ed0e9117a
commit ee82286da5
3 changed files with 17 additions and 12 deletions

View File

@ -4,6 +4,7 @@ from django.db import models
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.timezone import timedelta from django.utils.timezone import timedelta
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
@ -31,7 +32,7 @@ class Settings(models.Model):
]) ])
def __str__(self): 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): def api_key(self, reset=False):
""" """

View File

@ -7,7 +7,8 @@ from django.contrib.auth.models import User
from django.contrib.messages.views import SuccessMessageMixin from django.contrib.messages.views import SuccessMessageMixin
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from django.urls import reverse, reverse_lazy 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 import View
from django.views.generic.base import TemplateView, RedirectView from django.views.generic.base import TemplateView, RedirectView
from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic.edit import CreateView, UpdateView, DeleteView
@ -52,7 +53,7 @@ class UserAdd(StaffOnlyMixin, PermissionRequired403Mixin, SuccessMessageMixin,
permission_required = ('admin.add_user',) permission_required = ('admin.add_user',)
form_class = forms.UserAddForm form_class = forms.UserAddForm
success_url = reverse_lazy('babybuddy:user-list') 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, class UserUpdate(StaffOnlyMixin, PermissionRequired403Mixin,
@ -62,7 +63,7 @@ class UserUpdate(StaffOnlyMixin, PermissionRequired403Mixin,
permission_required = ('admin.change_user',) permission_required = ('admin.change_user',)
form_class = forms.UserUpdateForm form_class = forms.UserUpdateForm
success_url = reverse_lazy('babybuddy:user-list') 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, class UserDelete(StaffOnlyMixin, PermissionRequired403Mixin,
@ -73,7 +74,9 @@ class UserDelete(StaffOnlyMixin, PermissionRequired403Mixin,
success_url = reverse_lazy('babybuddy:user-list') success_url = reverse_lazy('babybuddy:user-list')
def delete(self, request, *args, **kwargs): 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) messages.success(request, success_message)
return super(UserDelete, self).delete(request, *args, **kwargs) return super(UserDelete, self).delete(request, *args, **kwargs)

View File

@ -6,6 +6,7 @@ from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.template.defaultfilters import slugify from django.template.defaultfilters import slugify
from django.utils import timezone from django.utils import timezone
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -129,7 +130,7 @@ class DiaperChange(models.Model):
verbose_name_plural = _('Diaper Changes') verbose_name_plural = _('Diaper Changes')
def __str__(self): def __str__(self):
return 'Diaper Change' return str(_('Diaper Change'))
def attributes(self): def attributes(self):
attributes = [] attributes = []
@ -181,7 +182,7 @@ class Feeding(models.Model):
verbose_name_plural = _('Feedings') verbose_name_plural = _('Feedings')
def __str__(self): def __str__(self):
return 'Feeding' return str(_('Feeding'))
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.start and self.end: if self.start and self.end:
@ -222,7 +223,7 @@ class Note(models.Model):
verbose_name_plural = _('Notes') verbose_name_plural = _('Notes')
def __str__(self): def __str__(self):
return 'Note' return str(_('Note'))
class NapsManager(models.Manager): class NapsManager(models.Manager):
@ -253,7 +254,7 @@ class Sleep(models.Model):
verbose_name_plural = _('Sleep') verbose_name_plural = _('Sleep')
def __str__(self): def __str__(self):
return 'Sleep' return str(_('Sleep'))
@property @property
def nap(self): def nap(self):
@ -299,7 +300,7 @@ class Timer(models.Model):
verbose_name_plural = _('Timers') verbose_name_plural = _('Timers')
def __str__(self): def __str__(self):
return self.name or 'Timer #{}'.format(self.id) return self.name or str(format_lazy(_('Timer #{id}'), id=self.id))
@classmethod @classmethod
def from_db(cls, db, field_names, values): def from_db(cls, db, field_names, values):
@ -361,7 +362,7 @@ class TummyTime(models.Model):
verbose_name_plural = _('Tummy Time') verbose_name_plural = _('Tummy Time')
def __str__(self): def __str__(self):
return 'Tummy Time' return str(_('Tummy Time'))
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if self.start and self.end: if self.start and self.end:
@ -396,7 +397,7 @@ class Weight(models.Model):
verbose_name_plural = _('Weight') verbose_name_plural = _('Weight')
def __str__(self): def __str__(self):
return 'Weight' return str(_('Weight'))
def clean(self): def clean(self):
validate_date(self.date, 'date') validate_date(self.date, 'date')