1

I am working on printing dates in more than a language. With Drupal 7, I used the following code.

<span class="arabic-date">
 <?php
   $arrayurdudays = array('', '', '', '', '', '', '', '');
   print format_date(time(), 'custom', ' l , d F , Y', null, 'ur');
 ?>
</span>  

With Drupal 8 and Twig, I tried more than one way and it didn't work for me.

I downloaded the Configuration Translation module, but it didn't work in my case; I read there is a way to print it inside a file (.theme), but I didn't understand the method well. I need it inside the header file.

Figure previously in Drupal 7

In Drupal 8

  • 1
    Does this help? Twig date() documentation – sonfd Jul 8 at 11:10
  • This works in the names of days and months in English, but I need to display them in the names of days and months in Urdu – Mr.TAREQ Jul 8 at 12:55
1

In a Twig template, you can use the format_date() filter. For example, in the node template, the following code could be used to show (using a custom format) when a node has been created. (The following code is a simplified version of the code used from the Unami theme used with the Unami Demo installation profile, in node.html.twig.)

{% set created_date = node.getCreatedTime|format_date('custom', 'l, d F, Y', null, 'ur') %}

{% if display_submitted %}
  <footer class="node__meta">
    <div{{ author_attributes.addClass('node__submitted') }}>
      {% trans %}<span class="by-author">by {{ author_name }}</span> {{ created_date }}{% endtrans %}
    </div>
  </footer>
{% endif %}

I would rather avoid passing a language code to format_date() to let the filter use the language set for the page. Setting the language code would make the template file always use that, even if the page is using a different language code.

{% set created_date = node.getCreatedTime|format_date('custom', 'l, d F, Y') %}

{% if display_submitted %}
  <footer class="node__meta">
    <div{{ author_attributes.addClass('node__submitted') }}>
      {% trans %}<span class="by-author">by {{ author_name }}</span> {{ created_date }}{% endtrans %}
    </div>
  </footer>
{% endif %}

Note that when the custom format is using M for the month, the month name could be not correctly translated. See Date formats with abbreviated month name (M) are not translated using context for more details.

| improve this answer | |

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.