From 769203162cdac3b9459ee7d2ec061104f305b7d8 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sat, 20 Apr 2013 23:09:09 +0000 Subject: [PATCH] GrampsLocale: Wrap locale.format and locale.format_string To concentrate the locale dependency in grampslocale.py svn: r22027 --- gramps/gen/utils/grampslocale.py | 17 ++++++++++++ gramps/plugins/gramplet/agestats.py | 5 ++-- gramps/plugins/gramplet/pedigreegramplet.py | 5 ++-- .../textreport/numberofancestorsreport.py | 7 +++-- gramps/webapp/utils.py | 26 +++++++------------ 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/gramps/gen/utils/grampslocale.py b/gramps/gen/utils/grampslocale.py index 62889d072..1a7107e62 100644 --- a/gramps/gen/utils/grampslocale.py +++ b/gramps/gen/utils/grampslocale.py @@ -915,6 +915,23 @@ class GrampsLocale(object): """ return GrampsType.xml_str(name) + def format(self, format, val, grouping=False, monetary=False): + """ + Format a number in the current numeric locale. See python's + locale.format for details. ICU's formatting codes are + incompatible with locale's, so just use locale.format for now. + """ + return locale.format(format, val, grouping, monetary) + + def format_string(self, format, val, grouping=False): + """ + Format a string in the current numeric locale. See python's + locale.format_string for details. ICU's message formatting codes are + incompatible with locale's, so just use locale.format_string + for now. + """ + return locale.format_string(format, val, grouping) + #------------------------------------------------------------------------- # # Translations Classes diff --git a/gramps/plugins/gramplet/agestats.py b/gramps/plugins/gramplet/agestats.py index 00fe0c50a..cf9262c4f 100644 --- a/gramps/plugins/gramplet/agestats.py +++ b/gramps/plugins/gramplet/agestats.py @@ -24,7 +24,6 @@ This Gramplet shows textual distributions of age breakdowns of various types. """ -import locale from collections import defaultdict from gramps.gen.plug import Gramplet @@ -230,7 +229,7 @@ class AgeStatsGramplet(Gramplet): retval = _("Statistics") + ":\n" retval += " " + _("Total") + ": %d\n" % count retval += " " + _("Minimum") + ": %d\n" % minval - retval += " " + _("Average") + locale.format_string(": %.1f\n", average) + retval += " " + _("Average") + glocale.format_string(": %.1f\n", average) retval += " " + _("Median") + ": %d\n" % median retval += " " + _("Maximum") + ": %d\n" % maxval return retval @@ -279,7 +278,7 @@ class AgeStatsGramplet(Gramplet): len(selected)) procent = (float(len(selected)) / (float(sum(hash.values())))*100) - self.append_text(locale.format("%#5.2f", procent)) + self.append_text(glocale.format("%#5.2f", procent)) self.append_text("\n") i += 1 self.append_text("--------" + diff --git a/gramps/plugins/gramplet/pedigreegramplet.py b/gramps/plugins/gramplet/pedigreegramplet.py index 202a3acb0..3dc2b66a1 100644 --- a/gramps/plugins/gramplet/pedigreegramplet.py +++ b/gramps/plugins/gramplet/pedigreegramplet.py @@ -27,7 +27,6 @@ from __future__ import unicode_literals import cgi -import locale #------------------------------------------------------------------------ # @@ -261,13 +260,13 @@ class PedigreeGramplet(Gramplet): if g == 0: self.link(_("Generation 1"), 'PersonList', handles, tooltip=_("Double-click to see people in generation")) - percent = locale.format( '%.2f', 100) + percent_sign + percent = glocale.format( '%.2f', 100) + percent_sign self.append_text(_(" has 1 of 1 individual (%(percent)s complete)\n") % {'percent': percent}) else: all.extend(handles) self.link(_("Generation %d") % g, 'PersonList', handles, tooltip=_("Double-click to see people in generation %d") % g) - percent = locale.format('%.2f', float(count)/2**(g-1) * 100) + percent_sign + percent = glocale.format('%.2f', float(count)/2**(g-1) * 100) + percent_sign self.append_text(glocale.translation.ngettext( " has %(count_person)d of %(max_count_person)d individuals (%(percent)s complete)\n", " has %(count_person)d of %(max_count_person)d individuals (%(percent)s complete)\n", diff --git a/gramps/plugins/textreport/numberofancestorsreport.py b/gramps/plugins/textreport/numberofancestorsreport.py index 453ad616a..bf6edbeda 100644 --- a/gramps/plugins/textreport/numberofancestorsreport.py +++ b/gramps/plugins/textreport/numberofancestorsreport.py @@ -32,9 +32,6 @@ # #------------------------------------------------------------------------ import copy -from gramps.gen.const import GRAMPS_LOCALE as glocale -_ = glocale.translation.gettext -import locale import math #------------------------------------------------------------------------ @@ -42,6 +39,8 @@ import math # GRAMPS modules # #------------------------------------------------------------------------ +from gramps.gen.const import GRAMPS_LOCALE as glocale +_ = glocale.translation.gettext from gramps.gen.display.name import displayer as global_name_display from gramps.gen.errors import ReportError from gramps.gen.plug.menu import PersonOption @@ -116,7 +115,7 @@ class NumberOfAncestorsReport(Report): gen += 1 theoretical = math.pow(2, ( gen - 1 ) ) total_theoretical += theoretical - percent = '(%s%%)' % locale.format('%3.2f', + percent = '(%s%%)' % glocale.format('%3.2f', ((sum(thisgen.values()) / theoretical ) * 100)) # TC # English return something like: diff --git a/gramps/webapp/utils.py b/gramps/webapp/utils.py index ec289df49..2fdd571d5 100644 --- a/gramps/webapp/utils.py +++ b/gramps/webapp/utils.py @@ -28,7 +28,6 @@ #------------------------------------------------------------------------ from __future__ import print_function -import locale import sys import re import datetime @@ -71,8 +70,13 @@ from gramps.gen.utils.db import get_birth_or_fallback, get_death_or_fallback from gramps.gen.plug import BasePluginManager from gramps.cli.grampscli import CLIManager from gramps.gen.constfunc import STRTYPE +from gramps.gen.utils.grampslocale import GrampsLocale -_ = lambda msg: msg +#FIXME: A locale should be obtained from the user and used to +#initialize the locale. Passing in lang and language parameters to the +#constructor prevents querying the environment. +glocale = GrampsLocale(lang='en_US.UTF-8', languages=['en']) +_ = glocale.translation.gettext TAB_HEIGHT = 200 @@ -151,22 +155,12 @@ def probably_alive(handle): return alive(person, db) def format_number(number, with_grouping=True): - # FIXME: should be user's setting - try: - locale.setlocale(locale.LC_ALL, "en_US.utf8") - except: - pass - if number != "": - return locale.format("%d", number, with_grouping) + if number != "": + return glocale.format("%d", number, with_grouping) else: - return locale.format("%d", 0, with_grouping) + return glocale.format("%d", 0, with_grouping) def table_count(table, with_grouping=True): - # FIXME: should be user's setting - try: - locale.setlocale(locale.LC_ALL, "en_US.utf8") - except: - pass if table == "person": number = models.Person.objects.count() elif table == "family": @@ -189,7 +183,7 @@ def table_count(table, with_grouping=True): number = models.Tag.objects.count() else: return "[unknown table]" - return locale.format("%d", number, with_grouping) + return glocale.format("%d", number, with_grouping) def nbsp(string): """