Извлечение переводов из всех шаблонов веток (CodeIgniter)

Мы используем механизм шаблонов Twig для нашего проекта на основе CodeIgniter. И мы столкнулись с проблемой извлечения переводимых строк из шаблона ветки.

{% trans %}Text to translate{% endtrans %}

xgettext не может извлекать переводимые строки из таких шаблонов, но может анализировать скомпилированные (в php) шаблоны веток и извлекать из них переводимые строки. Таким образом, проблема может быть решена путем компиляции всех шаблонов в php, а затем извлечения из него строк.

И вот вопрос:

Как заставить Twig скомпилировать набор расширений из командной строки?


person Sergey P. aka azure    schedule 08.11.2013    source источник


Ответы (1)


Результат моих исследований закончился следующим php-скриптом. Он предназначен для использования следующим образом:

./compile_twig_templates.php /path/to/tmp/dir

Скрипт заполнит каталог tmp скомпилированными шаблонами (чистые файлы .php), которые могут быть проанализированы xgettext как обычно.

#!/usr/bin/php
<?php
if (!$argv) {
    echo 'Script should be runned from command line';
    exit(1);
}

if ( !$argv[1] ) {
    echo "Usage: ".$argv[0]." path_to_cache_dir\n";
    echo "Example: ".$argv[0]." /tmp/twig_cache\n";
    exit(1);
}

if ( !is_dir($argv[1])) {
    echo "$argv[1] is not a directory";
    exit(1);
}


$script_path = dirname($argv[0]);

require_once 'libraries/Twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
require_once (string) "libraries/Twig-extensions/lib/Twig/Extensions/Autoloader.php";
Twig_Extensions_Autoloader::register();

//path to directory with twig templates
$tplDir = $script_path.'/views';
$tmpDir = $argv[1];
$loader = new Twig_Loader_Filesystem($tplDir);

// force auto-reload to always have the latest version of the template
$twig = new Twig_Environment($loader, array(
    'cache' => $tmpDir,
    'auto_reload' => true
));

//add all used extensions
$twig->addExtension(new Twig_Extensions_Extension_I18n());

//add all used custom functions (if required)
$twig->addFunction('base_url', new Twig_Function_Function('base_url'));

// Adding PHP helper functions as filters:
$twig->addFilter(new Twig_SimpleFilter('ceil', 'ceil'));

echo "Iterating over templates!\n";
// iterate over all your templates
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tplDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
{
    echo "Compiling ".$file."\n";
    // force compilation
    if ($file->isFile()) {
        try {
        $twig->loadTemplate(str_replace($tplDir.'/', '', $file));
        } catch (Twig_Error_Syntax $e) {
            echo "Caught exeption! $e\n";
            var_dump($e);
            exit(2);
        }
    }
}

?>
person Sergey P. aka azure    schedule 02.12.2013