Moodle
  1. Moodle
  2. MDL-27073

Support multi-language content in custom menu

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0
    • Fix Version/s: 2.0.4
    • Component/s: Accessibility
    • Labels:
      None
    • Testing Instructions:
      Hide
      • Install another language pack to the site (for example Czech - Čeština "cs")
      • Enable multi/language filter (content filtering is enough)
      • Go to Site administration > Appearance > Themes > Theme settings and define the custom menu using a multi-lang syntax (see my comments below for some example using the English/Czech variants of the menu)
      • TEST: make sure that when switching the language, you get relevant custom menu displayed
      Show
      Install another language pack to the site (for example Czech - Čeština "cs") Enable multi/language filter (content filtering is enough) Go to Site administration > Appearance > Themes > Theme settings and define the custom menu using a multi-lang syntax (see my comments below for some example using the English/Czech variants of the menu) TEST: make sure that when switching the language, you get relevant custom menu displayed
    • Affected Branches:
      MOODLE_20_STABLE
    • Fixed Branches:
      MOODLE_20_STABLE
    • Pull from Repository:
    • Pull Master Branch:
      MDL-27073_multilang-custommenu

      Description

      When we are using the new "custommenuitems", we should have the possibility to create the menu for different languages, since we are able to localize everything else.

      1) We could use a notation like this:

      [en-US:Moodle community;pt-PT:Comunidade Moodle]|http://moodle.org
      -[en-US:Moodle free support;pt-PT:Suporte gratuito do Moodle]|http://moodle.org/support
      -[en-US:Moodle development;pt-PT:Desenvolvimento do Moodle]|http://moodle.org/development

      by adding in /lib/outputcomponents.php (around line 2386) something like:
      if (!array_key_exists(0, $bits) || empty($bits[0]))

      { // Every item must have a name to be valid continue; }

      else {
      $bits[0] = ltrim($bits[0],'-');

      + $languages = explode(';', $bits[0]); // pt:nome;en:name|url|title|sort
      + if (array_key_exists(0, $languages)) {
      + foreach ($languages as $language)

      { + $menulang = explode(':', $language); + if ($menulang[0] == current_language() ) + $bits[0] = $menulang[1]; + }

      + }

      2) or using the multilang plugin by adding in /lib/outputcomponents.php (around line 2372) something like:
      public static function convert_text_to_menu_nodes($text) {
      + $text = format_text($text, $format=FORMAT_MARKDOWN, $options=NULL, $courseid=NULL);

      With this line we could use the following syntax to the custommenu text:
      <span lang="en" class="multilang">
      Home|http://moodleurl
      -blah|blahlink
      </span>
      <span lang="pt" class="multilang">
      Início|http://moodleurl
      -blah|blahlink
      </span>

        Gliffy Diagrams

        1. custommenu_without_titles_code.jpg
          171 kB
        2. custommenu_without_titles.jpg
          8 kB
        3. menu-italian.jpg
          46 kB
        4. menu-span.jpg
          210 kB

          Issue Links

            Activity

            Hide
            Rodolfo Matos added a comment -

            I was messing with the code, and in lib/outputcomponents.php around line 2372, we could add just after
            public static function convert_text_to_menu_nodes($text) {
            $text = format_text($text, $format=FORMAT_MARKDOWN, $options=NULL, $courseid=NULL);

            so the multilang plugin could work. Isn't this right?

            Show
            Rodolfo Matos added a comment - I was messing with the code, and in lib/outputcomponents.php around line 2372, we could add just after public static function convert_text_to_menu_nodes($text) { $text = format_text($text, $format=FORMAT_MARKDOWN, $options=NULL, $courseid=NULL); so the multilang plugin could work. Isn't this right?
            Hide
            David Mudrak added a comment -

            Submitting this for the integration. To test, you can use a custom menu definiton like this:

            <span lang="en" class="multilang">
            English community|http://moodle.org|Community site moodle.org
            -Contacts|http://our.school.edu/contact-us
            </span>
            <span lang="cs" class="multilang">
            Česká komunita|http://moodle.cz|Komunitní stránky
            -Kontakty|http://our.school.edu/kontaktujte-nas
            -Demo|http://demo.moodle.cz
            </span>
            

            Show
            David Mudrak added a comment - Submitting this for the integration. To test, you can use a custom menu definiton like this: <span lang="en" class="multilang"> English community|http://moodle.org|Community site moodle.org -Contacts|http://our.school.edu/contact-us </span> <span lang="cs" class="multilang"> Česká komunita|http://moodle.cz|Komunitní stránky -Kontakty|http://our.school.edu/kontaktujte-nas -Demo|http://demo.moodle.cz </span>
            Hide
            David Mudrak added a comment -
            Show
            David Mudrak added a comment - See http://moodle.org/mod/forum/discuss.php?d=177555 for the demand for this
            Hide
            Mary Evans added a comment - - edited

            Hi David,
            I have just implemented the 1st of the two ideas put forward, and got it to work in my latest version of Moodle 2.0.3(build 20110616).

            Here's what I wrote...
            en:Moodle community;it:Moodle comunità|http://moodle.org
            -en:Moodle free support;it:Moodle supporto gratuito|http://moodle.org/support
            -en:Moodle development;it:Moodle sviluppo|http://moodle.org/development
            en:Theme Settings;it:Tema impostazioni;|http://localhost/moodle/admin/settings.php?section=themesettings
            en:Purge Caches;it:Purga la cache|http://localhost/moodle/admin/purgecaches.php
            en:Cron;it:Fare il cron|http://localhost/moodle/admin/cron.php

            It works beautifully! (see attached snapshot) and easy to write too. Personally I think the span, although familiar to multi-lang users, is too long winded, whereas this options is, I think much simpler and easier to do.

            Cheers
            Mary

            Show
            Mary Evans added a comment - - edited Hi David, I have just implemented the 1st of the two ideas put forward, and got it to work in my latest version of Moodle 2.0.3(build 20110616). Here's what I wrote... en:Moodle community;it:Moodle comunità|http://moodle.org -en:Moodle free support;it:Moodle supporto gratuito|http://moodle.org/support -en:Moodle development;it:Moodle sviluppo|http://moodle.org/development en:Theme Settings;it:Tema impostazioni;|http://localhost/moodle/admin/settings.php?section=themesettings en:Purge Caches;it:Purga la cache|http://localhost/moodle/admin/purgecaches.php en:Cron;it:Fare il cron|http://localhost/moodle/admin/cron.php It works beautifully! (see attached snapshot) and easy to write too. Personally I think the span, although familiar to multi-lang users, is too long winded, whereas this options is, I think much simpler and easier to do. Cheers Mary
            Hide
            Mary Evans added a comment -

            Here's a snapshot of the output...in Italian...or course!
            The only bugbear if any, and nothing to do with the menu itself, is the date which I have just noticed shows a ? instead of a letter - why is this??? Is it something to do with my browser settings?

            Show
            Mary Evans added a comment - Here's a snapshot of the output...in Italian...or course! The only bugbear if any, and nothing to do with the menu itself, is the date which I have just noticed shows a ? instead of a letter - why is this??? Is it something to do with my browser settings?
            Hide
            Mary Evans added a comment -

            David,
            I have just been testing the patch you have submitted, using two languages English and Italian, and found that it does not work as expected.

            1. The Menu is displayed in both sets of languages.
            2. In the first set where lang="en" you can see "> which I suspect is part of the HTML writer which closes the anchor tag in the normal custom menu in outputrenderer.php

            I'll attach an image so that you can see what I mean.

            Cheers
            Mary

            Show
            Mary Evans added a comment - David, I have just been testing the patch you have submitted, using two languages English and Italian, and found that it does not work as expected. 1. The Menu is displayed in both sets of languages. 2. In the first set where lang="en" you can see "> which I suspect is part of the HTML writer which closes the anchor tag in the normal custom menu in outputrenderer.php I'll attach an image so that you can see what I mean. Cheers Mary
            Hide
            David Mudrak added a comment -

            Mary, if you see both languages then I believe you either do not have the multilang filter enabled correctly or the syntax of the custom menu definition is not valid.

            Show
            David Mudrak added a comment - Mary, if you see both languages then I believe you either do not have the multilang filter enabled correctly or the syntax of the custom menu definition is not valid.
            Hide
            Mary Evans added a comment -

            In the attached screenshot you can see the source view for the selected part of the menu as seen using Firebug (FireFox add-on)

            Show
            Mary Evans added a comment - In the attached screenshot you can see the source view for the selected part of the menu as seen using Firebug (FireFox add-on)
            Hide
            Mary Evans added a comment -

            The multilang filter is enabled and working ok in a course topic page. The notation for the menu I added to the custommenu settings page is as follows:
            <span lang="en" class="multilang">
            Moodle community|http://moodle.org
            -Moodle free support|http://moodle.org/support
            -Moodle development|http://moodle.org/development
            </span>
            <span lang="it" class="multilang">
            Moodle comunità|http://moodle.org
            -Moodle supporto gratuito|http://moodle.org/support
            -Moodle sviluppo|http://moodle.org/development
            </span>
            .

            Show
            Mary Evans added a comment - The multilang filter is enabled and working ok in a course topic page. The notation for the menu I added to the custommenu settings page is as follows: <span lang="en" class="multilang"> Moodle community|http://moodle.org -Moodle free support|http://moodle.org/support -Moodle development|http://moodle.org/development </span> <span lang="it" class="multilang"> Moodle comunità|http://moodle.org -Moodle supporto gratuito|http://moodle.org/support -Moodle sviluppo|http://moodle.org/development </span> .
            Hide
            Mary Evans added a comment -

            Apologies! I was working on localhost server and had not enabled filter as you suspected. Testing now on web server proved you were right! Which is good thing as I was getting distraught!
            Sorry about that.

            Mary

            Show
            Mary Evans added a comment - Apologies! I was working on localhost server and had not enabled filter as you suspected. Testing now on web server proved you were right! Which is good thing as I was getting distraught! Sorry about that. Mary
            Hide
            Eloy Lafuente (stronk7) added a comment -

            Hi Mary, I've tested this simple script:

            <?
             
            require_once('config.php');
             
            $text =
            '<span lang="en" class="multilang">
            Moodle community|http://moodle.org
            -Moodle free support|http://moodle.org/support
            -Moodle development|http://moodle.org/development
            </span>
            <span lang="es" class="multilang">
            Moodle comunità|http://moodle.org
            -Moodle supporto gratuito|http://moodle.org/support
            -Moodle sviluppo|http://moodle.org/development
            </span>';
             
            $fm = format_text($text, FORMAT_MOODLE, array(
                     'para'     => false,
                     'newlines' => false,
                     'context'  => get_system_context()));
            var_dump($fm);
            

            And it seems to work ok here, and the active lang is picked properly. And that's exactly what David's patch does.

            I only can imagine 2 reasons for this failing in your site:

            • You're getting cached results. Try modifying the text and or disabling cache in filter common settings.
            • To have not the multilang filter enabled by default (On) at system context.

            Can you try the test above and verify the 2 possible reasons commented? I'll halt this until getting some feedback from you.

            TIA!

            Show
            Eloy Lafuente (stronk7) added a comment - Hi Mary, I've tested this simple script: <?   require_once('config.php');   $text = '<span lang="en" class="multilang"> Moodle community|http://moodle.org -Moodle free support|http://moodle.org/support -Moodle development|http://moodle.org/development </span> <span lang="es" class="multilang"> Moodle comunità|http://moodle.org -Moodle supporto gratuito|http://moodle.org/support -Moodle sviluppo|http://moodle.org/development </span>';   $fm = format_text($text, FORMAT_MOODLE, array( 'para' => false, 'newlines' => false, 'context' => get_system_context())); var_dump($fm); And it seems to work ok here, and the active lang is picked properly. And that's exactly what David's patch does. I only can imagine 2 reasons for this failing in your site: You're getting cached results. Try modifying the text and or disabling cache in filter common settings. To have not the multilang filter enabled by default (On) at system context. Can you try the test above and verify the 2 possible reasons commented? I'll halt this until getting some feedback from you. TIA!
            Hide
            Mary Evans added a comment -

            Hi,
            It works fine. It was me...I got it wrong. I was working on localhost server where the Menulang filter was Disabled!

            All is well and it does work...get the thing passed and into Moodle ASAP...

            Cheers
            Mary

            Show
            Mary Evans added a comment - Hi, It works fine. It was me...I got it wrong. I was working on localhost server where the Menulang filter was Disabled! All is well and it does work...get the thing passed and into Moodle ASAP... Cheers Mary
            Hide
            Eloy Lafuente (stronk7) added a comment -

            Thanks!

            This has been integrated now, will land upstream soon.

            Show
            Eloy Lafuente (stronk7) added a comment - Thanks! This has been integrated now, will land upstream soon.
            Hide
            Eloy Lafuente (stronk7) added a comment -

            All git & cvs servers have been updated with these cool changes, so closing, many thanks!

            Show
            Eloy Lafuente (stronk7) added a comment - All git & cvs servers have been updated with these cool changes, so closing, many thanks!
            Hide
            Eloy Lafuente (stronk7) added a comment -

            Hi,

            just to comment I've discovered one regression caused by the code introduced here: MDL-28168

            Ciao

            Show
            Eloy Lafuente (stronk7) added a comment - Hi, just to comment I've discovered one regression caused by the code introduced here: MDL-28168 Ciao
            Hide
            David Mudrak added a comment -

            In the linked MDL-28168, a variant of syntax suggested by Mary Evans is proposed that would replace the <span> based one described in this issue. Note that we need whole menu items being language-dependent as we want the language-specific URL, too.

            Show
            David Mudrak added a comment - In the linked MDL-28168 , a variant of syntax suggested by Mary Evans is proposed that would replace the <span> based one described in this issue. Note that we need whole menu items being language-dependent as we want the language-specific URL, too.
            Hide
            Mary Evans added a comment -

            Are URLs translatable? I thought the 'Lingua Franca' of the WWW was English? Or am I reading your comment wrongly?

            Show
            Mary Evans added a comment - Are URLs translatable? I thought the 'Lingua Franca' of the WWW was English? Or am I reading your comment wrongly?
            Hide
            Jeff Forssell added a comment -

            I think that he just means that sometimes we will want to be able to send one lang to one page (URL) and another lang from the same"place" in the menu to another URL (even though Moodle has an operating multilang filter).

            I think it will often be the case that the menu can be quite different in size/structure for different languages depending on which resources have been prepared.

            Show
            Jeff Forssell added a comment - I think that he just means that sometimes we will want to be able to send one lang to one page (URL) and another lang from the same"place" in the menu to another URL (even though Moodle has an operating multilang filter). I think it will often be the case that the menu can be quite different in size/structure for different languages depending on which resources have been prepared.
            Hide
            David Mudrak added a comment -

            Yup, as Jeff says.

            Show
            David Mudrak added a comment - Yup, as Jeff says.
            Hide
            Jozas Nhial added a comment -

            Is this on Moodle 2.1? Is there documentation of usage somewhere? Thank you.

            Show
            Jozas Nhial added a comment - Is this on Moodle 2.1? Is there documentation of usage somewhere? Thank you.
            Hide
            David Mudrak added a comment -

            Yes Rui, this was added into 2.1 and also into the 2.0.3+. Unfortunately shortly after the release, we realized that the implementation is wrong. So the syntax will be changed soon (in this or next week release of "plus" versions). Then the syntax will be documented at http://docs.moodle.org/20/en/Custom_menu_items

            Show
            David Mudrak added a comment - Yes Rui, this was added into 2.1 and also into the 2.0.3+. Unfortunately shortly after the release, we realized that the implementation is wrong. So the syntax will be changed soon (in this or next week release of "plus" versions). Then the syntax will be documented at http://docs.moodle.org/20/en/Custom_menu_items
            Hide
            Anna Bánsághi added a comment - - edited

            I'm sorry, I've already revoked the comment about custommenu without titles,
            because found my mistake. Attachments are not current too.

            Anna

            Show
            Anna Bánsághi added a comment - - edited I'm sorry, I've already revoked the comment about custommenu without titles, because found my mistake. Attachments are not current too. Anna
            Hide
            Jozas Nhial added a comment -

            Thank you David. Did you manage to document the syntax somewhere? That link seems empty.

            Show
            Jozas Nhial added a comment - Thank you David. Did you manage to document the syntax somewhere? That link seems empty.
            Hide
            Kenny McCormack added a comment -

            Hi David,

            You mentioned above (07/Jul/11 2:18 PM) about release 2.1 having a potential problem. We have a Moodle site at 2.1.1+ on which the syntax unfortunately does not seem to work when you switch language.

            Has the code that does this been disabled for the moment in 2.1.1 builds and if so do you have a timescale on which the implementation will be corrected?

            Many thanks,

            Kenny

            Show
            Kenny McCormack added a comment - Hi David, You mentioned above (07/Jul/11 2:18 PM) about release 2.1 having a potential problem. We have a Moodle site at 2.1.1+ on which the syntax unfortunately does not seem to work when you switch language. Has the code that does this been disabled for the moment in 2.1.1 builds and if so do you have a timescale on which the implementation will be corrected? Many thanks, Kenny
            Hide
            David Mudrak added a comment -

            Ooops sorry I forgot to document the syntax. I just added an example to http://docs.moodle.org/21/en/Custom_menu_items on how you can use multilanguage support for custom menu items in Moodle 2.1 and higher. Thanks for the reminder!

            Show
            David Mudrak added a comment - Ooops sorry I forgot to document the syntax. I just added an example to http://docs.moodle.org/21/en/Custom_menu_items on how you can use multilanguage support for custom menu items in Moodle 2.1 and higher. Thanks for the reminder!
            Hide
            Kenny McCormack added a comment -

            Hi David

            Many thanks that works a treat

            Kenny

            Show
            Kenny McCormack added a comment - Hi David Many thanks that works a treat Kenny
            Hide
            Hanan Keren added a comment -

            Hi Everybody:

            In Moodle 2.3.4 you have to add a function to option 2 to work like this:

            before:

            public static function convert_text_to_menu_nodes($text, $language = null) {
            $text = format_text($text, $format=FORMAT_MARKDOWN, $options=NULL, $courseid=NULL);
            $lines = explode("\n", $text);
            $children = array();

            after:

            public static function convert_text_to_menu_nodes($text, $language = null) {
            $text = format_text($text, $format=FORMAT_MARKDOWN, $options=NULL, $courseid=NULL);
            $lines = explode("\n", $text);
            + $lines = array_slice($lines, 1, -2);
            $children = array();

            Good Day!

            Show
            Hanan Keren added a comment - Hi Everybody: In Moodle 2.3.4 you have to add a function to option 2 to work like this: before: public static function convert_text_to_menu_nodes($text, $language = null) { $text = format_text($text, $format=FORMAT_MARKDOWN, $options=NULL, $courseid=NULL); $lines = explode("\n", $text); $children = array(); after: public static function convert_text_to_menu_nodes($text, $language = null) { $text = format_text($text, $format=FORMAT_MARKDOWN, $options=NULL, $courseid=NULL); $lines = explode("\n", $text); + $lines = array_slice($lines, 1, -2); $children = array(); Good Day!

              People

              • Votes:
                6 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: