Use lazy gettext in models and forms to allow things to be shown in Spanish.

This commit is contained in:
Anna Sidwell 2019-04-07 17:04:02 +01:00
parent 6e34abda84
commit 87e9296007
4 changed files with 95 additions and 52 deletions

View File

@ -1,6 +1,6 @@
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy as _
from apps.map.models import CaseStudy, CaseStudyDraft

View File

@ -1,7 +1,8 @@
from django import forms
from django.urls import reverse, reverse_lazy
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from django.utils.text import format_lazy
from django.utils.translation import ugettext_lazy as _
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, HTML, Fieldset, Div
@ -152,6 +153,24 @@ class BootstrapClearableFileInput(forms.ClearableFileInput):
template_name = 'map/forms/widgets/file.html'
def PreviousButton():
return HTML(
format_lazy(
"<a class='btn btn-primary btnPrevious'>{prev}</a>",
prev=_("Previous")
)
)
def NextButton():
return HTML(
format_lazy(
"<a class='btn btn-primary btnNext pull-right'>{next}</a>",
next=_("Next")
)
)
class LongCaseStudyForm(BaseCaseStudyForm):
"""Long version of the CaseStudy form."""
@ -242,39 +261,58 @@ class LongCaseStudyForm(BaseCaseStudyForm):
self.fields['project_owners'].required = True
self.fields['shareholders'].required = True
organising_vs_label = _(
'Socio-environmental impacts (negative and potentially positive)')
organising_pro_label = _(
'Socio-environmental impacts (positive and potentially negative)')
organising_other_label = _(
'Socio-environmental impacts (positive and negative)')
self.fields['socioeconomic_benefits'].label = \
'<span class="organising organising-vs">' + \
_('Socio-environmental impacts (negative and potentially positive)') + \
'</span>' + \
'<span class="organising organising-pro">' + \
_('Socio-environmental impacts (positive and potentially negative)') + \
'</span>' + \
'<span class="organising organising-none organising-idk">' + \
_('Socio-environmental impacts (positive and negative)') + \
'</span>'
self.fields['socioeconomic_benefits'].label = format_lazy(
(
'<span class="organising organising-vs">{vs}</span>'
'<span class="organising organising-pro">{pro}</span>'
'<span class="organising organising-none organising-idk">{other}</span>'
),
vs=organising_vs_label,
pro=organising_pro_label,
other=organising_other_label
)
organising_other_text = _(
'Please expand on your response given in the full description on page one.'
' For example, for positive impacts you need to go beyond emissions'
' savings, paying rent for land, or complying with environmental or social'
' legislation. For negative impacts you need to focus on substantive'
' impacts on vulnerable groups, violations of land rights or abusive labour'
' practices.')
organising_vs_text = _(
'Please expand on your response given in the description. Note that we aim'
' to focus on violation of land rights / human rights / collective rights,'
' substantive negative impacts on vulnerable groups, aggression / threats /'
' violence severe environmental and/or cultural impacts, abusive labor '
' practices, and corruption / governance issues, but feel free to cover any'
' additional aspect that you consider relevant. Please also describe and'
' analyze socio-environmental impacts that could be presented or considered'
' as positive.')
organising_pro_text = _(
'Please expand on your response given in the description. Please also'
' describe and analyze socio-environmental impacts that could be considered'
' as negative.')
self.fields['socioeconomic_benefits'].help_text = format_lazy(
(
'<span class="organising organising-none organising-idk">{other}</span>'
'<span class="organising organising-vs">{vs}</span>'
'<span class="organising organising-pro">{pro}</span>'
),
vs=organising_vs_text,
pro=organising_pro_text,
other=organising_other_text
)
self.fields['socioeconomic_benefits'].help_text = \
'<span class="organising organising-none organising-idk">' + \
_('Please expand on your response given in the full description on page one.'
' For example, for positive impacts you need to go beyond emissions savings,'
' paying rent for land, or complying with environmental or social legislation.'
' For negative impacts you need to focus on substantive impacts on vulnerable groups, '
' violations of land rights or abusive labour practices.') + \
'</span>' + \
'<span class="organising organising-vs">' + \
_('Please expand on your response given in the description. Note that we aim to' \
' focus on violation of land rights / human rights / collective rights, substantive' \
' negative impacts on vulnerable groups, aggression / threats / violence,' \
' severe environmental and/or cultural impacts, abusive labor practices, and' \
' corruption / governance issues, but feel free to cover any additional aspect' \
' that you consider relevant. Please also describe and analyze socio-environmental' \
' impacts that could be presented or considered as positive.') + \
'</span>' + \
'<span class="organising organising-pro">' + \
_('Please expand on your response given in the description. Please also describe' \
' and analyze socio-environmental impacts that could be considered as negative.') + \
'</span>'
self.helper.form_action = reverse('long-form')
self.helper.layout = Layout(
@ -322,7 +360,7 @@ class LongCaseStudyForm(BaseCaseStudyForm):
'social_media_links'
),
FormActions(
HTML("<a class='btn btn-primary btnNext pull-right'>"+_("Next")+"</a>")
NextButton()
)
),
Tab(
@ -384,24 +422,29 @@ class LongCaseStudyForm(BaseCaseStudyForm):
css_class="manufacturing_questions"
),
FormActions(
HTML("<a class='btn btn-primary btnPrevious'>"+_("Previous")+"</a>"),
HTML("<a class='btn btn-primary btnNext pull-right'>"+_("Next")+"</a>")
PreviousButton(),
NextButton()
)
),
Tab(
_("Socio-environmental analysis"),
HTML(
"<p>" +
_("In the following, we expect the analysis to reflect the"
" perspective of the organization(s) or person(s) describing"
" the case.") + "</p>"
format_lazy(
"<p>{text}</p>",
text=_(
"In the following, we expect the analysis to reflect "
" the perspective of the organization(s) or person(s) "
" describing the case.")
)
),
'positive_or_negative',
Div(
HTML(
"<label class='col-md-3 control-label'>" +
_("What kind of case is this entry about?") +
"</label>"),
format_lazy(
"<label class='col-md-3 control-label'>{text}</label>",
text=_("What kind of case is this entry about?")
)
),
Div(
'positive_case_type',
'negative_case_reasons',
@ -424,8 +467,8 @@ class LongCaseStudyForm(BaseCaseStudyForm):
css_class="common_questions"
),
FormActions(
HTML("<a class='btn btn-primary btnPrevious'>"+_("Previous")+"</a>"),
HTML("<a class='btn btn-primary btnNext pull-right'>"+_("Next")+"</a>")
PreviousButton(),
NextButton()
)
),
Tab(
@ -439,8 +482,8 @@ class LongCaseStudyForm(BaseCaseStudyForm):
'shown_on_other_platforms',
'shown_on_other_platforms_detail',
FormActions(
HTML("<a class='btn btn-primary btnPrevious'>"+_("Previous")+"</a>"),
HTML("<a class='btn btn-primary btnNext pull-right'>"+_("Next")+"</a>")
PreviousButton(),
NextButton()
)
),
Tab(
@ -450,7 +493,7 @@ class LongCaseStudyForm(BaseCaseStudyForm):
'other_documents',
'other_documents_files',
FormActions(
HTML("<a class='btn btn-primary btnPrevious'>"+_("Previous")+"</a>"),
PreviousButton(),
Submit('submit', _('Submit'), css_class="btn-success pull-right")
)
)))

