dongta5621
2009-12-04 17:08
浏览 92
已采纳

Gettext:用HTML翻译字符串?

My current implementation, which is array based stores keys and values in a dictionary, example:

$arr = array(
    'message' => 'Paste a <a href="http://flickr.com/">flickr</a> URL below.',
);

I realize that it was probably a bad idea storing html inside of a string such as this, but if I'm using gettext then in my .mo/.po files how should I handle storing a similar string? Should I just store words, such as 'Paste a' and 'URL below' and 'flickr' separately?

图片转代码服务由CSDN问答提供 功能建议

我当前的实现,即基于数组的字典中存储键和值,例如: $ arr = array( 'message'=&gt;'粘贴&lt; a href =“http://flickr.com/”&gt; flickr&lt; / a&gt;以下网址。' , );

我意识到将html存储在像这样的字符串中可能是一个坏主意,但是如果我使用gettext那么 我的.mo / .po文件应该如何处理存储类似的字符串? 我应该分别存储单词,例如“粘贴一个”和“下面的URL”和“flickr”吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsdtszi0520538 2009-12-04 17:24
    已采纳

    You should store something like

    "Paste a %1 URL below"
    

    and replace all 'vars' using something simple like str_replace('%1', $link, $message);

    $link can also be translatable

    "<a href="http://flickr.com/">%1</a>"
    

    although that might be overkill (does flickr translate between languages?)

    rationale behind this is that different languages have different grammatical structures and the ordering of the words wont always be the same.

    Update:

    as @alex and @chelmertz mention in the comments, try using the sprintf function, which is built for this very thing.

    点赞 打赏 评论
  • drfls28608 2015-07-10 12:06

    I'd go for this:

    $arr = array(
        'message' => _('Paste a %s URL below.'),
    );
    

    Having all translations as string literals within gettext function calls allows to use standard tools to update *.po catalogues.

    点赞 打赏 评论

相关推荐 更多相似问题