| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHP Translation package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) PHP Translation team <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Translation\Converter\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Config\Resource\FileResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Translation\Loader\LoaderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Translation\MessageCatalogue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Translation\Writer\TranslationWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Translation\Converter\Loader\TranslationLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Translation\SymfonyStorage\Dumper\XliffDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Translation\SymfonyStorage\FileStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Convert any translation format to XLF. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @author Tobias Nyholm <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class Converter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var \Translation\SymfonyStorage\TranslationLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $reader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var TranslationWriter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param LoaderInterface $reader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param string|string[] $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |     public function __construct(LoaderInterface $reader, $format) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |         $this->reader = new TranslationLoader($reader, $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         $this->writer = new TranslationWriter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $this->writer->disableBackup(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         $this->writer->addDumper('xlf', new XliffDumper()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string $inputDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $outputDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param array  $locales | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 | 2 |  |      */ | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |     public function convert($inputDir, $outputDir, array $locales) | 
            
                                                        
            
                                    
            
            
                | 57 | 2 |  |     { | 
            
                                                        
            
                                    
            
            
                | 58 | 2 |  |         $inputDir = realpath($inputDir); | 
            
                                                        
            
                                    
            
            
                | 59 | 2 |  |         if (false === realpath($outputDir) && false === mkdir($outputDir)) { | 
            
                                                        
            
                                    
            
            
                | 60 | 2 |  |             throw new \Exception('Unable to create output directory.'.$outputDir); | 
            
                                                        
            
                                    
            
            
                | 61 | 2 |  |         } | 
            
                                                        
            
                                    
            
            
                | 62 | 2 |  |         $outputDir = realpath($outputDir); | 
            
                                                        
            
                                    
            
            
                | 63 | 2 |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         $inputStorage = new FileStorage($this->writer, $this->reader, [$inputDir]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 65 | 2 |  |         $outputStorage = new FileStorage($this->writer, $this->reader, [$outputDir], ['xliff_version' => '2.0']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 66 | 2 |  |         foreach ($locales as $locale) { | 
            
                                                        
            
                                    
            
            
                | 67 | 2 |  |             $inputCatalogue = new MessageCatalogue($locale); | 
            
                                                        
            
                                    
            
            
                | 68 | 2 |  |             $outputCatalogue = new MessageCatalogue($locale); | 
            
                                                        
            
                                    
            
            
                | 69 | 2 |  |  | 
            
                                                        
            
                                    
            
            
                | 70 | 2 |  |             $inputStorage->export($inputCatalogue); | 
            
                                                        
            
                                    
            
            
                | 71 | 2 |  |             foreach ($inputCatalogue->all() as $domain => $messages) { | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |                 $outputCatalogue->add($messages, $domain); | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |                 foreach ($messages as $id => $message) { | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |                     $outputCatalogue->setMetadata($id, $inputCatalogue->getMetadata($id, $domain), $domain); | 
            
                                                        
            
                                    
            
            
                | 75 | 2 |  |                 } | 
            
                                                        
            
                                    
            
            
                | 76 | 2 |  |             } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |             // rewrite the resources to new path. | 
            
                                                        
            
                                    
            
            
                | 79 | 2 |  |             /** @var FileResource $resource */ | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |             foreach ($inputCatalogue->getResources() as $resource) { | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |                 $path = str_replace($inputDir, $outputDir, $resource->getResource()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 82 | 2 |  |  | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |                 // rewrite $path extension to be xlf | 
            
                                                        
            
                                    
            
            
                | 84 | 2 |  |                 $path = substr($path, 0, strrpos($path, '.')).'.xlf'; | 
            
                                                        
            
                                    
            
            
                | 85 | 2 |  |  | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |                 // Make sure file exists | 
            
                                                        
            
                                    
            
            
                | 87 | 2 |  |                 file_put_contents($path, ''); | 
            
                                                        
            
                                    
            
            
                | 88 | 2 |  |  | 
            
                                                        
            
                                    
            
            
                | 89 | 2 |  |                 $outputCatalogue->addResource(new FileResource($path)); | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |             $outputStorage->import($outputCatalogue); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                        
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.