fat-free-framework 多语言:翻译包含 link 的字符串?

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

在 fat-free-framework 中,有什么方法可以在不使用多个翻译文件变量的情况下翻译包含超链接的字符串? 我读过 multi-language support documentation 和 F3 框架允许语言环境字符串的一些额外功能,但似乎不可能?

假设我要翻译一个包含超链接的句子:

Help me get translated.

我知道我可以这样翻译这个字符串:

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

然后 return 我模板中相应的值:

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

但是,有什么方法可以只使用一个变量来翻译这个字符串

$txt_HelpMeGetTranslated => '...'

然后return它

{{@txt_HelpMeGetTranslated }}

...同时保留超链接?

这是个好问题。我假设您的主要问题是 Fat-Free Framework 默认情况下会自动转义翻译消息。如果您可以信任您翻译的消息,您可以取消转义翻译以呈现超链接(或通常 HTML)。

  1. 翻译您的消息,例如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. 使用或不使用占位符格式化翻译后的字符串。结果将被转义(使用默认的 F3 配置),因此我们必须使用 raw 过滤器取消转义已翻译的消息以恢复我们的 HTML 链接。

    <ul>
        <li>{{ @translation_invitation_html | raw }}</li>
        <li>{{ @translation_invitation_html_placeholder, 'https://fatfreeframework.com' | format, raw }}</li>
    </ul>
    
  3. 结果将如下所示:

    <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>