public function testExtract()
 {
     $input = new ArgvInput(array('app/console', 'translation:extract', 'en', '--dir=' . ($inputDir = __DIR__ . '/../Translation/Extractor/Fixture/SimpleTest'), '--output-dir=' . ($outputDir = sys_get_temp_dir() . '/' . uniqid('extract'))));
     $expectedOutput = 'Keep old translations: No' . "\n" . 'Output-Path: ' . $outputDir . "\n" . 'Directories: ' . $inputDir . "\n" . 'Excluded Directories: Tests' . "\n" . 'Excluded Names: *Test.php, *TestCase.php' . "\n" . 'Output-Format: # whatever is present, if nothing then xliff #' . "\n" . 'Custom Extractors: # none #' . "\n" . '============================================================' . "\n" . 'Loading catalogues from "' . $outputDir . '"' . "\n" . 'Extracting translation keys' . "\n" . 'Extracting messages from directory : ' . $inputDir . "\n" . 'Writing translation file "' . $outputDir . '/messages.en.xliff".' . "\n" . 'done!' . "\n";
     $this->getApp()->run($input, $output = new Output());
     $this->assertEquals($expectedOutput, $output->getContent());
     $files = FileUtils::findTranslationFiles($outputDir);
     $this->assertTrue(isset($files['messages']['en']));
 }
 /**
  * @Route("/configs/{config}/domains/{domain}/locales/{locale}/messages",
  *          name="jms_translation_delete_message",
  *          defaults = {"id" = null},
  *          options = {"i18n" = false})
  * @Method("DELETE")
  */
 public function deleteMessageAction(Request $request, $config, $domain, $locale)
 {
     $id = $request->query->get('id');
     $config = $this->configFactory->getConfig($config, $locale);
     $files = FileUtils::findTranslationFiles($config->getTranslationsDir());
     if (!isset($files[$domain][$locale])) {
         throw new RuntimeException(sprintf('There is no translation file for domain "%s" and locale "%s".', $domain, $locale));
     }
     list($format, $file) = $files[$domain][$locale];
     $this->updater->deleteTranslation($file, $format, $domain, $locale, $id, $this->request->request->get('message'));
     return new Response();
 }
 /**
  * @param array $dirs
  * @return array
  */
 private function retrieveFiles(array $dirs)
 {
     $files = array();
     // Register translation resources
     foreach ($dirs as $dir) {
         foreach (FileUtils::findTranslationFiles($dir) as $catalogue => $locales) {
             foreach ($locales as $file) {
                 $files[] = $file[1];
             }
         }
     }
     return $files;
 }
 /**
  * @Route("/configs/{config}/domains/{domain}/locales/{locale}/messages",
  * 			name="jms_translation_update_message",
  * 			defaults = {"id" = null},
  * 			options = {"i18n" = false})
  * @Method("PUT")
  */
 public function updateMessageAction(Request $request, $config, $domain, $locale)
 {
     $id = $request->query->get('id');
     $config = $this->configFactory->getConfig($config, $locale);
     $files = FileUtils::findTranslationFiles($config->getTranslationsDir());
     if (!isset($files[$domain][$locale])) {
         throw new RuntimeException(sprintf('There is no translation file for domain "%s" and locale "%s".', $domain, $locale));
     }
     // TODO: This needs more refactoring, the only sane way I see right now is to replace
     //       the loaders of the translation component as these currently simply discard
     //       the extra information that is contained in these files
     list($format, $file) = $files[$domain][$locale];
     $this->updater->updateTranslation($file, $format, $domain, $locale, $id, $this->request->request->get('message'));
     return new Response();
 }
 /**
  * @param $dir
  * @param $targetLocale
  * @return \JMS\TranslationBundle\Model\MessageCatalogue
  */
 public function loadFromDirectory($dir, $targetLocale)
 {
     $files = FileUtils::findTranslationFiles($dir);
     $catalogue = new MessageCatalogue();
     $catalogue->setLocale($targetLocale);
     foreach ($files as $domain => $locales) {
         foreach ($locales as $locale => $data) {
             if ($locale !== $targetLocale) {
                 continue;
             }
             list($format, $file) = $data;
             $catalogue->merge($this->getLoader($format)->load($file, $locale, $domain));
         }
     }
     return $catalogue;
 }
 /**
  * @Route("/", name="jms_translation_index", options = {"i18n" = false})
  * @Template
  * @param string $config
  */
 public function indexAction()
 {
     $configs = $this->configFactory->getNames();
     $config = $this->request->query->get('config') ?: reset($configs);
     if (!$config) {
         throw new RuntimeException('You need to configure at least one config under "jms_translation.configs".');
     }
     $translationsDir = $this->configFactory->getConfig($config, 'en')->getTranslationsDir();
     $files = FileUtils::findTranslationFiles($translationsDir);
     if (empty($files)) {
         throw new RuntimeException('There are no translation files for this config, please run the translation:extract command first.');
     }
     $domains = array_keys($files);
     $domain = $this->request->query->get('domain') ?: reset($domains);
     if (!($domain = $this->request->query->get('domain')) || !isset($files[$domain])) {
         $domain = reset($domains);
     }
     $locales = array_keys($files[$domain]);
     natsort($locales);
     if (!($locale = $this->request->query->get('locale')) || !isset($files[$domain][$locale])) {
         $locale = reset($locales);
     }
     $catalogue = $this->loader->loadFile($files[$domain][$locale][1]->getPathName(), $files[$domain][$locale][0], $locale, $domain);
     // create alternative messages
     // TODO: We should probably also add these to the XLIFF file for external translators,
     //       and the specification already supports it
     $alternativeMessages = array();
     foreach ($locales as $otherLocale) {
         if ($locale === $otherLocale) {
             continue;
         }
         $altCatalogue = $this->loader->loadFile($files[$domain][$otherLocale][1]->getPathName(), $files[$domain][$otherLocale][0], $otherLocale, $domain);
         foreach ($altCatalogue->getDomain($domain)->all() as $id => $message) {
             $alternativeMessages[$id][$otherLocale] = $message;
         }
     }
     $newMessages = $existingMessages = array();
     foreach ($catalogue->getDomain($domain)->all() as $id => $message) {
         if ($message->isNew()) {
             $newMessages[$id] = $message;
             continue;
         }
         $existingMessages[$id] = $message;
     }
     return array('selectedConfig' => $config, 'configs' => $configs, 'selectedDomain' => $domain, 'domains' => $domains, 'selectedLocale' => $locale, 'locales' => $locales, 'format' => $files[$domain][$locale][0], 'newMessages' => $newMessages, 'existingMessages' => $existingMessages, 'alternativeMessages' => $alternativeMessages, 'isWriteable' => is_writeable($files[$domain][$locale][1]), 'file' => (string) $files[$domain][$locale][1], 'sourceLanguage' => $this->sourceLanguage);
 }
 /**
  * Detects the most suitable output format to use.
  *
  * @param $currentDomain
  * @return string
  * @internal param string $domain
  */
 private function detectOutputFormat($currentDomain)
 {
     if (null !== $this->config->getOutputFormat()) {
         return $this->config->getOutputFormat();
     }
     // check if which translation files in which format exist
     $otherDomainFormat = $localeFormat = $otherLocaleFormat = null;
     foreach (FileUtils::findTranslationFiles($this->config->getTranslationsDir()) as $domain => $locales) {
         foreach ($locales as $locale => $fileData) {
             list($format, ) = $fileData;
             if ($currentDomain !== $domain) {
                 $otherDomainFormat = $format;
                 continue 2;
             }
             if ($this->config->getLocale() === $locale) {
                 $localeFormat = $format;
                 continue;
             }
             $otherLocaleFormat = $format;
         }
     }
     if (null !== $localeFormat) {
         return $localeFormat;
     }
     if (null !== $otherLocaleFormat) {
         return $otherLocaleFormat;
     }
     if (null !== $otherDomainFormat) {
         return $otherDomainFormat;
     }
     return $this->config->getDefaultOutputFormat();
 }
 /**
  * 
  * 
  * @param MessageCatalogue $catalogue  A cataloque
  * @param string           $userLocale A locale
  * @param string           $domain     The domain
  * 
  * @return MessageCatalogue A MessageCatalogue instance
  * @access private
  * @since  2012-11-14
  */
 private function bundlesLoad(MessageCatalogue $catalogue, $userLocale, $domain = 'messages')
 {
     // get all bundles translaions in user locale
     $bundles = $this->container->get("kernel")->getBundles();
     if (is_array($bundles)) {
         foreach ($bundles as $bundle) {
             $dir_path = realpath($bundle->getPath() . '/Resources/translations/');
             if ($dir_path) {
                 $files = \JMS\TranslationBundle\Util\FileUtils::findTranslationFiles($dir_path);
                 foreach ($files as $domain => $locales) {
                     foreach ($locales as $locale => $data) {
                         if ($locale !== $userLocale) {
                             continue;
                         }
                         list($format, $file) = $data;
                         // merge catalogues
                         try {
                             $loader = $this->loadFile($file, $format, $locale, $domain);
                             $catalogue->addCatalogue($loader);
                         } catch (\Exception $e) {
                         }
                     }
                 }
             }
         }
     }
     return $catalogue;
 }
Esempio n. 9
0
 /**
  * @return array
  */
 private function getFiles()
 {
     if (empty($this->_files)) {
         $this->_files = FileUtils::findTranslationFiles($this->path);
     }
     return $this->_files;
 }