Skip to content
Snippets Groups Projects
Commit 3e9be61b authored by Thijs Kinkhorst's avatar Thijs Kinkhorst
Browse files

Initial version of tool to extract translatable strings from templates.

parent 536f71f2
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/php -q
<?php
use SimpleSAML\Module;
use SimpleSAML\Utils;
// This is the base directory of the SimpleSAMLphp installation
$baseDir = dirname(dirname(__FILE__));
// Add library autoloader
require_once($baseDir . '/lib/_autoload.php');
$transUtils = new Utils\Translate();
$sysUtils = new Utils\System();
$tempDirBase = $sysUtils->getTempDir() . "/temptemplatestemp";
$outputSuffix = '/locales/en/LC_MESSAGES';
$modules = ['', 'core', 'admin', 'cron', 'exampleauth', 'multiauth', 'saml'];
foreach($modules as $module) {
$tempDir = $tempDirBase . "/" . $module;
$transUtils->compileAllTemplates($module, $tempDir);
$domain = $module ?: 'messages';
$outputDir = $baseDir . ($module === '' ? '' : '/modules/' . $module) . $outputSuffix;
print `xgettext --default-domain=$domain -p $outputDir --from-code=UTF-8 -j --no-location -ktrans -L PHP $tempDir/*/*.php`;
}
// TODO: clean up workdir before/after run
// TODO: merge new strings into translated languages catalogs
<?php
declare(strict_types=1);
namespace SimpleSAML\Utils;
use SimpleSAML\Configuration;
use SimpleSAML\XHTML\Template;
/**
* @package SimpleSAMLphp
*/
class Translate
{
/**
* Compile all Twig templates for the given $module into the given $outputDir.
* This is used by the translation extraction tool to find the translatable
* strings for this module in the compiled templates.
* $module can be '' for the main SimpleSAMLphp templates.
*/
public function compileAllTemplates(string $module, string $outputDir): void
{
$config = Configuration::loadFromArray(['template.cache' => $outputDir, 'module.enable' => [$module => true]]);
$baseDir = $config->getBaseDir();
$tplSuffix = '/templates/';
$tplDir = $baseDir . ($module === '' ? '' : 'modules/' . $module) . $tplSuffix;
$templateprefix = ($module === '' ? '' : $module . ":");
foreach (
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($tplDir),
\RecursiveIteratorIterator::LEAVES_ONLY
) as $file
) {
if ($file->isFile()) {
$p = new Template($config, $templateprefix . str_replace($tplDir, '', $file->getPathname()));
$p->compile();
}
}
}
}
...@@ -491,6 +491,15 @@ class Template extends Response ...@@ -491,6 +491,15 @@ class Template extends Response
$this->data['header'] = $this->configuration->getValue('theme.header', 'SimpleSAMLphp'); $this->data['header'] = $this->configuration->getValue('theme.header', 'SimpleSAMLphp');
} }
/**
* Helper function for locale extraction: just compile but not display
* this template. This is not generally useful, getContents() will normally
* compile and display the template in one step.
*/
public function compile(): void
{
$this->twig->load($this->twig_template);
}
/** /**
* Get the contents produced by this template. * Get the contents produced by this template.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment