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?
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.
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.
t('This is a' . 'string that is not translatable')
)t($text_to_translate)
)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.
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
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.