View File

@ -5,7 +5,7 @@ from django.contrib.auth.models import User
from django.contrib.gis.db import models
from django.db import connection
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy as _
from django_extensions.db.fields import AutoSlugField
from django_countries.fields import CountryField
@ -657,7 +657,7 @@ class CaseStudy(models.Model):
# Should be filled in if generation_technology was answered as bio-energy
biomass_detail = models.CharField(
verbose_name=_("Bio-energy feedstock"),
help_text="<div class='text-muted'>" + _(
help_text=_("<div class='text-muted'>"
"<p>Please describe the source of the fuel and how it is processed/used."
" Please consider:</p>\n"
"<ul>\n"
@ -669,7 +669,7 @@ class CaseStudy(models.Model):
"</ul>\n"
"<p>We do not expect users to know this information, but if you do "
" it may be useful to give a fuller picture.</p>"
) + "</div>",
"</div>"),
max_length=200,
blank=True
)

View File

@ -80,7 +80,7 @@
</div>
</a>
<p style="margin-top: 40px">If you are interested in writing a technology assessment or policy analysis to appear on this site, please email <a href="mailto:info@ojuso.org">info@ojuso.org</a>.</p>
<p style="margin-top: 40px">{% trans 'If you are interested in writing a technology assessment or policy analysis to appear on this site, please email <a href="mailto:info@ojuso.org">info@ojuso.org</a>.' %}</p>
</div>