public function testLoad() { $loader = new YamlFileLoader(); $resource = __DIR__ . '/../fixtures/resources.yml'; $catalogue = $loader->load($resource, 'en', 'domain1'); $this->assertEquals(array('foo' => 'bar'), $catalogue->getMessages('domain1')); $this->assertEquals('en', $catalogue->getLocale()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); }
/** * Constructor de la Clase LocalException * Responde al RF(57) (Configurar y registrar excepciones) * * @param string $codigo Código de la excepción. * @param null $interna Excepción previa generada por el sistema. * @param string $locale Idioma a mostrar la excepción, español por defecto. * @throws \Exception excepción generada si no existe ninguna registrada con el código especificado. */ function __construct($codigo, $interna = null, $locale = null) { $arrayRutaFile = explode(DIRECTORY_SEPARATOR, $this->file); $strExtraer = ""; $direccionBundle = explode(DIRECTORY_SEPARATOR . $arrayRutaFile[count($arrayRutaFile) - 1], $this->file); for ($i = count($arrayRutaFile) - 1; $i >= 0; $i--) { if (preg_match('/[a-zA-Z0-9]Bundle$/', $arrayRutaFile[$i]) == 1) { $direccionBundle = explode($strExtraer, $this->file)[0]; $this->bundleName = $arrayRutaFile[$i]; $direccionFileExcepciones = $direccionBundle . DIRECTORY_SEPARATOR . "Resources" . DIRECTORY_SEPARATOR . "config"; if (is_dir($direccionFileExcepciones)) { break; } } $strExtraer = DIRECTORY_SEPARATOR . $arrayRutaFile[$i] . $strExtraer; } $value = $this->getArrayExcepcionesInFile($direccionFileExcepciones); if (!$this->existeExcepcionByCode($codigo, $value)) { throw new \Exception("Este bundle no tiene ninguna excepción definida con ese código, revice las excepciones definidas en :" . $direccionFileExcepciones . "/excepciones.(yml, xml)", 0, $interna); } $loader = new YamlFileLoader(); if ($locale == null) { $parcer = new Yaml(); $parametersYMl = $parcer->parse($this->getRootDir($direccionBundle) . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'parameters.yml'); $locale = $parametersYMl['parameters']['locale']; } $parametrosExcepcion = $value[$codigo]; $this->mensaje = $parametrosExcepcion['mensaje']; $this->message = $this->mensaje; $this->descripcion = $parametrosExcepcion['descripcion']; $resource = $direccionBundle . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'translations' . DIRECTORY_SEPARATOR . 'translatesexcepciones.' . $locale . '.yml'; if (is_readable($resource)) { try { $catalogo = $loader->load($resource, $locale, 'translatesexcepciones'); $parametrosExcepcion = $value[$codigo]; if (array_key_exists('mensaje', $parametrosExcepcion)) { $this->mensaje = $catalogo->get($parametrosExcepcion['mensaje'], 'translatesexcepciones'); $this->message = $this->mensaje; } if (array_key_exists('mensaje', $parametrosExcepcion)) { $this->descripcion = $catalogo->get($parametrosExcepcion['descripcion'], 'translatesexcepciones'); } } catch (NotFoundResourceException $ex) { } } if (array_key_exists('show_in_prod', $parametrosExcepcion)) { $this->showInProd = $parametrosExcepcion['show_in_prod']; } $this->codigo = $codigo; $this->code = $this->codigo; $this->clase = $this->file; $this->linea = $this->line; $this->trazas = $this->getTrace(); $this->metodo = $this->trazas[0]['function']; $this->interna = $interna; }
/** * {@inheritdoc} */ public function load($resource, $locale, $domain = 'messages') { $messages = parent::load($resource, $locale, $domain); foreach ($messages->all($domain) as $id => $translation) { $messages->set($id, $this->getTranslation($messages, $id, $domain)); } return $messages; }
/** * Ensure that translation files contain only message keys also available in the default translation. * * It's ok for a translation file to contain not all of the default translation's keys, since this happens when new functionality is * added and the translations will be completed later. * But it's not ok for a translation file to contain keys that are not available in the default translation. */ public function testYamlTranslationFilesContainNoUnknownKeys() { $loader = new YamlFileLoader(); $translations = array(); foreach ($this->getTranslationFilePaths() as $filePath) { list($domain, $locale) = explode('.', basename($filePath)); $catalogue = $loader->load($filePath, $locale, $domain); $translations[$domain][$locale] = array_keys($catalogue->all($domain)); } foreach ($translations as $domain => $locales) { foreach ($locales as $locale => $keys) { if ($locale === static::$defaultLocale) { continue; } $this->assertEquals(array(), array_diff($keys, $translations[$domain][static::$defaultLocale]), sprintf('The translation file for locale "%s" (domain "%s") contains message keys not available for locale "%s".', $locale, $domain, static::$defaultLocale)); } } }
/** * {@inheritdoc} */ public function load($resource, $locale, $domain = 'messages') { $catalogue = parent::load($resource['file'], $locale, $domain); if (!empty($resource['prefix'])) { $messages = $catalogue->all($domain); $prefixedKeys = array_map(function ($k) use($resource) { return $resource['prefix'] . $k; }, array_keys($messages)); $catalogue->replace(array_combine($prefixedKeys, $messages)); } return $catalogue; }
/** * {@inheritdoc} */ public function load($resource, $locale, $domain = 'messages') { if (!class_exists($domain)) { throw new \RuntimeException(sprintf("Could not load constants for a class '%s'.", $domain)); } $consts = (new \ReflectionClass($domain))->getConstants(); $translations = $this->yml_loader->load($resource, $locale); $merged = []; // Merge the translations in the translation file with the contact values. // So if we define a translation we will map that to the correct number // in the MessageCatalogue. Meaning we will have an array with the // constant value as key and the translation as values, or the constant // name if no translation was found. foreach ($consts as $const => $value) { if (is_array($value)) { continue; } $key = $this->getPrettyName($domain) . "." . strtolower($const); $merged[(string) $value] = $translations->has($key) ? $translations->get($key) : strtolower($const); } $catalogue = new MessageCatalogue($locale); $catalogue->add($merged, $domain); return $catalogue; }
/** * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException */ public function testLoadThrowsAnExceptionIfNotAnArray() { $loader = new YamlFileLoader(); $resource = __DIR__ . '/../fixtures/non-valid.yml'; $loader->load($resource, 'en', 'domain1'); }
/** * * {@inheritDoc} @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 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 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 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 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 \Sasedev\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(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 \Sasedev\ExtraToolsBundle\Translation\Formatter\YmlFormatter(); } elseif ($input->getOption('output-format') == 'php') { $formatter = new \Sasedev\ExtraToolsBundle\Translation\Formatter\PhpFormatter(); } elseif ($input->getOption('output-format') == 'pot') { $formatter = new \Sasedev\ExtraToolsBundle\Translation\Formatter\PotFormatter(); } else { $formatter = new \Sasedev\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))); } } } }