Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have always used t() for hook_menus title and description like this:

$items['some-path'] = array(
    'title' => t('My Page Title'),
    'description' => t('This is a description about what my page is for'),
    'page callback' => 'profile_user_page',
);

This makes sense to me so users can translate the title and description into different languages if required.

However this comment on a module review on DO says:

No, that's wrong, don't do that - descriptions in hook_menu() should never use t().

What is the reasoning for this and is this actually best practice?

Also if this is true then should we not use t() for the title as well?

share|improve this question
    
Clearly this should not be used then but if you search the hook_menu help page for "description' => t(" there are 6 occurrences so clearly this is a common mistake! –  Felix Eve Apr 10 at 9:33
1  
Common or not, it's not an excuse ;) And these occurrences are in comments - people are free to be mistaken there. –  Mołot Apr 10 at 9:40
1  
@Mołot They are free to be mistaken there, however a lot of new users will copy and paste that code so this will continue to be a common mistake. I think someone with admin privileges on DO should edit those comments to remove the t(). –  Felix Eve Apr 10 at 9:47
    
You can file an issue if you wish. –  Mołot Apr 10 at 10:47
1  
Good suggestion. Done. –  Felix Eve Apr 10 at 11:29

4 Answers 4

up vote 12 down vote accepted

See Strings at well-known places: built-in menus, permissions, log messages and .info files community documentation:

The Drupal 6 and 7 menu system stores menu item titles and descriptions in English. This allows the system to cache the data, but display to users using various languages on demand. For this to work, you should not use t() on the title or description of menu items in your hook_menu() implementation. Additionally, you should attempt to use a literal string (rather than a dynamic string) for these two keys, so the translation template extractor can find the string you used.

Emphasis mine.

share|improve this answer
    
I would add: Only if title callback is not set because by default title callback is t() function –  milkovsky Apr 16 at 10:39
1  
@milkovsky if title callback is set, it should take care about t() and you still should not use it in hook menu() - you know, caching. –  Mołot Apr 16 at 13:00

If you see documentation of hook_menu arguments..

  • "title": Required. The untranslated title of the menu item.
  • "title callback": Function to generate the title; defaults to t(). If you require only the raw string to be output, set this to FALSE.
  • "title arguments": Arguments to send to t() or your custom callback, with path component substitution as described above.
  • "description": The untranslated description of the menu item.

By default title callback is t function.. So it is always translated..

share|improve this answer

You should not use t() in hook_menu() implementations because t() is automatically called later on and doing so would cause double translation.

share|improve this answer
    
*Note that if you add a title callback in the hook_menu, and if it's not t, the title callback is responsible to translate the string. –  AyeshK Apr 10 at 21:00

Since Drupal 6 is not needed anymore.

Please read https://drupal.org/node/140311. Quoting that:

As of 6.x, Drupal internally handles the translation of menu titles and descriptions into the user's local language. Descriptions, if provided, are always translated with t(); there is no way to pass in additional data for placeholder substitution (in D5 and prior, passing in substitutions was a discouraged practice - with this change, the menu system enforces that rule directly). Titles are translated with t() by default, but t()-style string replacement is possible through the use of the new 'title arguments' property. You can also choose to replace t() with your own custom callback.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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