我刚刚遇到了这个问题,我相信我以优雅的方式解决了这个问题。
问题:我们想在PHP中使用Gettext,并使用主要语言字符串作为键翻译。但是,对于大块HTML(包括h1,h2,p,a等),我需要:
或
既不这些选项吸引了我,所以这是我做过什么:
- 保持简单的字符串(“OK”,“添加”,“确认”,“我真棒应用程序”)作为定期获取Gettext .po条目,原始文本为关键字
用markdown写入内容(大文本块),并将它们保存在文件中。 实施例的文件将是/homepage/content.md
(初级/源文本),/homepage/content.da-DK.md
,/homepage/content.de-DE.md
收件其获取内容文件(当前区域)并解析它的类。然后我用它喜欢:
<?=Template::getContent("homepage/content")?>
然而,有关动态大文本是什么?简单。使用模板引擎。我决定在Smarty,并在我的Template
类中使用它。
我现在可以使用模板逻辑.. 内降价!这真棒吗?!
随后而来的棘手的部分..
对于内容好看,有时你需要以不同的结构化你的HTML。考虑下面有3个“功能框”的广告系列区域。简单的解决方案:为该活动区域提供一个文件,并为每个的3个箱子提供一个文件。
但我可以做得比这更好。
我写了一个快速块解析器,所以我会将所有内容写入一个文件,然后分别渲染每个块。
示例文件:
[block campaign]
Buy this now!
=============
Blaaaah... And a smarty tag: {$cool}
[/block]
[block feature 1]
Feature 1
---------
asdasd you get it..
[/block]
[block feature 2] ...
这是我会怎样使它们在标记:
<?php
// At the top of the document...
// Class handles locale. :)
$template = Template::getContent("homepage/content", [
"cool" => "Smarty variable! AWESOME!"
]);
?>
...
<title><?=_("My Awesome App")?></title>
...
<div class="hero">
<!-- Template data already processed! :) -->
<?=$template->renderBlock("campaign")?>
</div>
<div class="featurebox">
<?=$template->renderBlock("feature 1")?>
</div>
<div class="featurebox">
<?=$template->renderBlock("feature 2")?>
</div>
恐怕我不能提供任何的源代码,因为这是为一个公司项目,但我希望你明白。
相关:[如何在对大文本值进行小编辑时有效地使用gettext PO文件](http://stackoverflow.com/questions/2966026/),[在使用gettext和gettext时合并键和全文。 po文件](http://stackoverflow.com/questions/15743919/),[它是一个好主意的消息ID是英文文本?](http://stackoverflow.com/questions/216478/), [我可以自动更新gettext的.po文件中的msgids以进行微不足道的文本更改吗?](http://stackoverflow.com/questions/8288050/) – ento