diff --git a/gramps/gen/plug/report/__init__.py b/gramps/gen/plug/report/__init__.py index d845287aa..c8f6d3596 100644 --- a/gramps/gen/plug/report/__init__.py +++ b/gramps/gen/plug/report/__init__.py @@ -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 diff --git a/gramps/gen/plug/report/_reportbase.py b/gramps/gen/plug/report/_reportbase.py index ea3c32555..1bd3b27eb 100644 --- a/gramps/gen/plug/report/_reportbase.py +++ b/gramps/gen/plug/report/_reportbase.py @@ -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 diff --git a/gramps/gen/plug/report/stdoptions.py b/gramps/gen/plug/report/stdoptions.py new file mode 100644 index 000000000..a84d9e5b8 --- /dev/null +++ b/gramps/gen/plug/report/stdoptions.py @@ -0,0 +1,51 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright 2013 John Ralls +# +# 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) diff --git a/gramps/plugins/textreport/ancestorreport.py b/gramps/plugins/textreport/ancestorreport.py index 089459b16..5b0bd12ef 100644 --- a/gramps/plugins/textreport/ancestorreport.py +++ b/gramps/plugins/textreport/ancestorreport.py @@ -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): """ diff --git a/gramps/plugins/textreport/detancestralreport.py b/gramps/plugins/textreport/detancestralreport.py index b5291b2de..05a681ccd 100644 --- a/gramps/plugins/textreport/detancestralreport.py +++ b/gramps/plugins/textreport/detancestralreport.py @@ -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 diff --git a/gramps/plugins/textreport/detdescendantreport.py b/gramps/plugins/textreport/detdescendantreport.py index b536737aa..087c54c06 100644 --- a/gramps/plugins/textreport/detdescendantreport.py +++ b/gramps/plugins/textreport/detdescendantreport.py @@ -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