/** * @param $transFile * @param $resourceDirs * @param $locale * @param $loader * @param $globalCatalogue * @return MessageCatalogue */ protected function merge($transFile, &$resourceDirs, Locale $locale, TranslationLoader $loader, $globalCatalogue) { $transFileDir = dirname($transFile); if (!in_array($transFileDir, $resourceDirs) && '.' != $transFileDir) { $resourceDirs[] = $transFileDir; $currentCatalogue = new MessageCatalogue($locale->getAlias()); $loader->loadMessages($transFileDir, $currentCatalogue); $operation = new MergeOperation($globalCatalogue, $currentCatalogue); $globalCatalogue = $operation->getResult(); return $globalCatalogue; } return $globalCatalogue; }
/** * Preload existring translations to check against duplicates * * @param string $locale */ protected function preloadExistingTranslations($locale) { foreach ($this->bundles as $bundle) { $translationsPath = $bundle->getPath() . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'translations'; $currentCatalogue = new MessageCatalogue($locale); if ($this->filesystem->exists($translationsPath)) { $this->loader->loadMessages($translationsPath, $currentCatalogue); $this->loadedTranslations[$bundle->getName()] = $currentCatalogue; } } }
/** * @param string $targetLocale */ protected function dumpMessages($targetLocale) { // load downloaded messages $this->logger->notice('Loading downloaded catalogues from "{tmpPath}"', ['tmpPath' => $this->getTmpPath()]); $extractedCatalogue = new MessageCatalogue($targetLocale); $this->translationLoader->loadMessages($this->getTmpPath(), $extractedCatalogue); // Exit if no messages found. if (0 === count($extractedCatalogue->getDomains())) { $this->logger->warning('No translation found for locale {locale}', ['locale' => $targetLocale]); return; } $this->logger->notice('Writing translation file for locale "{locale}".', ['locale' => $targetLocale]); $this->translationWriter->writeTranslations($extractedCatalogue, $this->outputFormat, ['path' => $this->translationsPath]); }
/** * Apply downloaded and extracted language packs to Symfony, in app/Resources dir * Returns applied locale codes * * @param string $locale * @param string $sourceDir */ protected function apply($locale, $sourceDir) { $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($sourceDir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST); $catalog = new MessageCatalogue($locale); /** @var \SplFileInfo $fileInfo */ foreach ($iterator as $fileInfo) { if ($iterator->getDepth() < 1 || $fileInfo->isDir()) { continue; } // fix bad formatted yaml that may come from third-party service YamlFixer::fixStrings($fileInfo->getPathname()); } $this->translationLoader->loadMessages($sourceDir, $catalog); $this->databasePersister->persist($locale, $catalog->all()); }
/** * @param string $locale * @param array $transPaths * @param TranslationLoader $loader * * @return MessageCatalogue[] */ private function loadFallbackCatalogues($locale, $transPaths, TranslationLoader $loader) { $fallbackCatalogues = array(); $translator = $this->getContainer()->get('translator'); if ($translator instanceof Translator) { foreach ($translator->getFallbackLocales() as $fallbackLocale) { if ($fallbackLocale === $locale) { continue; } $fallbackCatalogue = new MessageCatalogue($fallbackLocale); foreach ($transPaths as $path) { $path = $path . 'translations'; if (is_dir($path)) { $loader->loadMessages($path, $fallbackCatalogue); } } $fallbackCatalogues[] = $fallbackCatalogue; } } return $fallbackCatalogues; }
/** * @param string $source * @param string $locale * * @return MessageCatalogueInterface | null */ public function extract($source, $locale) { if (!$this->isSourceAvailable($source)) { return; } $fs = new Filesystem(); /* @var Bundle $foundBundle */ $foundBundle = $this->kernel->getBundle($this->bundle); // load any messages from templates $extractedCatalogue = new MessageCatalogue($locale); $resourcesDir = $this->resolveResourcesDirectory($foundBundle); if ($fs->exists($resourcesDir)) { $this->extractor->extract($resourcesDir, $extractedCatalogue); } // load any existing messages from the translation files $translationsDir = $foundBundle->getPath() . '/Resources/translations'; if ($fs->exists($translationsDir)) { $currentCatalogue = new MessageCatalogue($locale); $this->loader->loadMessages($translationsDir, $currentCatalogue); foreach ($extractedCatalogue->getDomains() as $domain) { $messages = $currentCatalogue->all($domain); if (count($messages)) { $extractedCatalogue->add($messages, $domain); } } } return $extractedCatalogue; }