Presenting strings with multilang spans inside plugins

Presenting strings with multilang spans inside plugins

by Martin Greenaway -
Number of replies: 4

Hi there,

I have a plugin where an administrator can enter a language-aware string, such as:

<span class="multilang" lang="en">Hello</span><span class="multilang" lang="fr">Bonjour<span>

I've read through the https://docs.moodle.org/dev/Output_functions and my assumption is that the multilanguage stuff is being done by a filter.

I've found that the use of format_string($x) sometimes works to filter the relevant language strings when I'm displaying this value. However, sometimes it doesn't, by which, I mean that it strips all HTML out leading to bulleted lists turning into straight paragraphs and still presents the content in both languages.  I have tried to use it with $options = array('escape' => false) but that has made no difference.

I've also tried format_text() which doesn't seem to process the filters (I assume this multilang stuff is being handled by a filter?) even though the documentation says that options->filter for this method defaults to true.

I'm not clear why this is the case - so is there anything I should know about this?  Is there anywhere I can go to learn more?


Average of ratings: -
In reply to Martin Greenaway

Re: Presenting strings with multilang spans inside plugins

by Mihai Bojonca -
Picture of Testers

The core multilang filer is not very capable. 

It will help you display in different languages if your text is very basic. Any formatting options that beyond headings and some other basic ones will not work.

Any extra tags will break your display. 

In order for it to work you need to keep your formatting very basic (which is not bad).

The solution is https://moodle.org/plugins/filter_multilang2


Average of ratings: Useful (1)
In reply to Mihai Bojonca

Re: Presenting strings with multilang spans inside plugins

by Martin Greenaway -

Sorry for the delay in responding, and thank you for the recommendation, I will investigate this filter. I've not had an opportunity to do so yet.

In the meantime, following your hints to keep formatting basic, I have managed to get the core multilang plugin to work by spanning everything inline, i.e. building a bulleted list outside of the language spans and then doing just the text inside them. 

For example, instead of:

<span class="multilang" lang="en">
  <ul>
    <li>My list item 1</li>
    <li>My list item 2</li>
  </ul>
</span>
<span class="multilang" lang="fr">
  <ul>
    <li>My French list item 1</li>
    <li>My French list item 2</li>
  </ul>
</span>

which didn't work properly, I have done this:

<ul>
  <li><span class="multilang" lang="en">My list item 1</span><span class="multilang" lang="fr">My French list item 1</span></li>
  <li><span class="multilang" lang="en">My list item 2</span><span class="multilang" lang="fr">My French list item 2</span></li>
</ul>

The latter seems to be working fine, although is a bit cumbersome to explain to a client.

In reply to Martin Greenaway

Re: Presenting strings with multilang spans inside plugins

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

I don't think spans can wrap around other content like lists in that way - you really need to use divs but the core multilang filter doesn't support that.

I believe the multilang2 filter mentioned above does. Certainly an upgrade to the core one that I wrote a few years ago does, but I think the one linked above is newer and better than the one I originally did, if it works for you. 


Richard

Average of ratings: Useful (2)
In reply to Richard Oelmann

Re: Presenting strings with multilang spans inside plugins

by Martin Greenaway -

You're probably right on that actually being a restriction of proper use of spans - I think sometimes one can get away with poor usage like that in other places and the immediate reaction when faced with something that implements in a correct manner is to blame the thing which is right for being so!