/** * Export the translations * * @param Translations $translations The translations object * @param string $bundle The bundle * @param string $domain The domain * @param string $locale The locale * @param string $filename The filename to export * @return $this * @throws FileCreatorException */ public function createFile(Translations $translations, $bundle, $domain, $locale, $filename) { $messages = $translations->getMessages($bundle, $domain, $locale); if (count($messages) > 0) { $data = $this->prepareYamlArray($messages); $buffer = '# ' . $bundle . '/' . $domain . '.' . $locale . '.yml' . PHP_EOL; $buffer .= $this->yamlDumper->dump($data, 100); $buffer .= PHP_EOL . PHP_EOL; $this->filePutContents($filename, $buffer); } return $this; }
/** * Export the translations * * @param Translations $translations The translations to export * @param array $bundles List of bundles (empty array: all) * @param array $domains List of domains (empty array: all) * @param array $locales List of locales (empty array: all) * @param string $filename The filename to export * @return Response * @throws ExporterException */ public function export(Translations $translations, array $bundles = array(), array $domains = array(), array $locales = array(), $filename = null) { // Current date and time $now = new \DateTime(); // Validate the params if (!count($bundles)) { $bundles = $translations->getBundles(); } if (!count($domains)) { $domains = $translations->getDomains(); } if (!count($locales)) { $locales = $translations->getLocales(); } if (null == $filename) { $filename = 'davamigo_translator_' . $now->format('Y-m-d_H-i-s') . '.yml'; } // Create the Yaml file $buffer = ''; foreach ($bundles as $bundle) { foreach ($domains as $domain) { foreach ($locales as $locale) { $messages = $translations->getMessages($bundle, $domain, $locale); if (count($messages) > 0) { $buffer .= '# ' . str_repeat('-', 80) . PHP_EOL; $buffer .= '# ' . $bundle . '/' . $domain . '.' . $locale . '.yml' . PHP_EOL; try { $data = $this->prepareYamlArray($messages); $buffer .= $this->yamlDumper->dump($data, 100); } catch (InvalidResourceException $exc) { $buffer .= $exc->getMessage(); } $buffer .= PHP_EOL . PHP_EOL; } } } } // Create the response return $this->createResponse($buffer, $filename); }
/** * Basic test of get messages */ public function testGetMessagesWithNullParamsThrowsAnException() { // Configure the test $this->setExpectedException('Davamigo\\TranslatorBundle\\Model\\Translator\\Exception\\InvalidArgumentException'); // Run the test $translations = new Translations(); $translations->getMessages(null, null, null); }