Confusing with "toLocalizedTime" Helper Method

Hi,

in my Template i use the following snippet to display the Date:

<span
    tal:define="
        toLocalizedTime nocall: context/@@plone/toLocalizedTime;
        item_startdate python:toLocalizedTime(newsitem.date, long_format=0);"
        tal:content="item_startdate">Date</span>

toLocalizedTime is here: https://github.com/plone/Products.CMFPlone/blob/master/Products/CMFPlone/browser/ploneview.py#L27

and call toLocalizedTime from Translation Service in https://github.com/plone/Products.CMFPlone/blob/master/Products/CMFPlone/i18nl10n.py#L107

I have define a plonelocales.po in my AddOn in locales/LC_MESSAGES/de to override the plone.app.locales which is defined in plone.app.locales/plone/app/locales/locales/de/LC_MESSAGES/plonelocales.po

msgid "date_format_long"
msgstr "${d}.${B}.${Y} ${H}:${M}"

msgid "date_format_short"
msgstr "${d}.${B}.${Y}"

My Site Configuration:

Products.CMFPlone.i18nl10n.override_dateformat.date_format_long %d. %B %Y %H:%M
Products.CMFPlone.i18nl10n.override_dateformat.date_format_short %d. %B %Y
The default Language of the Site is set to German.

2 Situations:
First:
In the Registry controlpanel, i enable the option
Products.CMFPlone.i18nl10n.override_dateformat

Second:
In the Registry controlpanel, i disable the option
Products.CMFPlone.i18nl10n.override_dateformat

I would like to display the name of month in german, but it didn't. No matter which configuration is present. My last idea is to write a helperview which replace the Name of Month via datestr.replace("Name1","Name2"), but i think that is not the right way. Any Ideas?

Short answer (but quick and dirty): https://pypi.python.org/pypi/experimental.ulocalized_time
I developed it for Plone 3, it was working on Plone 4 also (not sure on Plone 5, but probably works too).

PS: the key Products.CMFPlone.i18nl10n.override_dateformat is new to me, how is intended to be used?

Ok, i use Plone 5. In ulocalized_time it would checked the right format. I think it's a override mechanism for whatever.:confused: You can split in normal time.strftime or the the defined dateformat via date_format_long and date_format_short.

Thats the definition in i18nl10n.py

    # 1. if our Enabled flag in the configuration registry is set,
    # the format string there should override the translation machinery
    formatstring = get_formatstring_from_registry(msgid)
    if formatstring is not None:
        return time.strftime(formatstring)

    # 2. the normal case: translation machinery,
    # that is the ".../LC_MESSAGES/plonelocales.po" files
    formatstring = translate(msgid, domain, mapping, request,
                             target_language=target_language)

    # 3. if both failed, fall back to hardcoded ISO style
    if formatstring == msgid:
        if msgid == 'date_format_long':
            formatstring = '%Y-%m-%d %H:%M'  # 2038-01-19 03:14
        elif msgid == 'date_format_short':
            formatstring = '%Y-%m-%d'  # 2038-01-19
        elif msgid == 'time_format':
            formatstring = '%H:%M'  # 03:14
        else:
            formatstring = '[INTERNAL ERROR]'
        return time.strftime(formatstring)

some time ago I wrote this to solve a similar problem:

1 Like

May I heartily recommend to use the pat-moment js thingie when on Plone 5? It's so much easier

3 Likes

Are your custom translations available? In Plone 4, I had to add my translation package in the zcml line of my instance part

[instance]

...
zcml = translation.package
...