public function writeFallbackFile(TranslationEvent $event) { $file = THELIA_LOCAL_DIR . 'I18n' . DS . $event->getLocale() . '.php'; $fs = new Filesystem(); $translations = []; if (!$fs->exists($file)) { if (true === $event->isCreateFileIfNotExists()) { $dir = dirname($file); $fs->mkdir($dir); $this->cacheClear($event->getDispatcher()); } 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))); } } else { /*$loader = new PhpFileLoader(); $catalogue = $loade r->load($file); $translations = $catalogue->all(); */ $translations = (require $file); if (!is_array($translations)) { $translations = []; } } if ($fp = @fopen($file, 'w')) { $texts = $event->getTranslatableStrings(); $customs = $event->getCustomFallbackStrings(); $globals = $event->getGlobalFallbackStrings(); // just reset current translations for this domain to remove strings that do not exist anymore $translations[$event->getDomain()] = []; foreach ($texts as $key => $text) { if (!empty($customs[$key])) { $translations[$event->getDomain()][$text] = $customs[$key]; } if (!empty($globals[$key])) { $translations[$text] = $globals[$key]; } else { unset($translations[$text]); } } fwrite($fp, '<' . "?php\n\n"); fwrite($fp, "return [\n"); // Sort keys alphabetically while keeping index ksort($translations); foreach ($translations as $key => $text) { // Write only defined (not empty) translations if (!empty($translations[$key])) { if (is_array($translations[$key])) { $key = str_replace("'", "\\'", $key); fwrite($fp, sprintf(" '%s' => [\n", $key)); ksort($translations[$key]); foreach ($translations[$key] as $subKey => $subText) { $subKey = str_replace("'", "\\'", $subKey); $translation = str_replace("'", "\\'", $subText); fwrite($fp, sprintf(" '%s' => '%s',\n", $subKey, $translation)); } fwrite($fp, " ],\n"); } else { $key = str_replace("'", "\\'", $key); $translation = str_replace("'", "\\'", $text); fwrite($fp, sprintf(" '%s' => '%s',\n", $key, $translation)); } } } fwrite($fp, "];\n"); @fclose($fp); } }