2

I am kind of lost here for hours, trying to make a translated date format work for my content type(Drupal 7.22). There are the 2 configuration issues:

1. Format

I want my formated date (say Apr 27) to be wrapped as following:

<span class=date>
  <span class=month> Apr </span>
  <span class=day> 27 </span>
</span>

Although I create the date format I can't find the way to template it the way I want. The month and date shown in these spans is set when I create a content type (through a custom field populated with the help of date module), and so I should do sth like <?php echo render($content['field_date']); ?>. Unfortunately this renders the date with a different markup than the one I want and so I believe I have to change sth in a preprocess function.

2.Translation

Say I have a date: Apr 27 and I want it to be in my language (greek) which would be Απρ 27.

I imported the greek http://localize.drupal.org/ successfully and inside this .po I can see

msgid "Apr"
msgstr "Απρ"

After a very helpful answer and a good comment below I now understand that translation isn't the way to go, since I only want to change the values of the months, and not translate the whole site. So what I look for is a function to do this.

P.S. I could make 2 seperate questions but I feel that they fit well under the title: configuring date formats. Also I have been trying for hours to find clear(to me at least) solutions and it proved harder than I thought. Can anyone help?

  • I sincerely dont understand why such a question would be downvoted. It describes 2 specific, common case scenarios of drupal theming, it may be a lot useful to others, especially those new searching for theming guidelines. Back to the point, I went to /admin/config/regional/language, and added the second language(greek). Do I have to make it default (apart from enabling) in order for the translation of the months, provided inside the el.po file to take place? Truth is I want to only translate the months in the date format, not the whole site – Vagelis Kostopoulos May 27 '13 at 13:41
  • By default, Drupal translations will affect the entire site. If you want to translate only the months and nothing else you are better off writing your own custom function that does not make use of Drupal's built-in translation system. The alternative would be not to import any .po files and create your own translation and only translate the months. Then you would have to make this custom translation the default language of the site. – Patrick Kenny May 27 '13 at 13:49
  • thank you, I am trying to find a way to achieve one of the 2 options you gave, namely the custom function or my custom translation. – Vagelis Kostopoulos May 27 '13 at 14:18
1

Translations in Drupal are global, and will effect the entire site when you enable a non-english default language. However, for translations to be acted upon, you need to wrap the term you want translated in the function t().

So to have 'Apr' in the snipped below translated, you must do something like this:

<span class=date>
  <span class=month> <?php echo t('Apr'); ?> </span>
  <span class=day> 27 </span>
</span>

However, if you do not want to use Drupal' localization machinery, and just display the 4th month in greek, you may do something like this:

<span class=date>
  <span class=month> Απρ </span>
  <span class=day> 27 </span>
</span>

Also note Drupal let you define standard date format string if you navigate to Home » Administration » Configuration » Regional and language » Date and time. These get localized to your site's language when you set language, and have the added advantage of enforcing consistent date formats throughout the site.

  • thank you for the answer but I want the echoed values to be dynamic and not static. The month and date shown in these spans is set when I create a content type (through a custom field populated with the help of date module), and so I should do sth like <?php echo render($content['field_date']); ?>. Unfortunately this renders the date with a different markup than the one I want and so I believe I have to change sth in a preprocess function. I'll add it to the question for clarity – Vagelis Kostopoulos May 27 '13 at 13:54
0

after some searching and experimenting, I came up with this solution for my first problem:

  • I went to my theme in the template.php file
  • I created a function:

    <?php  //if the opening php tag exists already, then don't put it again here :)
    
    function awesome_preprocess_node(&$variables) {
    
    // checking if this field_mydate has a value
    if (isset($variables['field_mydate']['und'][0]['value'])) {
    
      //if it has, let's cache this point down the path
      $date1 = $variables['field_mydate']['und'][0]; 
    
      // I create a instance of the DateObject here. I pass the $date1['value'] which
      // corresponds to the date-path produced by my custom field of type: date. I found 
      // this path with the help of helper function: kpr($variables);
    
      $date_object = new DateObject($date1['value'], new DateTimeZone($date1['timezone_db']));
      $date_object->setTimezone(new DateTimeZone($date1['timezone']));
    
      // Using the following 2 calls of function date_format_date I manage to
      // seperate month and day. 
      // These variables will be available for my .tpl.php files
    
      $variables['date_month'] = date_format_date($date_object,'custom','M');
      $variables['date_day'] = date_format_date($date_object,'custom','j');
      }
    }
    
  • In my node.tpl.php I use

    <span class="date">
      <span class="day"> <?php print $date_day; ?> </span>
      <span class="month"> <?php print $date_month; ?> </span>  
    </span>
    

(take into account that 'awesome' is the machine name of my custom theme. If someone else uses it, they'll have to provide their own theme name. Also the 'field_mydate' is the machine name of the date field I created. This will also need changing for another person's needs)

Although this does the trick, it seems (a bit) hacky and also it doesn't seem to load the [attributes] that used to be there before. Is there a better, clearer solution?

maybe overriding theme_date_display_single ?! or

maybe using sth like $event = node_load($nid); inside the preprocess function to get the date object from there instead of creating a new one is preferable ?!

As for my second question, I see nothing else than translating the whole site

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.