Customizing strings with html tags

Customizing strings with html tags

by Fernando Ventura -
Number of replies: 11

Hi

  • Moodle: 3.10
  • Language pack: PT-PT

I'm trying to customize a string (in error.php) with html tags.

The string shows on exactly as I'd put it at the translate input box:

O ficheiro solicitado não está disponível. <br /><br />Se procurava documentos relativos a <strong>provas ou exames</strong>, estes podem ainda não terem sido disponibilizados, ou podem corresponder a provas ou exames que não se realizam este ano no agrupamento.<br /><p>Confirme nos <em> documentos oficiais </em> a eventual <strong>realização</strong> da prova/exame que procura.</p>

When the error occurs, I see this, with the html tags as text!?

What I'm doing wrong?

Thank you all, in advance.

Average of ratings: -
In reply to Fernando Ventura

Re: Customizing strings with html tags

by Fernando Ventura -
Hi
No one can help me?
I've found that the problem doesn't exist in all strings.
This one is from error.php, the filenotfound string.
I'm not a moodle developer.
Is there a good soul that can give me something to fix this?
thank you.
In reply to Fernando Ventura

Re: Customizing strings with html tags

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators
Hi Fernando,

Just to be sure, if you don't customize the string, or use english, does the error show well?

Séverin
In reply to Séverin Terrier

Re: Customizing strings with html tags

by Fernando Ventura -

Thank you for your attention!

Yes, the error string shows always "well" BUT in plain text (as the original one),

If I put there some basic html tags (<p>, <em>, <br />. ...) the html tags shows as text and are not rendered.

Some other strings errors (component: core_error, string: dbconnectionfailed), are! Is it possible that the the problem is linked to the nature of the error? 

The problem occurs at customizing the EN or PT translation. And on different themes. And on a fresh Moodle install.

I even tried to change the original EN package (not customizing) and the result was the same.

So, it must be the way that the error is handled by moodle...?

The dbconnectionfailed and the filenotfound strings has, for testing proposes and on EN language, the same string:

<p>Sentence 1</p><p><em>Sentence 2</em><br />New line</p> <strong>Last<br>one</strong>

When I disconnect the BD, the error shows well. 

When I go to a link with no file, the error shows the text exactly (almost, it converted the <br> to <br />!!!) like above (plain text).

If you need more info, please ask!

Thank you!

In reply to Fernando Ventura

Re: Customizing strings with html tags

by John Provasnik -
Picture of Particularly helpful Moodlers Picture of Testers
Maybe it's possible it doesn't work everywhere. I've had a forum string that would not take some of my HTML tags, but it would take "style". can you try some simple html, like O ficheiro solicitado não está disponível. just to see if it takes some styling.... but other than that, I'm not sure.
In reply to John Provasnik

Re: Customizing strings with html tags

by Fernando Ventura -
Thank you for your answer.
Tried: < span style="color:#FF0000;">O ficheiro solicitado não está disponível. < /span> 
Shows plain text. Exactly the same value. (span without space before, of course. The space is here exactly to prevent the rendering of the span tag)
In reply to Fernando Ventura

Re: Customizing strings with html tags

by koen roggemans -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Translators
I don't think you should use html in strings.
Reading the code guide about strings on https://docs.moodle.org/dev/String_API or on https://docs.moodle.org/dev/Using_variables_in_localization_strings, there is nothing about using html, even though a lot of html is used in strings.
For help strings, basic markdown is allowed - you could try to add new lines.
I have the impression that html works depending how the string is used, but you can't count on it.
In reply to koen roggemans

Re: Customizing strings with html tags

by Fernando Ventura -
Thank you for your answer.
I think the problem isn't at String API. It does what is expected to do: de value is update correctly at de DB, and selected too.
The problem must be at some render component... it choose to render with HTML ou only text. I saw some FORMAT_HTML and FORMAT_PLAIN options...
It seems that filenotfound string goes to a path that will have a FORMAT_PLAIN option, and the dbconnectionfailed goes to a different path, with FORMAT_HTML option. Why and where, I don't know triste
In reply to Fernando Ventura

Re: Customizing strings with html tags

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

From what I can trace in the code, HTML is not supported when displaying the exception message, and "filenotfound" is an exception.

The error string "filenotfound" is displayed via print_error() call which in turn throws moodle_exception. Default exception handler uses fatal_error() rendering method to render the message, which in turn uses s() call to format the given error message.

It intentionally and by design does not support HTML.

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

Re: Customizing strings with html tags

by Fernando Ventura -
Thank you very much!
Is intentional!! I can't see why, but ok, I'm not a designer, so smile
Can anyone suggest a workaround to show a customize page on moodle exceptions? Or it's not possible at all?
Average of ratings:Useful (1)
In reply to Fernando Ventura

Re: Customizing strings with html tags

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

Is intentional!! I can't see why

Because we don't know what the exception message contains. It can come from any part of 3rd party library, contributed plugin, customised string, user data etc. It would be very unwise and against all security guidelines to blindly trust random HTML and render it to the user's browser.

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

Re: Customizing strings with html tags

by Fernando Ventura -
I may understand that...
But this strings goes to DB, so, they are sanitized already, aren't they? And any other messages (from moodle-exception, handle by moodle) could be sanitized too, instead showing them in plain text. I'm assuming that there is a way to sanitize any string... (probably bounding html tags to an allowed short list...?)
Sorry to bother you, probably I'm telling only blunders, I'm just trying to completely understand.

But, more important now, can you tell me if there is a way to show some page different than the mentioned message? I will use intend filenotfound error in my site (there is hundreds of links, valid until some date, after that date, I will delete the files and want to show the info page if the user clicks on the link, in the next year I will put there another set of files - same names, of course), and the actual message page result ir rather ugly!
 
Thank you and please escuse me for abusing your patience.
Average of ratings:Useful (1)