/** * Import the translations * * @param UploadedFile|string $file The filename to read * @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) * @return Translations * @throws ImporterException */ public function import($file, Translations $translations, array $bundles = array(), array $domains = array(), array $locales = array()) { // Init $this->readResources = 0; $this->newTranslations = 0; // Get the file name $originalName = $this->getFileName($file); // Read the Excel file $worksheet = $this->readExcelFile($file); // Validate the headers try { $headers = $this->validateHeader($worksheet); } catch (\Exception $exc) { throw new ImporterException('Invalid Excel file ' . $originalName, 0, $exc); } $length = count($headers); $row = 1; do { ++$row; $bundle = $worksheet->getCellByColumnAndRow(0, $row)->getValue(); $domain = $worksheet->getCellByColumnAndRow(1, $row)->getValue(); $resource = $worksheet->getCellByColumnAndRow(2, $row)->getValue(); if ($bundle && $domain && $resource) { ++$this->readResources; if ((empty($bundles) || in_array($bundle, $bundles)) && (empty($domains) || in_array($domain, $domains))) { // for ($col = count(static::$fixedHeader); $col < $length; ++$col) { $locale = $headers[$col]; if (empty($locales) || in_array($locale, $locales)) { $value = $worksheet->getCellByColumnAndRow($col, $row)->getValue(); $current = $translations->getTranslation($bundle, $domain, $locale, $resource); if ($value && $value != $current) { $translations->addTranslation($bundle, $domain, $locale, $resource, $value); ++$this->newTranslations; } } } } } } while ($bundle && $domain && $resource); return $translations; }
/** * 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') . '.xls'; } // Create the Excel object $excelObj = $this->phpExcelFactory->createPHPExcelObject(); if (null == $excelObj) { throw new ExporterException('Can\'t create the Excel object.'); } // Se the Excel properties $excelObj->getProperties()->setTitle('Davamigo Symfony Translations')->setDescription('Davamigo Symfony Translations')->setCompany('Davamigo')->setCreator('Davamigo')->setLastModifiedBy('Davamigo')->setCreated($now->getTimestamp())->setModified($now->getTimestamp()); // Create active sheet $excelObj->setActiveSheetIndex(0); $worksheet = $excelObj->getSheet(0); $worksheet->setTitle('Translations'); // Create the Excel header (first row) $row = 1; $col = 0; $headers = array_merge(static::$fixedHeader, $locales); foreach ($headers as $string) { $cell = $worksheet->getCellByColumnAndRow($col, $row); $cell->setValue($string); $style = $worksheet->getStyle($cell->getCoordinate()); $style->applyFromArray(static::$headerCellStyle); ++$col; } foreach ($bundles as $bundle) { foreach ($domains as $domain) { $resources = $translations->getResources($bundle, $domain); foreach ($resources as $resource) { // $content = array($bundle, $domain, $resource); foreach ($locales as $locale) { $content[] = $translations->getTranslation($bundle, $domain, $locale, $resource); } ++$row; $col = 0; foreach ($content as $string) { $cell = $worksheet->getCellByColumnAndRow($col, $row); $cell->setValue($string); $style = $worksheet->getStyle($cell->getCoordinate()); $style->applyFromArray(static::$textCellStyle); ++$col; } } } } // Auto-size all columns for ($col = 0; $col < count($headers); ++$col) { $dimension = $worksheet->getColumnDimensionByColumn($col); $dimension->setAutoSize(true); } // Create the response return $this->createResponse($excelObj, $filename); }
/** * Basic test of get translation */ public function testGetTranslationWithNullParamsThrowsAnException() { // Configure the test $this->setExpectedException('Davamigo\\TranslatorBundle\\Model\\Translator\\Exception\\InvalidArgumentException'); // Run the test $translations = new Translations(); $translations->getTranslation(null, null, null, null); }