【问题标题】:Extract template strings for i18n from Twig in Slim从 Slim 中的 Twig 中提取 i18n 的模板字符串
【发布时间】:2014-03-13 10:03:37
【问题描述】:
我花了很多时间试图弄清楚如何在基于 Slim 的应用程序中“编译”我所有的 Twig 模板,以确保所有字符串都准备好被xgettext 拾取以进行进一步处理和翻译。
只要我把正确的信息放在一起,结果就很容易了,但是我在互联网上找不到任何地方告诉我如何在 Slim 应用程序中使用 Twig 来做到这一点。
【问题讨论】:
标签:
internationalization
twig
translation
gettext
slim
【解决方案1】:
Twig 文档中有一个很好的 how to extract template strings 示例。但是,在您能够这样做之前,您需要将 Twig 环境从您的 Slim 应用程序中拉出,as described in the Slim Knowledge Base。
所以,这里是 Twig 文档中示例的修改版本:
// Specify where your templates are located.
$tplDir = '/path/to/your/templates';
// Get the Twig environment from your Slim app, $app.
$twig = $app->view()->getEnvironment();
// Iterate over all your templates.
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tplDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
{
// Force compilation.
if ($file->isFile()) {
$twig->loadTemplate(str_replace($tplDir.'/', '', $file));
}
}