[r21966]GrampsLocale: Extract 2 reporting functions
set_translation() to the Report class Add a new module stdoptions to gen.plug.report with function add_localization_option(). The intent of stdoptions is to reduce the code-copying among reports; this is the first bit. svn: r21983
This commit is contained in:
parent
eb63ae3d8e
commit
751ac2aef0
@ -35,3 +35,5 @@ from ._bibliography import Bibliography, Citation
|
||||
from ._options import MenuReportOptions, ReportOptions, DocOptions
|
||||
|
||||
from ._book import BookList, Book, BookItem, append_styles
|
||||
|
||||
import _stdoptions as stdoptions
|
||||
|
@ -28,6 +28,8 @@
|
||||
# Report
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
from gramps.plugins.lib.libtranslate import Translator
|
||||
|
||||
class Report(object):
|
||||
"""
|
||||
The Report base class. This is a base class for generating
|
||||
@ -53,7 +55,20 @@ class Report(object):
|
||||
|
||||
def begin_report(self):
|
||||
pass
|
||||
|
||||
|
||||
def set_translation(self, language):
|
||||
"""
|
||||
Set the translator to one selected with
|
||||
stdoptions.add_localization_option().
|
||||
"""
|
||||
if not language:
|
||||
return
|
||||
trans = Translator(lang=language)
|
||||
self._ = trans.gettext
|
||||
self.__get_date = trans.get_date
|
||||
self.__get_type = trans.get_type
|
||||
return trans
|
||||
|
||||
def write_report(self):
|
||||
pass
|
||||
|
||||
|
51
gramps/gen/plug/report/stdoptions.py
Normal file
51
gramps/gen/plug/report/stdoptions.py
Normal file
@ -0,0 +1,51 @@
|
||||
#
|
||||
# Gramps - a GTK+/GNOME based genealogy program
|
||||
#
|
||||
# Copyright 2013 John Ralls <jralls@ceridwen.us>
|
||||
#
|
||||
# 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
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#
|
||||
# $Id$
|
||||
|
||||
"""
|
||||
Commonly used report options. Call the function, don't copy the code!
|
||||
"""
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# gramps modules
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
from gramps.gen.plug.menu import EnumeratedListOption
|
||||
from gramps.gen.const import GRAMPS_LOCALE as glocale
|
||||
_ = glocale.translation.gettext
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# StandardReportOptions
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
|
||||
def add_localization_option(menu, category):
|
||||
"""
|
||||
Insert an option for localizing the report into a different locale
|
||||
from the UI locale.
|
||||
"""
|
||||
trans = EnumeratedListOption(_("Translation"),
|
||||
glocale.DEFAULT_TRANSLATION_STR)
|
||||
trans.add_item(glocale.DEFAULT_TRANSLATION_STR, _("Default"))
|
||||
languages = glocale.get_language_dict()
|
||||
for language in sorted(languages, key=glocale.sort_key):
|
||||
trans.add_item(languages[language], language)
|
||||
trans.set_help(_("The translation to be used for the report."))
|
||||
menu.add_option(category, "trans", trans)
|
@ -32,8 +32,6 @@
|
||||
#------------------------------------------------------------------------
|
||||
import math
|
||||
import copy
|
||||
from gramps.gen.const import GRAMPS_LOCALE as glocale
|
||||
_ = glocale.get_translation().gettext
|
||||
|
||||
#------------------------------------------------------------------------
|
||||
#
|
||||
@ -41,6 +39,7 @@ _ = glocale.get_translation().gettext
|
||||
#
|
||||
#------------------------------------------------------------------------
|
||||
from gramps.gen.const import GRAMPS_LOCALE as glocale
|
||||
_ = glocale.get_translation().gettext
|
||||
from gramps.gen.display.name import displayer as global_name_display
|
||||
from gramps.gen.errors import ReportError
|
||||
from gramps.gen.lib import ChildRefType
|
||||
@ -52,8 +51,8 @@ from gramps.gen.plug.docgen import (IndexMark, FontStyle, ParagraphStyle,
|
||||
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.plugins.lib.libnarrate import Narrator
|
||||
from gramps.plugins.lib.libtranslate import Translator
|
||||
|
||||
#------------------------------------------------------------------------
|
||||
#
|
||||
@ -105,8 +104,6 @@ class AncestorReport(Report):
|
||||
self.center_person = database.get_person_from_gramps_id(pid)
|
||||
if (self.center_person == None) :
|
||||
raise ReportError(_("Person %s is not in the Database") % pid )
|
||||
language = menu.get_option_by_name('trans').get_value()
|
||||
translator = Translator(language)
|
||||
|
||||
# Copy the global NameDisplay so that we don't change application
|
||||
# defaults.
|
||||
@ -115,7 +112,8 @@ class AncestorReport(Report):
|
||||
if name_format != 0:
|
||||
self._name_display.set_default_format(name_format)
|
||||
|
||||
self._ = translator.gettext
|
||||
lang = menu.get_option_by_name('trans').get_value()
|
||||
translator = self.set_translation(lang)
|
||||
self.__narrator = Narrator(self.database, use_fulldate=True,
|
||||
translator=translator)
|
||||
|
||||
@ -296,15 +294,8 @@ class AncestorOptions(MenuReportOptions):
|
||||
namebrk = BooleanOption(_("Add linebreak after each name"), False)
|
||||
namebrk.set_help(_("Indicates if a line break should follow the name."))
|
||||
menu.add_option(category_name, "namebrk", namebrk)
|
||||
|
||||
trans = EnumeratedListOption(_("Translation"),
|
||||
Translator.DEFAULT_TRANSLATION_STR)
|
||||
trans.add_item(Translator.DEFAULT_TRANSLATION_STR, _("Default"))
|
||||
languages = glocale.get_language_dict()
|
||||
for language in sorted(languages, key=glocale.sort_key):
|
||||
trans.add_item(languages[language], language)
|
||||
trans.set_help(_("The translation to be used for the report."))
|
||||
menu.add_option(category_name, "trans", trans)
|
||||
|
||||
stdoptions.add_localization_option(menu, category_name)
|
||||
|
||||
def make_default_style(self, default_style):
|
||||
"""
|
||||
|
@ -35,8 +35,6 @@
|
||||
#
|
||||
#------------------------------------------------------------------------
|
||||
import copy
|
||||
from gramps.gen.const import GRAMPS_LOCALE as glocale
|
||||
_ = glocale.get_translation().gettext
|
||||
|
||||
#------------------------------------------------------------------------
|
||||
#
|
||||
@ -44,6 +42,7 @@ _ = glocale.get_translation().gettext
|
||||
#
|
||||
#------------------------------------------------------------------------
|
||||
from gramps.gen.const import GRAMPS_LOCALE as glocale
|
||||
_ = glocale.get_translation().gettext
|
||||
from gramps.gen.display.name import displayer as global_name_display
|
||||
from gramps.gen.errors import ReportError
|
||||
from gramps.gen.lib import EventType, FamilyRelType, Person, NoteType
|
||||
@ -55,8 +54,8 @@ from gramps.gen.plug.report import ( Report, Bibliography )
|
||||
from gramps.gen.plug.report import endnotes
|
||||
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.plugins.lib.libnarrate import Narrator
|
||||
from gramps.plugins.lib.libtranslate import Translator
|
||||
|
||||
#------------------------------------------------------------------------
|
||||
#
|
||||
@ -159,19 +158,13 @@ class DetAncestorReport(Report):
|
||||
else:
|
||||
empty_place = ""
|
||||
|
||||
language = get_value('trans')
|
||||
translator = Translator(language)
|
||||
self._ = translator.gettext
|
||||
|
||||
self.__narrator = Narrator(self.database, self.verbose, use_call,use_fulldate ,
|
||||
empty_date, empty_place,
|
||||
lang = menu.get_option_by_name('trans').get_value()
|
||||
translator = self.set_translation(lang)
|
||||
self.__narrator = Narrator(self.database, self.verbose, use_call,
|
||||
use_fulldate, empty_date, empty_place,
|
||||
translator=translator,
|
||||
get_endnote_numbers=self.endnotes)
|
||||
|
||||
self.__get_date = translator.get_date
|
||||
|
||||
self.__get_type = translator.get_type
|
||||
|
||||
self.bibli = Bibliography(Bibliography.MODE_DATE|Bibliography.MODE_PAGE)
|
||||
|
||||
def apply_filter(self, person_handle, index):
|
||||
@ -720,8 +713,8 @@ class DetAncestorOptions(MenuReportOptions):
|
||||
from functools import partial
|
||||
|
||||
# Report Options
|
||||
|
||||
addopt = partial(menu.add_option, _("Report Options"))
|
||||
category = _("Report Options")
|
||||
addopt = partial(menu.add_option, category)
|
||||
|
||||
pid = PersonOption(_("Center Person"))
|
||||
pid.set_help(_("The center person for the report"))
|
||||
@ -750,15 +743,8 @@ class DetAncestorOptions(MenuReportOptions):
|
||||
pageben.set_help(
|
||||
_("Whether to start a new page before the end notes."))
|
||||
addopt("pageben", pageben)
|
||||
|
||||
trans = EnumeratedListOption(_("Translation"),
|
||||
Translator.DEFAULT_TRANSLATION_STR)
|
||||
trans.add_item(Translator.DEFAULT_TRANSLATION_STR, _("Default"))
|
||||
languages = glocale.get_language_dict()
|
||||
for language in sorted(languages, key=glocale.sort_key):
|
||||
trans.add_item(languages[language], language)
|
||||
trans.set_help(_("The translation to be used for the report."))
|
||||
addopt("trans", trans)
|
||||
|
||||
stdoptions.add_localization_option(menu, category)
|
||||
|
||||
# Content options
|
||||
|
||||
|
@ -59,8 +59,8 @@ from gramps.gen.plug.report import (Report, Bibliography)
|
||||
from gramps.gen.plug.report import endnotes
|
||||
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.plugins.lib.libnarrate import Narrator
|
||||
from gramps.plugins.lib.libtranslate import Translator
|
||||
|
||||
#------------------------------------------------------------------------
|
||||
#
|
||||
@ -172,9 +172,6 @@ class DetDescendantReport(Report):
|
||||
else:
|
||||
empty_place = ""
|
||||
|
||||
language = get_value('trans')
|
||||
translator = Translator(language)
|
||||
self._ = translator.gettext
|
||||
|
||||
# Copy the global NameDisplay so that we don't change application
|
||||
# defaults.
|
||||
@ -183,15 +180,13 @@ class DetDescendantReport(Report):
|
||||
if name_format != 0:
|
||||
self._name_display.set_default_format(name_format)
|
||||
|
||||
translator = self.set_translation(get_value('trans'))
|
||||
self.__narrator = Narrator(self.database, self.verbose,
|
||||
use_call, use_fulldate,
|
||||
empty_date, empty_place,
|
||||
translator=translator,
|
||||
get_endnote_numbers=self.endnotes)
|
||||
|
||||
self.__get_date = translator.get_date
|
||||
|
||||
self.__get_type = translator.get_type
|
||||
|
||||
self.bibli = Bibliography(Bibliography.MODE_DATE|Bibliography.MODE_PAGE)
|
||||
|
||||
@ -884,8 +879,8 @@ class DetDescendantOptions(MenuReportOptions):
|
||||
"""
|
||||
|
||||
# Report Options
|
||||
|
||||
add_option = partial(menu.add_option, _("Report Options"))
|
||||
category = _("Report Options")
|
||||
add_option = partial(menu.add_option, category)
|
||||
|
||||
pid = PersonOption(_("Center Person"))
|
||||
pid.set_help(_("The center person for the report"))
|
||||
@ -926,14 +921,7 @@ class DetDescendantOptions(MenuReportOptions):
|
||||
_("Whether to start a new page before the end notes."))
|
||||
add_option("pageben", pageben)
|
||||
|
||||
trans = EnumeratedListOption(_("Translation"),
|
||||
Translator.DEFAULT_TRANSLATION_STR)
|
||||
trans.add_item(Translator.DEFAULT_TRANSLATION_STR, _("Default"))
|
||||
languages = glocale.get_language_dict()
|
||||
for language in sorted(languages, key=glocale.sort_key):
|
||||
trans.add_item(languages[language], language)
|
||||
trans.set_help(_("The translation to be used for the report."))
|
||||
add_option("trans", trans)
|
||||
stdoptions.add_localization_option(menu, category)
|
||||
|
||||
# Content
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user