11

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?

5
  • 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, 2014 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, 2014 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, 2014 at 9:47
  • You can file an issue if you wish.
    – Mołot
    Apr 10, 2014 at 10:47
  • 1
    Good suggestion. Done.
    – Felix Eve
    Apr 10, 2014 at 11:29

5 Answers 5

17

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.

2
  • I would add: Only if title callback is not set because by default title callback is t() function
    – milkovsky
    Apr 16, 2014 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, 2014 at 13:00
11

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..

7

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.

6

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

1
  • *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.
    – AKS
    Apr 10, 2014 at 21:00
0

It clearly says on the documentation page for hook_menu() i.e

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7.x

"title": Required. The untranslated title of the menu item.

"description": The untranslated description of the menu item.

So no need to use t() function for both above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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