get_string function

get_string function

by Bezk Intos -
Number of replies: 10

Hi.

In my a.php file I have SQL that retrieves content (<span class='multilang' lang='fr'>French</span><span class='multilang' lang='en'>English</span>) from mdl_data_content table as $str.

I also have this in my a.php file:

$lang = $_GET['lang'];

$component = "mod_data";

echo get_string_manager()->get_string($str, $component, null, $lang);


I'm expecting to have as result: "Franch" or "English" depending on lang parameter in URL (http://localhost/a.php?lang=fr   OR  http://localhost/a.php?lang=en) which is retrieved through $_GET['lang'] but I get as result "FranchEnglish" so obviously I'm doing something wrong with get_string function.

Can someone help please?

Average of ratings: -
In reply to Bezk Intos

Re: get_string function

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

Couple of things.

Using $_GET is generally a bad idea. See e.g. this for more details.

The get_string() function is used to display UI texts that are defined in the lang/{langcode}/{pluginname}.php files in your plugin. This function is not for displaying content. And you do not need to call the string manager's method.

What you probably want is to make sure that multilang filter is enabled at your site and then use something like echo format_string($str);

Average of ratings: Useful (1)
In reply to David Mudrák

Re: get_string function

by Bezk Intos -

Hi David, thank you very much for your Reply.

I'm trying to retrieve some data directly from mdl_data_content table via my php file and then put it to Moodle HTML block via IFRAME. This is my newbie attempt to do something the only way I know how. So as I understand it this php I have created is not part of the moodle UI so that's why multilanguage filter is not working by default. Multilanguage filter is enabled at my site and multilanguage filer is working well except in this parts I created by myself and included via IFRAME. Is it even possible to use multilanguage filter in such a way (in my php file which is not a part of moodle)? I tried with echo format_string($str); but again I got "FrenchEnglish" so obviously something else must be done?

In reply to Bezk Intos

Re: get_string function

by Bezk Intos -

So just to be sure everybody understands me...

In /var/www/moodle/test.php I have this:

<?php

$text = "<span class='multilang' lang='fr'>French</span><span class='multilang' lang='en'>English</span>";

echo $text;

?>

So, echo will give back "FrenchEnglish". What do I have to do in this test.php file to have only "French" or "English" (depending on the currently selected language)? Is it even possible to use multilang filter in such way?

In reply to Bezk Intos

Re: get_string function

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

Yes, that is exactly what the filter is written for. The following code works for me given that I have the filter configured so that it applies to both "Content and headings" (that is basically an UI description of saying that it is used in both format_string() and format_text() functions).

<?php

// All Moodle scripts must have this to initialize the environment.
require(__DIR__.'/config.php');

// Either the PAGE's context must be set or the context id must be passed to
// the format_string() as the 'context' option so the filter can work.
$PAGE->set_context(context_system::instance());

// Note that the filter expects double quotes wrapping the attributes. Also,
// the following order of attributes is supposed to be a bit faster.
$text = '<span lang="fr" class="multilang">French</span><span lang="en" class="multilang">English</span>';

echo format_string($text);
Average of ratings: Useful (2)
In reply to David Mudrák

Re: get_string function

by Bezk Intos -

It works!

Thank you very very much for your time and effort (and detailed explanation)!

In reply to David Mudrák

Re: get_string function

by Toriton Shigant -

Hello David,

I have following code inside renderer.php

$content .= '<div class="course-btn"><p><a class="btn btn-primary" href="'.new moodle_url('/course/view.php', array('id' => $course->id)).'">'.get_string('entercourse').'</a></p></div>';

And I want to apply changes to the text output from string "entercourse" in two different languages. Obviously I cannot paste below

<span lang="ja" class="multilang">コースに入る</span><span lang="en" class="multilang">Enter this course</span>
to where .get_string('entercourse')  is written. It brings error.

How should I write and perhaps I should paste your above code somewhere inside moodle core file first I guess.. but don't know where to look.

As I am still new to Moodle, my question might not be relevant to this thread..
Thank you.
Toriton
In reply to Toriton Shigant

Re: get_string function

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Toriton,

I you just want to change the button 'Enter this course' to display different text, then just use the Language_customisation interface to override the 'entercourse' string in both the English and Japanese language packs.

In reply to Davo Smith

Re: get_string function

by Toriton Shigant -

Hi Davo,

Wow, that's amazing. I didn't even know changing default string is possible in such easy way! Thank you so much. I could change the text!

I have one more question if you don't mind.. This also may be related.

Inside layout folder (theme > mytheme > layout) I have "footer.php". For example if I want to change "copyright" text in between 2 languages, what should I do or even it is possible?

I tried to put the code below directly into the footer.php but as you know it doesn't work.

<p><span class="multilang" lang="en">Copyright</span><span class="multilang" lang="ja">著作権</span></p>

It seems like the multi language translation only works on existing string like I was doing above, parts like site name, course name, footer disclaimer etc by entering multilang code into "Rich-Text-Editor" from the theme control or defining another setting like using <?php echo format_text($PAGE->theme->settings->xxxx) ?> to link to theme setting page (then again using Rich-text-editor to apply multilang code.

Hope my question makes sense.
Toriton

In reply to Toriton Shigant

Re: get_string function

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you're already editing the theme, then the easiest solution would be to do the following:

  • Open up theme/[themename]/lang/en/theme_[themename].php
  • Add a new language string: $string['copyright'] = "Copyright";
  • Edit footer.php to add: <p><?php echo get_string('copyright', 'theme_[themename]'); ?></p>
  • Either increase the theme version number by 1 digit and visit site admin > notifications, OR follow site admin > developer > purge all caches
  • Then you should be able to override that language string for Japanese via the same UI as you used before