-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from JBlond/extract
- Add extract feature
- Loading branch information
Showing
4 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/.idea | ||
/.phpunit.result.cache | ||
/composer.lock | ||
/example/cache/*/*.php | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |