[r22027]GrampsLocale: Wrap locale.format and locale.format_string

To concentrate the locale dependency in grampslocale.py

svn: r22044
This commit is contained in:
John Ralls 2013-04-20 23:22:59 +00:00
parent 7c2469a3ca
commit 9284223d4b
5 changed files with 34 additions and 26 deletions

View File

@ -915,6 +915,23 @@ class GrampsLocale(object):
""" """
return GrampsType.xml_str(name) 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 # Translations Classes

View File

@ -24,7 +24,6 @@
This Gramplet shows textual distributions of age breakdowns of various types. This Gramplet shows textual distributions of age breakdowns of various types.
""" """
import locale
from collections import defaultdict from collections import defaultdict
from gramps.gen.plug import Gramplet from gramps.gen.plug import Gramplet
@ -230,7 +229,7 @@ class AgeStatsGramplet(Gramplet):
retval = _("Statistics") + ":\n" retval = _("Statistics") + ":\n"
retval += " " + _("Total") + ": %d\n" % count retval += " " + _("Total") + ": %d\n" % count
retval += " " + _("Minimum") + ": %d\n" % minval 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 += " " + _("Median") + ": %d\n" % median
retval += " " + _("Maximum") + ": %d\n" % maxval retval += " " + _("Maximum") + ": %d\n" % maxval
return retval return retval
@ -279,7 +278,7 @@ class AgeStatsGramplet(Gramplet):
len(selected)) len(selected))
procent = (float(len(selected)) / procent = (float(len(selected)) /
(float(sum(hash.values())))*100) (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") self.append_text("\n")
i += 1 i += 1
self.append_text("--------" + self.append_text("--------" +

View File

@ -27,7 +27,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import cgi import cgi
import locale
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #
@ -261,13 +260,13 @@ class PedigreeGramplet(Gramplet):
if g == 0: if g == 0:
self.link(_("Generation 1"), 'PersonList', handles, self.link(_("Generation 1"), 'PersonList', handles,
tooltip=_("Double-click to see people in generation")) 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}) self.append_text(_(" has 1 of 1 individual (%(percent)s complete)\n") % {'percent': percent})
else: else:
all.extend(handles) all.extend(handles)
self.link(_("Generation %d") % g, 'PersonList', handles, self.link(_("Generation %d") % g, 'PersonList', handles,
tooltip=_("Double-click to see people in generation %d") % g) 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( 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",
" 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",

View File

@ -32,9 +32,6 @@
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import copy import copy
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
import locale
import math import math
#------------------------------------------------------------------------ #------------------------------------------------------------------------
@ -42,6 +39,8 @@ import math
# GRAMPS modules # 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.display.name import displayer as global_name_display
from gramps.gen.errors import ReportError from gramps.gen.errors import ReportError
from gramps.gen.plug.menu import PersonOption from gramps.gen.plug.menu import PersonOption
@ -116,7 +115,7 @@ class NumberOfAncestorsReport(Report):
gen += 1 gen += 1
theoretical = math.pow(2, ( gen - 1 ) ) theoretical = math.pow(2, ( gen - 1 ) )
total_theoretical += theoretical total_theoretical += theoretical
percent = '(%s%%)' % locale.format('%3.2f', percent = '(%s%%)' % glocale.format('%3.2f',
((sum(thisgen.values()) / theoretical ) * 100)) ((sum(thisgen.values()) / theoretical ) * 100))
# TC # English return something like: # TC # English return something like:

View File

@ -28,7 +28,6 @@
#------------------------------------------------------------------------ #------------------------------------------------------------------------
from __future__ import print_function from __future__ import print_function
import locale
import sys import sys
import re import re
import datetime 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.gen.plug import BasePluginManager
from gramps.cli.grampscli import CLIManager from gramps.cli.grampscli import CLIManager
from gramps.gen.constfunc import STRTYPE 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 TAB_HEIGHT = 200
@ -151,22 +155,12 @@ def probably_alive(handle):
return alive(person, db) return alive(person, db)
def format_number(number, with_grouping=True): 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 != "": if number != "":
return locale.format("%d", number, with_grouping) return glocale.format("%d", number, with_grouping)
else: else:
return locale.format("%d", 0, with_grouping) return glocale.format("%d", 0, with_grouping)
def table_count(table, with_grouping=True): 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": if table == "person":
number = models.Person.objects.count() number = models.Person.objects.count()
elif table == "family": elif table == "family":
@ -189,7 +183,7 @@ def table_count(table, with_grouping=True):
number = models.Tag.objects.count() number = models.Tag.objects.count()
else: else:
return "[unknown table]" return "[unknown table]"
return locale.format("%d", number, with_grouping) return glocale.format("%d", number, with_grouping)
def nbsp(string): def nbsp(string):
""" """