| 1 | <?php |
||
| 8 | class TranslationFileExplorer |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Symfony default translation folder (in a bundle) |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | private $defaultTranslationFolder = 'Resources/translations'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * An array of supported file formats to look for |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $fileFormats = array(); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Looks in the path for Resources/translation files and returns a finder object with the result |
||
| 26 | * |
||
| 27 | * @param string $path |
||
| 28 | * @param array $locales |
||
| 29 | * |
||
| 30 | * @return \Symfony\Component\Finder\Finder |
||
| 31 | */ |
||
| 32 | 2 | public function find($path, array $locales, $translationDirectory = null) |
|
| 33 | { |
||
| 34 | 2 | $finder = new Finder(); |
|
| 35 | 2 | $translationDirectory = $translationDirectory ?? $this->defaultTranslationFolder; |
|
| 36 | |||
| 37 | 2 | $exploreDir = $path . '/' . $translationDirectory; |
|
| 38 | |||
| 39 | 2 | if (is_dir($exploreDir)) { |
|
| 40 | 2 | $finder->files() |
|
| 41 | 2 | ->name(sprintf('/(.*(%s)\.(%s))/', implode('|', $locales), implode('|', $this->fileFormats))) |
|
| 42 | 2 | ->in($exploreDir); |
|
| 43 | |||
| 44 | 2 | return $finder; |
|
| 45 | } |
||
| 46 | |||
| 47 | throw new TranslationsNotFoundException('Directory `' . $exploreDir . '` does not exist, translations could not be found.'); |
||
| 48 | } |
||
| 49 | |||
| 50 | 6 | public function setFileFormats($fileFormats) |
|
| 54 | } |
||
| 55 |