misc: use pgettext for contextual translations (#68202)

This commit is contained in:
Frédéric Péters 2022-08-16 23:41:14 +02:00
parent 8c63a16ff0
commit 8e20fc4e22
8 changed files with 55 additions and 50 deletions

View File

@ -28,7 +28,7 @@ from wcs.carddef import CardDef
from wcs.categories import Category
from wcs.formdef import DRAFTS_DEFAULT_LIFESPAN, FormDef, FormdefImportError, FormdefImportRecoverableError
from wcs.forms.root import qrcode
from wcs.qommon import _, force_str, misc, template
from wcs.qommon import _, force_str, misc, pgettext_lazy, template
from wcs.qommon.afterjobs import AfterJob
from wcs.qommon.backoffice.menu import html_top
from wcs.qommon.errors import AccessForbiddenError, TraversalError
@ -49,7 +49,6 @@ from wcs.qommon.form import (
WidgetList,
WysiwygTextWidget,
)
from wcs.qommon.misc import C_
from wcs.qommon.storage import Equal, Null, StrictNotEqual
from wcs.roles import get_user_roles, logged_users_role
from wcs.workflows import Workflow
@ -467,8 +466,8 @@ class OptionsDirectory(Directory):
title=_('User support'),
value=self.formdef.user_support,
options=[
(None, C_('user_support|No'), ''),
('optional', C_('user_support|Optional'), 'optional'),
(None, pgettext_lazy('user_support', 'No'), ''),
('optional', pgettext_lazy('user_support', 'Optional'), 'optional'),
],
)
return self.handle(form, _('User support'))
@ -703,12 +702,14 @@ class FormDefPage(Directory):
r += self.add_option_line(
'options/description',
_('Description'),
self.formdef.description and C_('description|On') or C_('description|None'),
self.formdef.description
and pgettext_lazy('description', 'On')
or pgettext_lazy('description', 'None'),
)
r += self.add_option_line(
'options/keywords',
_('Keywords'),
self.formdef.keywords and self.formdef.keywords or C_('keywords|None'),
self.formdef.keywords and self.formdef.keywords or pgettext_lazy('keywords', 'None'),
)
r += self.add_option_line(
'options/category',
@ -716,7 +717,7 @@ class FormDefPage(Directory):
self.formdef.category_id
and self.formdef.category
and self.formdef.category.name
or C_('category|None'),
or pgettext_lazy('category', 'None'),
)
r += htmltext('</ul>')
r += htmltext('</div>')
@ -781,13 +782,17 @@ class FormDefPage(Directory):
r += self.add_option_line(
'options/confirmation',
_('Confirmation Page'),
self.formdef.confirmation and C_('confirmation page|Enabled') or C_('confirmation page|Disabled'),
self.formdef.confirmation
and pgettext_lazy('confirmation page', 'Enabled')
or pgettext_lazy('confirmation page', 'Disabled'),
)
r += self.add_option_line(
'options/only_allow_one',
_('Limit to one form'),
self.formdef.only_allow_one and C_('limit to one|Enabled') or C_('limit to one|Disabled'),
self.formdef.only_allow_one
and pgettext_lazy('limit to one', 'Enabled')
or pgettext_lazy('limit to one', 'Disabled'),
)
if self.formdef.roles:
@ -795,8 +800,8 @@ class FormDefPage(Directory):
'options/always_advertise',
_('Display to unlogged users'),
self.formdef.always_advertise
and C_('display to unlogged|Enabled')
or C_('display to unlogged|Disabled'),
and pgettext_lazy('display to unlogged', 'Enabled')
or pgettext_lazy('display to unlogged', 'Disabled'),
)
r += self.add_option_line(
@ -811,15 +816,17 @@ class FormDefPage(Directory):
'options/tracking_code',
_('Tracking Code'),
self.formdef.enable_tracking_codes
and C_('tracking code|Enabled')
or C_('tracking code|Disabled'),
and pgettext_lazy('tracking code', 'Enabled')
or pgettext_lazy('tracking code', 'Disabled'),
)
if get_publisher().has_site_option('formdef-captcha-option'):
r += self.add_option_line(
'options/captcha',
_('CAPTCHA for anonymous users'),
self.formdef.has_captcha and C_('captcha|Enabled') or C_('captcha|Disabled'),
self.formdef.has_captcha
and pgettext_lazy('captcha', 'Enabled')
or pgettext_lazy('captcha', 'Disabled'),
)
if get_publisher().has_site_option('formdef-appearance-keywords'):
@ -828,7 +835,7 @@ class FormDefPage(Directory):
_('Appearance'),
self.formdef.appearance_keywords
and self.formdef.appearance_keywords
or C_('appearance|Standard'),
or pgettext_lazy('appearance', 'Standard'),
)
if (
@ -836,20 +843,20 @@ class FormDefPage(Directory):
or self.formdef.lateral_template
or self.formdef.submission_lateral_template
):
template_status = C_('template|Custom')
template_status = pgettext_lazy('template', 'Custom')
else:
template_status = C_('template|None')
template_status = pgettext_lazy('template', 'None')
r += self.add_option_line('options/templates', _('Templates'), template_status, popup=False)
online_status = C_('online status|Active')
online_status = pgettext_lazy('online status', 'Active')
if self.formdef.disabled:
# manually disabled
online_status = C_('online status|Disabled')
online_status = pgettext_lazy('online status', 'Disabled')
if self.formdef.disabled_redirection:
online_status = _('Redirected')
elif self.formdef.is_disabled():
# disabled by date
online_status = C_('online status|Inactive by date')
online_status = pgettext_lazy('online status', 'Inactive by date')
r += self.add_option_line('options/online_status', _('Online Status'), online_status)
r += htmltext('</ul>')
@ -899,7 +906,7 @@ class FormDefPage(Directory):
roles.append(_('Unknown role (%s)') % x)
value = htmltext(', ').join([str(x) for x in roles])
else:
value = C_('roles|None')
value = pgettext_lazy('roles', 'None')
return value
def get_roles_label_and_auth_context(self):

