Exemple #1
0
 public function writeTranslationFile(TranslationEvent $event)
 {
     $file = $event->getTranslationFilePath();
     $fs = new Filesystem();
     if (!$fs->exists($file) && true === $event->isCreateFileIfNotExists()) {
         $dir = dirname($file);
         if (!$fs->exists($file)) {
             $fs->mkdir($dir);
             $this->cacheClear($event->getDispatcher());
         }
     }
     if ($fp = @fopen($file, 'w')) {
         fwrite($fp, '<' . "?php\n\n");
         fwrite($fp, "return array(\n");
         $texts = $event->getTranslatableStrings();
         $translations = $event->getTranslatedStrings();
         // Sort keys alphabetically while keeping index
         asort($texts);
         foreach ($texts as $key => $text) {
             // Write only defined (not empty) translations
             if (!empty($translations[$key])) {
                 $text = str_replace("'", "\\'", $text);
                 $translation = str_replace("'", "\\'", $translations[$key]);
                 fwrite($fp, sprintf("    '%s' => '%s',\n", $text, $translation));
             }
         }
         fwrite($fp, ");\n");
         @fclose($fp);
     } else {
         throw new \RuntimeException(Translator::getInstance()->trans('Failed to open translation file %file. Please be sure that this file is writable by your Web server', array('%file' => $file)));
     }
 }