Search code examples
fat-free-framework

fat-free-framework multi-language: translating a string that includes link?


In fat-free-framework is there any way to translate a string that includes a hyperlink, without using multiple translate file variables? I've read the multi-language support documentation and F3 frameworks allow some extra functionality for locales strings but it doesn't seem it is possible?

Let's say I want to translate a sentence that includes hyperlink:

Help me get translated.

I know I could translate this string like this:

$txt_HelpMe => 'Help me',
$txt_Get => 'get',
$txt_Translated = 'translated'

And then return the values in my template accordingly:

{{$txt_HelpMe}} <a href="#">{{$txt_Get}}</a> {{$txt_Translated}}

However, is there any way I could translate this string by using just one variable

$txt_HelpMeGetTranslated => '...'

and then return it

{{@txt_HelpMeGetTranslated }}

... while keeping the hyperlink?


Solution

  • This is a good question. I assume that your main problem is that Fat-Free Framework is automatically escaping translation messages by default. If you can trust your translated messages, you can un-escape the translation to render hyperlinks (or HTML in general).

    1. Translate your messages, e.g. dict/en.php

      <?php return [
        'translation_invitation_html' => 'Please help us to <a href="https://fatfreeframework.com">translate</a> the software.',
        'translation_invitation_html_placeholder' => 'Please help us to <a href="{0}">translate</a> the software.',
      ]
      
    2. Format the translated string with or without a placeholder. The result will be escaped (with the default F3 configuration), therefore we have to un-escape the translated messages with the raw filter to restore our HTML links.

      <ul>
          <li>{{ @translation_invitation_html | raw }}</li>
          <li>{{ @translation_invitation_html_placeholder, 'https://fatfreeframework.com' | format, raw }}</li>
      </ul>
      
    3. The result will look like this:

      <ul>
          <li>Please help us to <a href="https://fatfreeframework.com">translate</a> the software.</li>
          <li>Please help us to <a href="https://fatfreeframework.com">translate</a> the software.</li>
      </ul>