View File

@ -24,8 +24,7 @@ from wcs.carddef import CardDef, get_cards_graph
from wcs.categories import CardDefCategory
from wcs.workflows import Workflow
from ..qommon import _
from ..qommon.misc import C_
from ..qommon import _, pgettext_lazy
from ..qommon.storage import Null, StrictNotEqual
@ -95,7 +94,7 @@ class CardDefPage(FormDefPage):
self.formdef.category_id
and self.formdef.category
and self.formdef.category.name
or C_('category|None'),
or pgettext_lazy('category', 'None'),
)
r += htmltext('</ul>')
r += htmltext('</div>')
@ -168,14 +167,14 @@ class CardDefPage(FormDefPage):
or self.formdef.lateral_template
or self.formdef.submission_lateral_template
):
template_status = C_('template|Custom')
template_status = pgettext_lazy('template', 'Custom')
else:
template_status = C_('template|None')
template_status = pgettext_lazy('template', 'None')
r += self.add_option_line('options/templates', _('Templates'), template_status, popup=False)
if self.formdef.user_support == 'optional':
user_support_status = C_('user_support|Optional')
user_support_status = pgettext_lazy('user_support', 'Optional')
else:
user_support_status = C_('user_support|No')
user_support_status = pgettext_lazy('user_support', 'No')
r += self.add_option_line('options/user_support', _('User support'), user_support_status)
r += htmltext('</ul>')

View File

@ -46,7 +46,7 @@ from wcs.roles import logged_users_role
from wcs.variables import LazyFieldVar, LazyList
from wcs.workflows import ActionsTracingEvolutionPart, WorkflowStatusItem, item_classes, template_on_formdata
from ..qommon import _, errors, ezt, force_str, get_cfg, misc, ngettext, ods, template
from ..qommon import _, errors, ezt, force_str, get_cfg, misc, ngettext, ods, pgettext_lazy, template
from ..qommon.afterjobs import AfterJob
from ..qommon.backoffice.listing import pagination_links
from ..qommon.backoffice.menu import html_top
@ -65,7 +65,7 @@ from ..qommon.form import (
WidgetList,
WysiwygTextWidget,
)
from ..qommon.misc import C_, ellipsize, get_type_name
from ..qommon.misc import ellipsize, get_type_name
from ..qommon.storage import (
And,
Contains,
@ -94,7 +94,7 @@ from .submission import FormFillPage
def get_common_statuses():
return [
('waiting', _('Waiting for an action'), 'waiting'),
('open', C_('formdata|Open'), 'open'),
('open', pgettext_lazy('formdata', 'Open'), 'open'),
('done', _('Done'), 'done'),
('all', _('All'), 'all'),
]
@ -313,7 +313,9 @@ class ManagementDirectory(Directory):
categories = Category.select()
if categories:
Category.sort_by_position(categories)
category_options = [(None, C_('categories|All'), '')] + [(x.id, x.name, x.id) for x in categories]
category_options = [(None, pgettext_lazy('categories', 'All'), '')] + [
(x.id, x.name, x.id) for x in categories
]
category_slugs = (params.get('category_slugs') or '').split(',')
category_slugs = [c.strip() for c in category_slugs if c.strip()]
for i, category in enumerate([c for c in categories if c.url_name in category_slugs]):
@ -335,7 +337,7 @@ class ManagementDirectory(Directory):
SingleSelectWidget,
'submission_channel',
title=_('Channel'),
options=[(None, C_('channel|All'), '')]
options=[(None, pgettext_lazy('channel', 'All'), '')]
+ [(x, y, x) for x, y in FormData.get_submission_channels().items()],
value=params.get('submission_channel'),
)
@ -611,7 +613,9 @@ class ManagementDirectory(Directory):
r += htmltext('<th><span>%s</span></th>') % _('Reference')
r += htmltext('<th data-field-sort-key="receipt_time"><span>%s</span></th>') % _('Created')
r += htmltext('<th data-field-sort-key="last_update_time"><span>%s</span></th>') % _('Last Modified')
r += htmltext('<th data-field-sort-key="user_name"><span>%s</span></th>') % C_('frontoffice|User')
r += htmltext('<th data-field-sort-key="user_name"><span>%s</span></th>') % pgettext_lazy(
'frontoffice', 'User'
)
r += htmltext('<th class="nosort"><span>%s</span></th>') % _('Status')
r += htmltext('</tr></thead>')
r += htmltext('<tbody>')
@ -1165,7 +1169,7 @@ class FormPage(Directory):
filters = [
('waiting', _('Waiting for an action'), None),
('all', _('All'), None),
('pending', C_('formdata|Open'), None),
('pending', pgettext_lazy('formdata', 'Open'), None),
('done', _('Done'), None),
]
for status in waitpoint_status:
@ -2670,7 +2674,7 @@ class FormPage(Directory):
if selected_filter and selected_filter != 'all':
if selected_filter in ('pending', 'waiting'):
applied_filters = ['wf-%s' % x.id for x in self.formdef.workflow.get_not_endpoint_status()]
criteria_label = _('Status: %s') % C_('formdata|Open')
criteria_label = _('Status: %s') % pgettext_lazy('formdata', 'Open')
if selected_filter == 'waiting':
user_roles = [logged_users_role().id]
if get_request().user:

