1

How to retrieve the postDate of an entry in a different language than the CP. In the general config settings i have the following,

    'defaultCpLanguage' => 'en'

But i would like to display the dates in the frontend in a different language. As in translated month names. Is this possible?

I tried the following with no result:

    {{ entry.postDate | date("d/F/y", "Europe/Paris") }}
    {{entry.postDate.format("d F y")}} #}
    <span class="date">{{entry.postDate | date('d F Y')}}</span>

The format works, but there is no translation.

  • I came across this post: craftcms.stackexchange.com/questions/1127/… is there a better solution around? or is this the only way? So to summarize you should install craft in your native language and after that set the CP to English? – Jacob Oct 29 '15 at 10:42
  • I changed the locale in the database as explained in the question above but now my CP is in the same language as the db i thought defaultCpLanguage would override this...this makes no sense – Jacob Oct 29 '15 at 13:10
  • I don't think I'm following 100%. The language the CP is being displayed in and the locale that is being displayed on the front-end are two separate things. Can you clarify more what you're trying to do? – Brad Bell Oct 29 '15 at 15:32
  • That second parameter you set to "Europe/Paris" in the date filter is to set the timezone, NOT the language! – carlcs Oct 29 '15 at 22:38
2

It looks like you need Craft Pro and even then it's not possible to have the CP in a language that is not installed as a front-end locale.

The (new) defaultCpLanguage setting doesn't change this. See Brad's answers here:
Change system language +
German locale & English CP language resulting in disappeared entries

But to help you with your date problem. It is possible to access most data from craft/app/framework/i18n/data/ via craft.i18n methods.

{% macro dateFormat(date, locale) %}
    {% set localeData = craft.i18n.getLocaleData(locale|default('de')) %}
    {{
        localeData.getWeekdayName(date|date('w')) ~ ', ' ~
        date|date('j ') ~
        localeData.getMonthName(date|date('m')) ~
        date|date(' Y')
    }}
{% endmacro %}
{% import _self as macros %}

{% for locale in ['en', 'es', 'fr', 'de', 'ru', 'nl', 'no'] %}
    <li>{{ macros.dateFormat(now, locale) }}</li> 
{% endfor %}
1

If your looking to just replace month or day names you could do something like this:

{% set translations = {
    'January': 'janvier', 
    'February': 'février', 
    'March':'mars', 
    'April':'avril'} 
 %}    

{{ entry.postDate|date('d F Y')|replace(translations) }}

You could add any amount of translations in that object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.