enable deferred translation of date format strings

This commit is contained in:
Paul Franklin 2017-02-22 21:29:13 -08:00
parent 1c4d27c2a9
commit 4541027dc3
3 changed files with 25 additions and 15 deletions

View File

@ -44,6 +44,10 @@ from . import _grampslocale
from ..utils.grampslocale import GrampsLocale from ..utils.grampslocale import GrampsLocale
from ._datestrings import DateStrings from ._datestrings import DateStrings
# _T_ is a gramps-defined keyword -- see po/update_po.py and po/genpot.sh
def _T_(value): # enable deferred translations (see Python docs 22.1.3.4)
return value
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# DateDisplay # DateDisplay
@ -53,33 +57,31 @@ class DateDisplay:
""" """
Base date display class. Base date display class.
""" """
_locale = GrampsLocale(lang='en_US', languages='en')
_tformat = _grampslocale.tformat _tformat = _grampslocale.tformat
_ = _grampslocale.glocale.translation.sgettext
formats = ( formats = (
# format 0 - must always be ISO # format 0 - must always be ISO
_("YYYY-MM-DD (ISO)"), _T_("YYYY-MM-DD (ISO)"),
# format # 1 - must always be locale-preferred numerical format # format # 1 - must always be locale-preferred numerical format
# such as YY.MM.DD, MM-DD-YY, or whatever your locale prefers. # such as YY.MM.DD, MM-DD-YY, or whatever your locale prefers.
# This should be the format that is used under the locale by # This should be the format that is used under the locale by
# strftime() for '%x'. # strftime() for '%x'.
# You may translate this as "Numerical", "System preferred", or similar. # You may translate this as "Numerical", "System preferred", or similar.
_("date format|Numerical"), _T_("date format|Numerical"),
# Full month name, day, year # Full month name, day, year
_("Month Day, Year"), _T_("Month Day, Year"),
# Abbreviated month name, day, year # Abbreviated month name, day, year
_("MON DAY, YEAR"), _T_("MON DAY, YEAR"),
# Day, full month name, year # Day, full month name, year
_("Day Month Year"), _T_("Day Month Year"),
# Day, abbreviated month name, year # Day, abbreviated month name, year
_("DAY MON YEAR") _T_("DAY MON YEAR")
) )
""" """
.. note:: Will be overridden if a locale-specific date displayer exists. .. note:: Will be overridden if a locale-specific date displayer exists.
@ -90,7 +92,6 @@ class DateDisplay:
This ``formats`` must agree with This ``formats`` must agree with
:meth:`~_display_calendar`/:meth:`~_display_gregorian`. :meth:`~_display_calendar`/:meth:`~_display_gregorian`.
""" """
del _
newyear = ("", "Mar1", "Mar25", "Sep1") newyear = ("", "Mar1", "Mar25", "Sep1")
@ -728,4 +729,4 @@ class DateDisplayEn(DateDisplay):
display = DateDisplay.display_formatted display = DateDisplay.display_formatted
_locale = DateDisplay._locale # normally set in register_datehandler _locale = GrampsLocale(languages='en') # no register_datehandler here

View File

@ -34,6 +34,7 @@ import time
# Gramps modules # Gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..const import GRAMPS_LOCALE as glocale
from ..lib.date import Date from ..lib.date import Date
from . import LANG_TO_DISPLAY, LANG, parser, displayer from . import LANG_TO_DISPLAY, LANG, parser, displayer
@ -42,14 +43,22 @@ from . import LANG_TO_DISPLAY, LANG, parser, displayer
# Convenience functions # Convenience functions
# #
#-------------------------------------------------------------- #--------------------------------------------------------------
def get_date_formats(): def get_date_formats(flocale=glocale):
""" """
Return the list of supported formats for date parsers and displayers. Return the list of supported formats for date parsers and displayers.
The UI language formats will be used unless another locale is fed in.
:param flocale: allow deferred translation of date formats
:type flocale: a :class:`.GrampsLocale` instance
""" """
# trans_text is a defined keyword (see po/update_po.py, po/genpot.sh)
trans_text = flocale.translation.sgettext
try: try:
return LANG_TO_DISPLAY[LANG].formats return tuple(trans_text(fmt)
for fmt in LANG_TO_DISPLAY[flocale.lang].formats)
except: except:
return LANG_TO_DISPLAY["C"].formats return tuple(trans_text(fmt)
for fmt in LANG_TO_DISPLAY['C'].formats)
def set_format(value): def set_format(value):
try: try:

View File

@ -30,8 +30,8 @@ from ...lib.date import Date
class DateDisplayTest(unittest.TestCase): class DateDisplayTest(unittest.TestCase):
def setUp(self): def setUp(self):
from .._datedisplay import DateDisplay from .._datedisplay import DateDisplayEn
self.display = DateDisplay() self.display = DateDisplayEn()
self.display_RU = GrampsLocale(lang='ru').date_displayer self.display_RU = GrampsLocale(lang='ru').date_displayer
def assert_map_key_val(self, m, k, v): def assert_map_key_val(self, m, k, v):