How to localize months and week days in Drupal 7 through the API?

Go To StackoverFlow.com

2

I have a Drupal 7 site and need to output a small calendar control written in PHP. It outputs a month name and table with days. Table has a header with week day names: 'Mon', 'Tue'.

I need to output this with the current site language selected in Drupal. What's the best way of doing this?

2012-04-05 16:54
by mechmsk


1

In Drupal the function used to translate strings is t().

When you enable a language that is not English, the literal strings passed to t() are replaced with the translation available in the translation files. If there isn't a translation for the weekday short names, then on admin/config/regional/translate/translate you can add the translations.

screenshot

As the translation system is implemented in Drupal, it works only when the string passed to t() is a literal string. If the module is calling the function passing one of the following arguments, the string is not translatable.

  • the concatenation between two or more strings (t('This is a' . 'string that is not translatable'))
  • the content of a variable (t($text_to_translate))
  • the result of a function (t(get_string(STRING_ID_ERROR_MESSAGE)))

If the module is hosted on drupal.org, then it can use the translation services provided from http://localize.drupal.org.

2012-04-05 17:48
by kiamlaluno
Thanks a lot, that helpe - mechmsk 2012-04-09 18:40


1

Month names (long format) are not added for translation by default.

This would be an elegant solution to add these:

function monthname_install() {
  $months = array(
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December',
  );

  $options['context'] = 'Long month name';

  foreach($months as $m) {
    t($m, array(), $options);
  }

  drupal_set_message(t('Month names have been made available for translation. The rest is up to the translation team :-)'));
}

See also: https://drupal.org/comment/8251325#comment-8251325

2013-12-07 20:31
by Erlendoos


0

Erlendoos, I tried your solution and didn't work for me.

I finally ended using locale() instead of t(), like this:

function monthname_install() {
  $months = array(
                     'January',
                     'February',
                     'March',
                     'April',
                     'May',
                     'June',
                     'July',
                     'August',
                     'September',
                     'October',
                     'November',
                     'December',
                );

  foreach($months as $m) {
    locale( $m, 'Long month name' );
  }

  drupal_set_message(t('All long month names available for translation.'));
}

https://api.drupal.org/api/drupal/modules%21locale%21locale.module/function/locale/7

After doing the above, I already have the month names for translation.

Note that if you enabled the "Archive" example views, and you place a block or a page by using that view, every name used by the view will be already available for translation. But in a "fresh installed" drupal, you have to run the above in a .install file to be able to translate the strings.

Hope this helps people that, like me, had the same problem and were trying the above solution without success.

2014-03-01 12:11
by sromero
Ads