Variable in module strings not working

Re: Variable in module strings not working

by Howard Miller -
Number of replies: 2
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I'm not entirely sure what you are trying to do. You may be confusing two things...

$coursename = 'My big course';
echo get_string('xxx', 'mod_custom', $coursename);

goes with...

$string['xxx'] = 'You have applied for course {$a}';

OR....

$a = new stdClass;
$a->coursename = 'My big course';
echo get_string('xxx', 'mod_custom', $a);

goes with...

$string['xxx'] = 'You have applied for course {$a->coursename}';

Generally you would only use the latter if you had more than one string substitution in the same language string. 

Average of ratings: Useful (1)
In reply to Howard Miller

Re: Variable in module strings not working

by Vladimir Marton -
I have my own module. This module is supposed to send an email to user when he confirms all the steps in it. That is not important. The important is the workflow of this module:


There is a string in it, it goes like this:

$string['touser_youhaveapplied_subject'] = 'You have applied for a course {$a->coursename}';
This string is then loaded as a subject of the email in another php file:

get_string('touser_youhaveapplied_subject','mod_invoicewizard', $a);

$a is the object of stdClass that I create in advance and Im 100% sure it has a coursename property.

But when the email is sent, the subject of it is 'You have applied for a course {$a}' and I dont know why and what to do.


In reply to Howard Miller

Re: Variable in module strings not working

by Vladimir Marton -

Just now I found a way to make it work. I switched it the other way around, so:

$string['touser_youhaveapplied_subject'] = 'You have applied for a course {$a->coursename}';
and...

get_string('touser_youhaveapplied_subject','mod_invoicewizard', $a);
Suddenly, it works. I dont exactly know how the moodle cache purging works, but Im definitely sure that I had it like this before once and it was not working. If moodle cached my old files and I did not purge it to refresh these two lines, then that must have been the problem. So I advise to everyone to purge the cache after every file change, or make your cron for admin/cron.php to run every 15 seconds or so :D


Thanks a lot Howard, thanks to you I tried all of it again,so you basically solved my problem!

I have a question tho - if I wanted to use more properties of the object $a, how would I make it work? Because if I use the whole $a object in $string and try to get its properties in get_string then it does not work.


//EDIT: Now it works the other way around, I had to purge the cache for the old lines to disappear and after the new code is chached now, it all works as it should. So purging the cache is the final solution for this problem.