NatLibFi /
Skosmos
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Util; |
||
| 4 | |||
| 5 | use Symfony\Component\Translation\Extractor\ExtractorInterface; |
||
| 6 | use Symfony\Component\Translation\MessageCatalogue; |
||
| 7 | |||
| 8 | class JsTranslationExtractor implements ExtractorInterface |
||
| 9 | { |
||
| 10 | public function __construct(string $directory) |
||
| 11 | { |
||
| 12 | $this->directory = $directory; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 13 | } |
||
| 14 | |||
| 15 | private string $prefix = ''; |
||
| 16 | |||
| 17 | public function setPrefix(string $prefix): void |
||
| 18 | { |
||
| 19 | $this->prefix = $prefix ?: ''; |
||
| 20 | } |
||
| 21 | |||
| 22 | public function extract($directory, MessageCatalogue $catalogue): void |
||
| 23 | { |
||
| 24 | if (!is_dir($this->directory)) { |
||
| 25 | echo "Invalid directory: $this->directory" . PHP_EOL; |
||
| 26 | return; |
||
| 27 | } |
||
| 28 | $iterator = new \RecursiveIteratorIterator( |
||
| 29 | new \RecursiveDirectoryIterator($this->directory) |
||
| 30 | ); |
||
| 31 | foreach ($iterator as $file) { |
||
| 32 | if ($file->getExtension() !== 'js') { |
||
| 33 | continue; |
||
| 34 | } |
||
| 35 | $content = file_get_contents($file->getPathname()); |
||
| 36 | preg_match_all('/\$t\([\'"]([^\'"]+)[\'"]\)/', $content, $matches); |
||
| 37 | foreach ($matches[1] as $key) { |
||
| 38 | $catalogue->set($this->prefix . $key, $key); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 |