0

I'm trying to implement hook_menu() in my module. The problem is, it doesn't add the menu item for English while it does for Turkish. I'm guessing it has something to do with title arguments, but I can't be sure. Below is my code:

function suweb_agi_menu() {
  global $language;
  $lang = $language->language;
  $items = [];
  $items['agi'] = [
    'title' => ($lang == 'tr')?'AGİ (Aile Geçim İndirimi)':'AGİ (Family Status Declaration)',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['suweb_agi_form'],
    'access arguments' => ['access content'],
    'menu_name' => 'menu-sol-menu'
  ];
  return $items;
}

What am I doing wrong?

4
  • What is the goal/logic behind this loop? If you are trying to provide a different title for each language, you are better off using the t() function, and then translate the strings via the Translate UI. That way you don't need a loop, or the suweb_agi_language() function. Commented Aug 18, 2016 at 6:35
  • Please look at my edited function in my OP, which also does not add the menu item for English.
    – Kartagis
    Commented Aug 18, 2016 at 7:24
  • What are the multilingual settings for your sol-menu Menu? Commented Aug 18, 2016 at 7:46
  • Translate and Localize. Menu items with language will allow translations. Menu items without language will be localized. is checked.
    – Kartagis
    Commented Aug 18, 2016 at 7:48

2 Answers 2

1

The code you are using doesn't work simply because hook_menu() is not called all the times. Drupal caches the result of the implementations of hook_menu() and call them when the cache is invalidated, for example, when after a module has been enabled/installed, or disabled/uninstalled. Changing language doesn't invalidate the cache Drupal uses for hook_menu(), so your code would keep using the title in the same language until a module is non installed, for example.

The code you are using is also useless. Just use the following one, and the title would be shown in the same language used for the page, if there is a translation for that language.

function suweb_agi_menu() {
  global $language;
  $lang = $language->language;
  $items = [];
  $items['agi'] = [
    'title' => 'AGİ (Family Status Declaration)',
    'page callback' => 'drupal_get_form',
    'page arguments' => ['suweb_agi_form'],
    'access arguments' => ['access content'],
    'menu_name' => 'menu-sol-menu'
  ];
  return $items;
}

You can always provide a translation for your menu title on admin/config/regional/translate/translate. (It requires the Locale module.)

7
  • I thought clearing the caches was sufficient for any changes you make in your hook_menu implementation.
    – Kartagis
    Commented Aug 18, 2016 at 10:35
  • I think the problem is not with the translation of the title, but with the actual inclusion/addition of a menu item in a menu. @Kartagis : Can you confirm this? Commented Aug 18, 2016 at 10:59
  • How do you mean? How can I confirm this? The menu hasn't been added by me, by the way.
    – Kartagis
    Commented Aug 18, 2016 at 11:01
  • I meant if you can confirm that my understanding of what you ask for is correct; namely that the problem is not with translating the title of the menu item, but actually having the menu item appearing in the menu at all. Cheers Commented Aug 18, 2016 at 11:09
  • Oh yes, the problem is just with adding the menu item for English.
    – Kartagis
    Commented Aug 18, 2016 at 11:16
1

The following code does what you seem to need (screenshots attached). The following modules need to be activated:

  • i18n_menu (part of the i18n)

Further configuration:

  • I added a new language (tr) and set language detection to "Determine the language from the URL (Path prefix or domain)."
  • I added a new menu with machine name (menu-sol-menu) and set the translation mode to "Translate and Localize". Also added the new menu as a block to the header region of the theme.
  • Added the following code in a custom module and activated it.

    function agi_menu() {
      $items = [];
      $items['agi'] = [
        'title' => 'AGI (Family Status Declaration)',
        'page callback' => 'drupal_get_form',
        'page arguments' => ['node_form'],
        'access arguments' => ['access content'],
        'menu_name' => 'menu-sol-menu'
      ];
      return $items;
    }
    

I visited the menu admin page and saw that a new menu item was added:

enter image description here

I checked the language option for that new menu item, it was set to neutral.

enter image description here

I visited the home page of my drupal instance and checked that the menu item appears, for both english (default language) and turkish, using the URL to define the current language.

enter image description here

Finally, I looked in the translate interface for the title of the menu item.

enter image description here

Conclusion: The provided code with the described configuration works as expected by the OP.

Good luck!

N.B.: Especially for the 'title' and 'title callback' properties, the documentation is very clear about how to use them in a multilingual setting

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

8
  • I did %/agi and agi/% as well, and the menu item disappeared.
    – Kartagis
    Commented Aug 18, 2016 at 6:39
  • What was the % argument for? Also, look at my comment below your OP. Why do you need a multiple menu items? Commented Aug 18, 2016 at 6:40
  • Because multiple languages
    – Kartagis
    Commented Aug 18, 2016 at 6:49
  • I did exactly as described, still no :S
    – Kartagis
    Commented Aug 18, 2016 at 11:35
  • Sorry, one step missing. The new menu item language was Turkish for me, and when I changed it to Language Neutral, the item was there for both languages, just not translated.
    – Kartagis
    Commented Aug 18, 2016 at 11:38

Your Answer

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

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