View File

@ -64,6 +64,7 @@ def N_(x):
_ = lazy(gettext, str)
pgettext_lazy = lazy(pgettext, str)
from . import publisher # noqa pylint: disable=wrong-import-position
from .publisher import get_cfg # noqa pylint: disable=wrong-import-position

View File

@ -18,7 +18,7 @@ from django.core.management.commands import makemessages
class Command(makemessages.Command):
xgettext_options = makemessages.Command.xgettext_options + ['--keyword=N_', '--keyword=C_']
xgettext_options = makemessages.Command.xgettext_options + ['--keyword=N_']
def add_arguments(self, parser):
super().add_arguments(parser)

View File

@ -557,11 +557,6 @@ def get_foreground_colour(background_colour):
return fg_colour
def C_(msg):
'''Translates and removes context from message'''
return _(msg).split('|', 1)[1]
def indent_xml(elem, level=0):
# in-place prettyprint formatter
# http://effbot.org/zone/element-lib.htm#prettyprint

View File

@ -27,8 +27,7 @@ from wcs.backoffice.management import FormPage
from wcs.carddef import CardDef
from wcs.categories import Category
from wcs.formdef import FormDef
from wcs.qommon import _, misc
from wcs.qommon.misc import C_
from wcs.qommon import _, misc, pgettext_lazy
from wcs.qommon.storage import Contains, Equal, StrictNotEqual
@ -43,7 +42,7 @@ class IndexView(RestrictedView):
def get(self, request, *args, **kwargs):
categories = Category.select()
categories.sort(key=lambda x: misc.simplify(x.name))
category_options = [{'id': '_all', 'label': C_('categories|All')}] + [
category_options = [{'id': '_all', 'label': pgettext_lazy('categories', 'All')}] + [
{'id': x.url_name, 'label': x.name} for x in categories
]
return JsonResponse(
@ -296,7 +295,7 @@ class FormsCountView(RestrictedView):
field.default_filter_value = '_all'
options = [
('_all', _('All')),
('pending', C_('statistics|Open')),
('pending', pgettext_lazy('statistics', 'Open')),
('done', _('Done')),
]
for status in waitpoint_status:

View File

@ -42,7 +42,7 @@ from .formdef import (
UnknownReferencedErrorMixin,
)
from .mail_templates import MailTemplate
from .qommon import _, ezt, force_str, get_cfg, misc
from .qommon import _, ezt, force_str, get_cfg, misc, pgettext_lazy
from .qommon.form import (
CheckboxWidget,
ConditionWidget,
@ -54,7 +54,7 @@ from .qommon.form import (
WidgetList,
)
from .qommon.humantime import seconds2humanduration
from .qommon.misc import C_, file_digest, get_as_datetime, xml_node_text
from .qommon.misc import file_digest, get_as_datetime, xml_node_text
from .qommon.storage import (
Contains,
Null,
@ -972,7 +972,7 @@ class Workflow(StorableObject):
def get_list_of_roles(self, include_logged_in_users=True):
t = []
t.append(('_submitter', C_('role|User'), '_submitter'))
t.append(('_submitter', pgettext_lazy('role', 'User'), '_submitter'))
for workflow_role in self.roles.items():
t.append(list(workflow_role) + [workflow_role[0]])
if include_logged_in_users:
@ -2755,7 +2755,7 @@ def get_role_translation_label(workflow, role_id):
if role_id == logged_users_role().id:
return logged_users_role().name
if role_id == '_submitter':
return C_('role|User')
return pgettext_lazy('role', 'User')
if str(role_id).startswith('_'):
return workflow.roles.get(role_id)
else: