Multilingual link

Multilingual link

by Wonton Sopabuena -
Number of replies: 7

Hello everyone,

I'm working on a Moodle site. I'm using standardwhite theme and I've changed its header.html file to have a logo in the top of the page (including a link). To do this, after reading this forum I changed in that file this:

<h1 echo $heading ?></h1>

<div echo $menu ?></div>

into this:

<div class="headermain"><a href=http://www.myothersite.com><img src="<?php echo $CFG->wwwroot.'/theme/'.current_theme() ?>/mylogo.png" alt="MyOtherSite" title="MyOtherSite" id="logo" /></a></div>

<h1 class="headermain"><a href=http://www.myothersite.com><?php echo “My Other Site” ?></a></h1>

This works. My problem cames in the moment that I have to link to one site when the user has Moodle in English and to other site when the user has Moodle in Spanish.

I've tried checking the "Filter all strings" and using "<span class="multilang" lang="en">...</span><span class="multilang" lang="es">...</span> but it doesn't work.

Is it because "<span class lang>" doesn't work in header.html? Any of you could tell me what could I do to link to one or other site depending on my selected language?

Thank you very much.

Wonton

Average of ratings: -
In reply to Wonton Sopabuena

Re: Multilingual link

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
Hi Wonton,

filters are not applied when you are using plain "echo". Instead, try something like this (not tested, consider it as a scratch):

global $USER;
if (empty($USER->lang)) {
 print_default_link_for_anonymous_users();
} else {
 switch ($USER->lang) {
 case 'en_utf8': print_english_link(); break;
 case 'cs_utf8': print_czech_link(); break;
 // etc.
 }
}
In reply to David Mudrák

Re: Multilingual link

by Wonton Sopabuena -

PERFECT!!!! big grin

It worked very well!!

Thank you very much for your help David!

In reply to Wonton Sopabuena

Re: Multilingual link

by Wonton Sopabuena -

Hello again David!

And sorry for being tedious wink.

I'm using your solution to access Moodle from Drupal in the correct language. My links are:

www.myexample.com/moodle/courses/view.php?id=2&lang=en when Drupal is in English and www.myexample.com/moodle/courses/view.php?id=2&lang=es when Drupal is in Spanish. It works OK.

On the other hand I'm using your solution to make that my logo links to the correct Drupal page:

<div class="headermain"><a href=<?php global $USER; switch ($USER->lang) { case 'es_utf8': echo  http://www.myexample.com/node/2; break; case 'en_utf8': echo http://www.myexample.com/node/1; break;default: echo http://www.myexample.com/node/1; break;} ?>><img src="<?php echo $CFG->wwwroot.'/theme/'.current_theme() ?>/logo.gif" title="My Site" id="logo" /></a></div>

This works OK too, with an exception...

The problem cames when the user logs in Moodle as a guest. In this case the logo links always to node/1 (English) despite I use the URL lang=es (Spanish) solution. I think it is because it gets the default Moodle language (English) ignoring the URL lang=es tag.

Do you know what could I do or where could I see to make that the logo links Drupal depending on the lang URL tag?

Thank you very much again.

In reply to Wonton Sopabuena

Re: Multilingual link

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
Hi Wonto

what version are you using? In 1.9.x, there is a preferred language detection that might be involved here. Try to turn it off. Search for it in the server administration.

Ciao
In reply to David Mudrák

Re: Multilingual link

by Wonton Sopabuena -

Hello again David!

I'm using Moodle 1.9. I've found that option "autodetect language". Acording to the explanation, if it's checked, for a guest, Moodle takes the browser language, if it isn't checked, for a guest, Moodle takes the Moodle's default language.

I've tried with both options and it doesn't work. I've inserted in the header.php file one echo to show the value of $USER->lang, and for a guest is always NULL (""), so with your solution it always go through the default option in the switch.

I'm going to investigate, as you suggested me, the URL parameters issue, to see if in runtime, I can know the URL langa parameter or something like that.

Any other advise? All of your suggestions have been very helpful to me, so thank you and kind regards.

Wonton

In reply to Wonton Sopabuena

Re: Multilingual link

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
OK, try to incorporate additional check based on the following (again, just a scratch):

if (empty($USER->lang)) {
 // user is guest or anonymous
 $lang = optional_param("lang", "en_utf8");
} else {
 $lang = $USER->lang;
}

switch ($lang) {
case 'en_utf8': do_sth(); break;
case 'es_utf8': do_sth(); break;
}
In reply to David Mudrák

Re: Multilingual link

by Wonton Sopabuena -

Once again...PERFECT!!!! I'm very impressed!!!

It worked perfectly!. I had to add one thing (marked in red):

if (empty($USER->lang)) {
// user is guest or anonymous
$lang = optional_param("lang", "en_utf8");
} else {
$lang = $USER->lang;
}

switch ($lang) {
case 'en_utf8': do_sth(); break;case 'en': do_sth();break;
case 'es_utf8': do_sth(); break;case 'es': do sth();break;
}

When the language comes from URL param is 'en' or 'es', and when it cames from the Moodle user settings it is 'en_utf8' or 'es_utf8'. Although I think, my code is not necessary if the URL is www.myexample.com/courses/view.php?id=2&lang=en_utf8 instead of www.myexample.com/courses/view.php?id=2&lang=en.

Well, once again, thank you very much David!!!

Wonton