Skip to content

Commit 8d4af53

Browse files
committed
Make translation tools theme-aware
1 parent 2a8023a commit 8d4af53

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

src/SimpleSAML/Command/UnusedTranslatableStringsCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ protected function configure(): void
5555
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
5656
'Which modules to perform this action on',
5757
);
58+
$this->addOption(
59+
'themes',
60+
null,
61+
InputOption::VALUE_NEGATABLE,
62+
'Include/exclude templates from themes directories of modules (default --themes)',
63+
true, /* safe default, so we don't lose translations */
64+
);
5865
}
5966

6067

@@ -99,7 +106,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
99106
$phpScanner = new PhpScanner(...$translationDomains);
100107
$phpScanner->setFunctions(['trans' => 'gettext', 'noop' => 'gettext']);
101108

102-
$translationUtils = new Utils\Translate(Configuration::getInstance());
109+
$translationUtils = new Utils\Translate(
110+
Configuration::getInstance(),
111+
$input->getOption('themes'),
112+
);
103113
$twigTranslations = [];
104114
// Scan files in base
105115
foreach ($modules as $module) {

src/SimpleSAML/Command/UpdateTranslatableStringsCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ protected function configure(): void
6363
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
6464
'Which modules to perform this action on',
6565
);
66+
$this->addOption(
67+
'themes',
68+
null,
69+
InputOption::VALUE_NEGATABLE,
70+
'Include/exclude templates from themes directories of modules (default --no-themes)',
71+
false,
72+
);
6673
}
6774

6875
/**
@@ -127,7 +134,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
127134
$phpScanner = new PhpScanner(...$translationDomains);
128135
$phpScanner->setFunctions(['trans' => 'gettext', 'noop' => 'gettext']);
129136

130-
$translationUtils = new Utils\Translate(Configuration::getInstance());
137+
$translationUtils = new Utils\Translate(
138+
Configuration::getInstance(),
139+
$input->getOption('themes'),
140+
);
131141
$twigTranslations = [];
132142
// Scan files in base
133143
foreach ($modules as $module) {

src/SimpleSAML/Utils/Translate.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66

77
use Gettext\Scanner\PhpScanner;
88
use SimpleSAML\Configuration;
9+
use SimpleSAML\Module;
910
use SimpleSAML\XHTML\Template;
1011
use Symfony\Bridge\Twig\Translation\TwigExtractor;
1112
use Symfony\Component\Finder\Finder;
1213
use Symfony\Component\Translation\MessageCatalogue;
1314

15+
use array_merge;
16+
use explode;
17+
use in_array;
18+
use is_dir;
19+
use str_starts_with;
20+
use strlen;
21+
use substr;
22+
1423
/**
1524
* @package SimpleSAMLphp
1625
*/
@@ -45,19 +54,48 @@ public function getTranslationsFromPhp(string $module, PhpScanner $phpScanner):
4554
}
4655

4756

48-
public function getTranslationsFromTwig(string $module): array
57+
public function getTranslationsFromTwig(string $module, bool $includeThemes = false): array
4958
{
5059
$twigTranslations = [];
5160
$moduleDir = $this->baseDir . ($module === '' ? '' : 'modules/' . $module . '/');
5261
$moduleTemplateDir = $moduleDir . 'templates/';
62+
$moduleThemeDir = $moduleDir . 'themes/';
63+
$moduleDirs = [];
64+
if (is_dir($moduleTemplateDir)) {
65+
$moduleDirs[] = $moduleTemplateDir;
66+
}
67+
if ($includeThemes && is_dir($moduleThemeDir)) {
68+
$moduleDirs[] = $moduleThemeDir;
69+
}
5370

5471
// Scan Twig-templates
5572
$finder = new Finder();
56-
foreach ($finder->files()->in($moduleTemplateDir)->name('*.twig') as $file) {
57-
$template = new Template(
58-
$this->configuration,
59-
($module ? ($module . ':') : '') . $file->getRelativePathname(),
60-
);
73+
foreach ($finder->files()->in($moduleDirs)->name('*.twig') as $file) {
74+
if (!($includeThemes && str_starts_with($file->getPathname(), $moduleThemeDir))) {
75+
/* process templates */
76+
$template = new Template(
77+
$this->configuration,
78+
($module ? ($module . ':') : '') . $file->getRelativePathname(),
79+
);
80+
} else {
81+
/* process themed templates from other modules */
82+
list($theme, $themedModule) = explode(DIRECTORY_SEPARATOR, substr($file->getPath(), strlen($moduleThemeDir)), 2);
83+
if ($themedModule !== 'default' && !Module::isModuleEnabled($themedModule)) {
84+
throw new Exception(
85+
'The module \'' . $themedModule . '\' (themed by \'' . $module . ':' . $theme . '\') is not enabled.'
86+
);
87+
}
88+
$template = new Template(
89+
Configuration::loadFromArray(
90+
array_merge(
91+
$this->configuration->toArray(),
92+
['theme.use' => $module . ':' . $theme,],
93+
),
94+
),
95+
($themedModule !== 'default' ? ($themedModule . ':') : '') .
96+
substr($file->getRelativePathname(), strlen($theme . DIRECTORY_SEPARATOR . $themedModule) + 1),
97+
);
98+
}
6199

62100
$catalogue = new MessageCatalogue('en', []);
63101
$extractor = new TwigExtractor($template->getTwig());

0 commit comments

Comments
 (0)