Skip to content

Commit

Permalink
Merge pull request #5 from JBlond/extract
Browse files Browse the repository at this point in the history
- Add extract feature
  • Loading branch information
JBlond committed Mar 30, 2022
2 parents 98badbb + 17a8411 commit 3b5e443
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea
/.phpunit.result.cache
/composer.lock
/example/cache/*/*.php
/vendor
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ echo $tpl->render();
* PHP Multibyte String ' gettext'
* Twig >= 3.0

### Optional Requirements

* xgettext for Extract / generating po files.

### License (MIT License)

Expand Down
51 changes: 51 additions & 0 deletions example/extract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use jblond\TwigTrans\Extract;
use jblond\TwigTrans\Translation;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;

require '../vendor/autoload.php';

error_reporting(E_ALL);
ini_set('display_errors', 'On');

$langCode = 'de_DE';
putenv("LC_ALL=$langCode.UTF-8");
if (setlocale(LC_ALL, "$langCode.UTF-8") === false) {
echo sprintf('Language Code %s not found', $langCode);
}

// set the path to the translation
bindtextdomain("Web_Content", "./locale");

// choose Domain
textdomain("Web_Content");

$twigConfig = [
'cache' => './cache',
'debug' => true,
'auto_reload' => true
];

$twigLoader = new FilesystemLoader('./tpl/');
$twig = new Environment($twigLoader, $twigConfig);

// this is for the filter |trans
$filter = new TwigFilter(
'trans',
function ($context, $string) {
return Translation::transGetText($string, $context);
},
['needs_context' => true]
);
$twig->addFilter($filter);

// load the i18n extension for using the translation tag for twig
// {% trans %}my string{% endtrans %}
$twig->addExtension(new Translation());

$extract = new Extract($twig);
$extract->addTemplate('default.twig');
$extract->extract();
116 changes: 116 additions & 0 deletions src/jblond/TwigTrans/Extract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace jblond\TwigTrans;

use RuntimeException;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

/**
*
*/
class Extract
{
/**
* @var Environment
*/
protected $environment;

/**
* Gettext parameters.
*
* @var string[]
*/
protected $parameters;

/**
* @var
*/
private $executable;

/**
* @param Environment $environment
*/
public function __construct(Environment $environment)
{
$this->environment = $environment;
$this->reset();
}

/**
* @param mixed $executable
* @return void
*/
public function setExecutable($executable): void
{
$this->executable = $executable;
}

/**
* @return void
*/
protected function reset(): void
{
$this->parameters = [];
}

/**
* @param string $path
* @throws SyntaxError
* @throws LoaderError
* @throws RuntimeError
* @return void
*/
public function addTemplate(string $path): void
{
$this->environment->load($path);
}

/**
* @param $parameter
* @return void
*/
public function addGettextParameter($parameter): void
{
$this->parameters[] = $parameter;
}

/**
* @param array $parameters
* @return void
*/
public function setGettextParameters(array $parameters): void
{
$this->parameters = $parameters;
}


/**
* @return void
*/
public function extract(): void
{
$cacheDirectory = $this->environment->getCache();
if ($cacheDirectory === false) {
throw new RuntimeException('No cache directory is set');
}
$command = $this->executable ? : 'xgettext';
$command .= ' ' . implode(' ', $this->parameters);
$command .= ' ' . $cacheDirectory . '/*/*.php';
echo $command;
$error = 0;
$output = system($command, $error);
if (0 !== $error) {
throw new RuntimeException(sprintf(
'Gettext command "%s" failed with error code %s and output: %s',
$command,
$error,
$output
));
}

$this->reset();
}
}

0 comments on commit 3b5e443

Please sign in to comment.