コード例 #1
0
 /**
  * Load translations and metadata of the trans-unit.
  *
  * {@inheritdoc}
  *
  * @api
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     /* @var MessageCatalogue $catalogue */
     $base_catalogue = parent::load($resource, $locale, $domain);
     $catalogue = new MessageCatalogue($locale);
     $catalogue->addCatalogue($base_catalogue);
     // Process a second pass over the file to collect metadata
     $xml = simplexml_load_file($resource);
     $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
     foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
         // Read the attributes
         $attributes = (array) $translation->attributes();
         $attributes = $attributes['@attributes'];
         if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) {
             continue;
         }
         $key = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
         $metadata = (array) $attributes;
         // read the notes
         if (isset($translation->note)) {
             $metadata['note'] = (string) $translation->note;
         }
         $catalogue->setMetadata((string) $key, $metadata, $domain);
     }
     return $catalogue;
 }
コード例 #2
0
ファイル: TranslationLoader.php プロジェクト: ratasxy/symfony
 /**
  * Loads translation messages from a directory to the catalogue.
  * 
  * @param string $directory the directory to look into
  * @param MessageCatalogue $catalogue the catalogue
  */
 public function loadMessages($directory, MessageCatalogue $catalogue)
 {
     foreach($this->loaders as $format => $loader) {
         // load any existing translation files
         $finder = new Finder();
         $files = $finder->files()->name('*.'.$catalogue->getLocale().$format)->in($directory);
         foreach ($files as $file) {
             $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale').$format) - 1);
             $catalogue->addCatalogue($loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
         }
     }
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function getDefaultCatalogue($empty = true)
 {
     $defaultCatalogue = new MessageCatalogue($this->getLocale());
     foreach ($this->getFilters() as $filter) {
         $filteredCatalogue = $this->getCatalogueFromPaths(array($this->getDefaultResourceDirectory()), $this->getLocale(), $filter);
         $defaultCatalogue->addCatalogue($filteredCatalogue);
     }
     if ($empty) {
         $defaultCatalogue = $this->emptyCatalogue($defaultCatalogue);
     }
     return $defaultCatalogue;
 }
コード例 #4
0
ファイル: TranslationLoader.php プロジェクト: rikaix/symfony
 /**
  * Loads translation messages from a directory to the catalogue.
  *
  * @param string $directory the directory to look into
  * @param MessageCatalogue $catalogue the catalogue
  */
 public function loadMessages($directory, MessageCatalogue $catalogue)
 {
     foreach ($this->loaders as $format => $loader) {
         // load any existing translation files
         $finder = new Finder();
         $extension = $catalogue->getLocale() . '.' . $format;
         $files = $finder->files()->name('*.' . $extension)->in($directory);
         foreach ($files as $file) {
             $domain = substr($file->getFileName(), 0, -1 * strlen($extension) - 1);
             $catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
         }
     }
 }
コード例 #5
0
 public function getCatalogueFromPaths($paths, $locale, $pattern = null)
 {
     $messageCatalogue = new MessageCatalogue($locale);
     $xliffFileLoader = new XliffFileLoader();
     $finder = new Finder();
     if (null !== $pattern) {
         $finder->name($pattern);
     }
     $translationFiles = $finder->files()->in($paths);
     if (count($translationFiles) === 0) {
         throw new \Exception('There is no translation file available.');
     }
     foreach ($translationFiles as $file) {
         $fileCatalogue = $xliffFileLoader->load($file->getPathname(), $locale, $file->getBasename('.xlf'));
         $messageCatalogue->addCatalogue($fileCatalogue);
     }
     return $messageCatalogue;
 }
コード例 #6
0
 /**
  * @param string $format
  * @param string $resource
  * @param string $domain
  * @param MessageCatalogue $catalogue
  * @throws LoaderNotFoundException
  */
 public function loadResource($format, $resource, $domain, MessageCatalogue $catalogue)
 {
     if (!isset($this->loaders[$format])) {
         if (!isset($this->serviceIds[$format])) {
             throw new LoaderNotFoundException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
         }
         $this->loaders[$format] = $this->serviceLocator->getService($this->serviceIds[$format]);
         unset($this->serviceIds[$format]);
     }
     $catalogue->addCatalogue($this->loaders[$format]->load($resource, $catalogue->getLocale(), $domain));
 }
コード例 #7
0
 public function testMetadataMerge()
 {
     $cat1 = new MessageCatalogue('en');
     $cat1->setMetadata('a', 'b');
     $this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
     $cat2 = new MessageCatalogue('en');
     $cat2->setMetadata('b', 'c', 'domain');
     $this->assertEquals(array('domain' => array('b' => 'c')), $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');
     $cat1->addCatalogue($cat2);
     $this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
 }
コード例 #8
0
 /**
  * @expectedException LogicException
  */
 public function testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne()
 {
     $catalogue = new MessageCatalogue('en');
     $catalogue->addCatalogue(new MessageCatalogue('fr', array()));
 }
コード例 #9
0
ファイル: AbstractProvider.php プロジェクト: M03G/PrestaShop
 /**
  * Get the Catalogue from database only.
  *
  * @return MessageCatalogue A MessageCatalogue instance
  */
 public function getDatabaseCatalogue()
 {
     $databaseCatalogue = new MessageCatalogue($this->locale);
     foreach ($this->getTranslationDomains() as $translationDomain) {
         $domainCatalogue = $this->getDatabaseLoader()->load(null, $this->locale, $translationDomain);
         if ($domainCatalogue instanceof MessageCatalogue) {
             $databaseCatalogue->addCatalogue($domainCatalogue);
         }
     }
     return $databaseCatalogue;
 }
コード例 #10
0
    /**
     * @see Command
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $twig = $this->getContainer()->get('twig');
        $this->prefix = $input->getOption('prefix');

        if ($input->getOption('force') !== true && $input->getOption('dump-messages') !== true) {
            $output->writeln('You must choose one of --force or --dump-messages');
        } else {

            // get bundle directory
            $foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
            $bundleTransPath = $foundBundle->getPath() . '/Resources/translations';
            $output->writeln(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $foundBundle->getName()));

            $output->writeln('Parsing files.');

            // load any messages from templates
            $this->messages = new \Symfony\Component\Translation\MessageCatalogue($input->getArgument('locale'));
            $finder = new Finder();
            $files = $finder->files()->name('*.html.twig')->in($foundBundle->getPath() . '/Resources/views/');
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing template <comment>%s</comment>', $file->getPathname()));
                $tree = $twig->parse($twig->tokenize(file_get_contents($file->getPathname())));
                $this->_crawlNode($tree);
            }

            // load any existing yml translation files
            $finder = new Finder();
            $files = $finder->files()->name('*.' . $input->getArgument('locale') . '.yml')->in($bundleTransPath);
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing translation <comment>%s</comment>', $file->getPathname()));
                $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale') . '.yml') - 1);
                $yml_loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
                $this->messages->addCatalogue($yml_loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
            }

            // load any existing xliff translation files
            $finder = new Finder();
            $files = $finder->files()->name('*.' . $input->getArgument('locale') . '.xliff')->in($bundleTransPath);
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing translation <comment>%s</comment>', $file->getPathname()));
                $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale') . '.xliff') - 1);
                $loader = new \Symfony\Component\Translation\Loader\XliffFileLoader();
                $this->messages->addCatalogue($loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
            }

            // load any existing php translation files
            $finder = new Finder();
            $files = $finder->files()->name('*.' . $input->getArgument('locale') . '.php')->in($bundleTransPath);
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing translation <comment>%s</comment>', $file->getPathname()));
                $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale') . '.php') - 1);
                $loader = new \Symfony\Component\Translation\Loader\PhpFileLoader();
                $this->messages->addCatalogue($loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
            }

            // load any existing pot translation files
            $finder = new Finder();
            $files = $finder->files()->name('*.' . $input->getArgument('locale') . '.pot')->in($bundleTransPath);
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing translation <comment>%s</comment>', $file->getPathname()));
                $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale') . '.pot') - 1);
                $loader = new \BCC\ExtraToolsBundle\Translation\Loader\PotFileLoader();
                $this->messages->addCatalogue($loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
            }

            // show compiled list of messages
            if($input->getOption('dump-messages') === true){
                foreach ($this->messages->getDomains() as $domain) {
                    $output->writeln(sprintf("\nDisplaying messages for domain <info>%s</info>:\n", $domain));
                    $output->writeln(\Symfony\Component\Yaml\Yaml::dump($this->messages->all($domain),10));
                }
            }

            // save the files
            if($input->getOption('force') === true) {
                $output->writeln("\nWriting files.\n");
                $path = $foundBundle->getPath() . '/Resources/translations/';
                if ($input->getOption('output-format') == 'yml') {
                    $formatter = new \BCC\ExtraToolsBundle\Translation\Formatter\YmlFormatter();
                } elseif ($input->getOption('output-format') == 'php') {
                    $formatter = new \BCC\ExtraToolsBundle\Translation\Formatter\PhpFormatter();
                } elseif ($input->getOption('output-format') == 'pot') {
                    $formatter = new \BCC\ExtraToolsBundle\Translation\Formatter\PotFormatter();
                } else {
                    $formatter = new \BCC\ExtraToolsBundle\Translation\Formatter\XliffFormatter($input->getOption('source-lang'));
                }
                foreach ($this->messages->getDomains() as $domain) {
                    $file = $domain . '.' . $input->getArgument('locale') . '.' . $input->getOption('output-format');
                    if (file_exists($path . $file)) {
                        copy($path . $file, $path . '~' . $file . '.bak');
                    }
                    $output->writeln(sprintf(' > generating <comment>%s</comment>', $path . $file));
                    file_put_contents($path . $file, $formatter->format($this->messages->all($domain)));
                }
            }
        }
    }
コード例 #11
0
 /**
  * 
  * 
  * @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;
 }