enable translated output for this report

svn: r22208
This commit is contained in:
Paul Franklin 2013-05-09 05:03:52 +00:00
parent ce44a4e57a
commit d8e3e0fb6f
2 changed files with 94 additions and 51 deletions

View File

@ -4,6 +4,7 @@
#
# Copyright (C) 2008-2011 Reinhard Müller
# Copyright (C) 2010 Jakim Friant
# Copyright (C) 2013 Paul Franklin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -28,19 +29,47 @@
#
#------------------------------------------------------------------------
import datetime
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
#------------------------------------------------------------------------
#
# GRAMPS modules
#
#------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
from gramps.gen.lib import (ChildRefType, Date, Span, Name, StyledText,
StyledTextTag, StyledTextTagType)
from gramps.gen.display.name import displayer as name_displayer
from gramps.gen.utils.alive import probably_alive
#------------------------------------------------------------------------
#
# List of records
#
#------------------------------------------------------------------------
def _T_(value): # enable deferred translations (see Python docs 22.1.3.4)
return value
RECORDS = [
(_T_("Youngest living person"), 'person_youngestliving', True),
(_T_("Oldest living person"), 'person_oldestliving', True),
(_T_("Person died at youngest age"), 'person_youngestdied', False),
(_T_("Person died at oldest age"), 'person_oldestdied', True),
(_T_("Person married at youngest age"), 'person_youngestmarried', True),
(_T_("Person married at oldest age"), 'person_oldestmarried', True),
(_T_("Person divorced at youngest age"), 'person_youngestdivorced', False),
(_T_("Person divorced at oldest age"), 'person_oldestdivorced', False),
(_T_("Youngest father"), 'person_youngestfather', True),
(_T_("Youngest mother"), 'person_youngestmother', True),
(_T_("Oldest father"), 'person_oldestfather', True),
(_T_("Oldest mother"), 'person_oldestmother', True),
(_T_("Couple with most children"), 'family_mostchildren', True),
(_T_("Living couple married most recently"), 'family_youngestmarried',True),
(_T_("Living couple married most long ago"), 'family_oldestmarried', True),
(_T_("Shortest past marriage"), 'family_shortest', False),
(_T_("Longest past marriage"), 'family_longest', True)]
#------------------------------------------------------------------------
#
# Global functions
@ -62,7 +91,12 @@ def _find_death_date(db, person):
return event.get_date_object()
return None
def find_records(db, filter, top_size, callname):
def find_records(db, filter, top_size, callname,
trans_text=glocale.translation.sgettext):
"""
@param trans_text: allow deferred translation of strings
@type trans_text: a GrampsLocale sgettext instance
"""
today = datetime.date.today()
today_date = Date(today.year, today.month, today.day)
@ -89,6 +123,7 @@ def find_records(db, filter, top_size, callname):
for person_handle in person_handle_list:
person = db.get_person_from_handle(person_handle)
# FIXME this should check for a "fallback" birth also/instead
birth_ref = person.get_birth_ref()
if not birth_ref:
@ -156,6 +191,7 @@ def find_records(db, filter, top_size, callname):
child = db.get_person_from_handle(child_ref.ref)
# FIXME this should check for a "fallback" birth also/instead
child_birth_ref = child.get_birth_ref()
if not child_birth_ref:
continue
@ -201,7 +237,7 @@ def find_records(db, filter, top_size, callname):
father = db.get_person_from_handle(father_handle)
mother = db.get_person_from_handle(mother_handle)
name = StyledText(_("%(father)s and %(mother)s")) % {
name = StyledText(trans_text("%(father)s and %(mother)s")) % {
'father': _get_styled_primary_name(father, callname),
'mother': _get_styled_primary_name(mother, callname)}
@ -272,7 +308,7 @@ def find_records(db, filter, top_size, callname):
duration, name, 'Family', family.handle, top_size)
#python 3 workaround: assign locals to tmp so we work with runtime version
tmp = locals()
return [(text, varname, tmp[varname])
return [(trans_text(text), varname, tmp[varname])
for (text, varname, default) in RECORDS]
def _record(lowest, highest, value, text, handle_type, handle, top_size):
@ -372,27 +408,3 @@ def _get_styled_primary_name(person, callname, placeholder=False):
"""
return _get_styled(person.get_primary_name(), callname, placeholder)
#------------------------------------------------------------------------
#
# List of records
#
#------------------------------------------------------------------------
RECORDS = [
(_("Youngest living person"), 'person_youngestliving', True),
(_("Oldest living person"), 'person_oldestliving', True),
(_("Person died at youngest age"), 'person_youngestdied', False),
(_("Person died at oldest age"), 'person_oldestdied', True),
(_("Person married at youngest age"), 'person_youngestmarried', True),
(_("Person married at oldest age"), 'person_oldestmarried', True),
(_("Person divorced at youngest age"), 'person_youngestdivorced', False),
(_("Person divorced at oldest age"), 'person_oldestdivorced', False),
(_("Youngest father"), 'person_youngestfather', True),
(_("Youngest mother"), 'person_youngestmother', True),
(_("Oldest father"), 'person_oldestfather', True),
(_("Oldest mother"), 'person_oldestmother', True),
(_("Couple with most children"), 'family_mostchildren', True),
(_("Living couple married most recently"), 'family_youngestmarried', True),
(_("Living couple married most long ago"), 'family_oldestmarried', True),
(_("Shortest past marriage"), 'family_shortest', False),
(_("Longest past marriage"), 'family_longest', True)]

View File

@ -4,6 +4,7 @@
#
# Copyright (C) 2008-2011 Reinhard Müller
# Copyright (C) 2010 Jakim Friant
# Copyright (C) 2013 Paul Franklin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -27,23 +28,28 @@
# Standard Python modules
#
#------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
#------------------------------------------------------------------------
#
# GRAMPS modules
#
#------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
from gramps.plugins.lib.librecords import (RECORDS, find_records,
CALLNAME_DONTUSE, CALLNAME_REPLACE, CALLNAME_UNDERLINE_ADD)
from gramps.gen.plug.docgen import (FontStyle, ParagraphStyle, FONT_SANS_SERIF,
PARA_ALIGN_CENTER, IndexMark, INDEX_TYPE_TOC)
CALLNAME_DONTUSE, CALLNAME_REPLACE,
CALLNAME_UNDERLINE_ADD)
from gramps.gen.plug.docgen import (FontStyle, ParagraphStyle,
FONT_SANS_SERIF, PARA_ALIGN_CENTER,
IndexMark, INDEX_TYPE_TOC)
from gramps.gen.plug.menu import (BooleanOption, EnumeratedListOption,
FilterOption, NumberOption, PersonOption, StringOption)
FilterOption, NumberOption,
PersonOption, StringOption)
from gramps.gen.plug.report import Report
from gramps.gen.plug.report import utils as ReportUtils
from gramps.gen.plug.report import MenuReportOptions
from gramps.gen.plug.report import stdoptions
from gramps.gen.lib import Span
from gramps.gen.constfunc import cuni
#------------------------------------------------------------------------
@ -70,44 +76,70 @@ class RecordsReport(Report):
for (text, varname, default) in RECORDS:
self.include[varname] = menu.get_option_by_name(varname).get_value()
self._lang = options.menu.get_option_by_name('trans').get_value()
self._locale = self.set_locale(self._lang)
def write_report(self):
"""
Build the actual report.
"""
records = find_records(self.database, self.filter, self.top_size, self.callname)
records = find_records(self.database, self.filter, self.top_size,
self.callname, trans_text=self._)
self.doc.start_paragraph('REC-Title')
title = _("Records")
title = self._("Records")
mark = IndexMark(title, INDEX_TYPE_TOC, 1)
self.doc.write_text(title, mark)
self.doc.end_paragraph()
if self._lang == 'default':
self.doc.start_paragraph('REC-Subtitle')
self.doc.write_text(self.filter.get_name())
self.doc.end_paragraph()
else:
# The only way which I thought of to get a desired non-English
# filter name if the starting UI is a non-English one, was to
# change ReportUtils.get_person_filters so that it creates the
# filters with English (untranslated) names, but that would mean
# changing every filter.get_name() in every place that the
# get_person_filters filters are used, to always translate the
# get_name, and I wasn't in the mood to do that to all of them,
# so until that happen -- assuming it works, since I didn't try
# it to see, since the person's name will be in get_person_filters
# but the deferred translation will be where the filter.get_name()
# is, so it might not work at all -- but until it does, or another
# way is found, there will be no translated subtitle, only a
# subtitle if the report's output is in the main/UI language
pass # FIXME
for (text, varname, top) in records:
if not self.include[varname]:
continue
self.doc.start_paragraph('REC-Heading')
self.doc.write_text(text)
self.doc.write_text(self._(text))
self.doc.end_paragraph()
last_value = None
rank = 0
for (number, (sort, value, name, handletype, handle)) in enumerate(top):
for (number,
(sort, value, name, handletype, handle)) in enumerate(top):
# FIXME check whether person or family, if a family mark BOTH
person = self.database.get_person_from_handle(handle)
mark = ReportUtils.get_person_mark(self.database, person)
if value != last_value:
last_value = value
rank = number
self.doc.start_paragraph('REC-Normal')
self.doc.write_text(_("%(number)s. ") % {'number': rank+1})
# FIXME this won't work for RTL languages:
self.doc.write_text(self._("%(number)s. ") % {'number': rank+1})
self.doc.write_markup(cuni(name), name.get_tags(), mark)
self.doc.write_text(_(" (%(value)s)") % {'value': str(value)})
if isinstance(value, Span):
tvalue = value.get_repr(dlocale=self._locale)
else:
tvalue = value
self.doc.write_text(" (%s)" % tvalue)
self.doc.end_paragraph()
self.doc.start_paragraph('REC-Footer')
@ -163,15 +195,16 @@ class RecordsReportOptions(MenuReportOptions):
footer = StringOption(_("Footer text"), "")
menu.add_option(category_name, "footer", footer)
stdoptions.add_localization_option(menu, category_name)
for (text, varname, default) in RECORDS:
option = BooleanOption(text, default)
option = BooleanOption(_(text), default)
if varname.startswith('person'):
category_name = _("Person Records")
elif varname.startswith('family'):
category_name = _("Family Records")
menu.add_option(category_name, varname, option)
def __update_filters(self):
"""
Update the filter list based on the selected person
@ -181,7 +214,6 @@ class RecordsReportOptions(MenuReportOptions):
filter_list = ReportUtils.get_person_filters(person, False)
self.__filter.set_filters(filter_list)
def __filter_changed(self):
"""
Handle filter change. If the filter is not specific to a person,
@ -195,7 +227,6 @@ class RecordsReportOptions(MenuReportOptions):
# The rest don't
self.__pid.set_available(False)
def make_default_style(self, default_style):
#Paragraph Styles