| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Nikaia\TranslationSheet\Translation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Nikaia\TranslationSheet\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Foundation\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Nikaia\TranslationSheet\Spreadsheet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Nikaia\TranslationSheet\Commands\Output; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | class Writer | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     use Output; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** @var Collection */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     protected $translations; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** @var Spreadsheet */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     protected $spreadsheet; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** @var Filesystem */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     protected $files; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** @var Application */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     protected $app; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |     public function __construct(Spreadsheet $spreadsheet, Filesystem $files, Application $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         $this->spreadsheet = $spreadsheet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         $this->files = $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         $this->app = $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |         $this->nullOutput(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |     public function setTranslations($translations) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |         $this->translations = $translations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |     public function write() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |             ->groupTranslationsByFile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->map(function ($items, $sourceFile) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |                 $this->writeFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |                     $this->app->make('path.lang').'/'.$sourceFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |                     $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |             }); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |     protected function writeFile($file, $items) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $this->output->writeln('  '.$file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         $content = "<?php\n\nreturn ".Util::varExport($items).";\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         if (! $this->files->isDirectory($dir = dirname($file))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |             $this->files->makeDirectory($dir, 0755, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         $this->files->put($file, $content); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |     protected function groupTranslationsByFile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $items = $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             ->translations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |             ->groupBy('sourceFile') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ->map(function ($fileTranslations) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |                 return $this->buildTranslationsForFile($fileTranslations); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         // flatten does not seem to work for every case. !!! refactor !!! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         foreach ($items as $subitems) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |             $result = array_merge($result, $subitems); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         return new Collection($result); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |     protected function buildTranslationsForFile($fileTranslations) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         $files = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         $locales = $this->spreadsheet->getLocales(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         foreach ($locales as $locale) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             foreach ($fileTranslations as $translation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 // We will only write non empty translations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 // For instance, we have `app.title` that is the same for each locale, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 // We dont want to translate it to every locale, and prefer letting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 // Laravel default back to the default locale. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |                 if (!isset($translation[$locale]) || strlen($translation[$locale]) == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |                 $localeFile = str_replace('{locale}/', $locale.'/', $translation['sourceFile']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |                 if (empty($files[$localeFile])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |                     $files[$localeFile] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |                 Arr::set($files[$localeFile], $translation['key'], $translation[$locale]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         return $files; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  |