/** * @param TranslatorConfig $config * @param Translator $translator * @return void */ public function perform(TranslatorConfig $config, Translator $translator) { if (!$config->hasDumper($this->option('dumper'))) { $this->writeln("<fg=red>Undefined dumper '{$this->option('dumper')}'.</fg=red>"); return null; } $catalogue = $translator->getCatalogue($this->argument('locale'))->loadDomains(); if ($this->option('fallback')) { //Let's merge with fallback locale $fallbackCatalogue = $translator->getCatalogue($config->fallbackLocale())->loadDomains(); //Soft merge $catalogue->mergeFrom($fallbackCatalogue->toMessageCatalogue(), false); } //Pre-loading all domains $messageCatalogue = $catalogue->toMessageCatalogue(); if ($this->isVerbosity() && !empty($catalogue->getDomains())) { $this->writeln("<info>Dumping domain(s):</info> " . join(',', $catalogue->getDomains())); } $dumper = $config->dumperClass($this->option('dumper')); /** * @var DumperInterface $dumper */ $dumper = new $dumper(); $dumper->dump($messageCatalogue, ['path' => $this->argument('path'), 'default_locale' => $config->defaultLocale(), 'xliff_version' => '2.0']); $this->writeln("Dump successfully completed."); }
/** * Register found invocations in translator bundles. * * @param ReflectionInvocation[] $invocations */ protected function registerInvocations(array $invocations) { foreach ($invocations as $invocation) { if ($invocation->countArguments() < 1) { //This is not valid invocation continue; } if ($invocation->argument(0)->getType() != ReflectionArgument::STRING) { //We can only index invocations with constant string arguments continue; } $this->logger()->debug("Found invocation of '{invocation}' in '{file}' at line {line}.", ['invocation' => $invocation->getName(), 'file' => $invocation->getFilename(), 'line' => $invocation->getLine()]); $string = $invocation->argument(0)->stringValue(); if ($invocation->getName() == 'say') { $string = $this->removeBraces($string); } //Translation using default bundle $domain = TranslatorInterface::DEFAULT_DOMAIN; if ($invocation->getName() == 'say') { $domain = $this->translator->resolveDomain($invocation->getClass()); } else { //L or P functions if ($invocation->countArguments() >= 3) { if ($invocation->argument(2)->getType() != ReflectionArgument::STRING) { //Unable to resolve domain name continue; } $domain = $this->translator->resolveDomain($invocation->argument(2)->stringValue()); } } $this->register($domain, $string); } }
/** * Handle usage of translator method belongs to TranslatorTrait. * * @param ReflectionCall $call */ protected function indexTrait(ReflectionCall $call) { if (!in_array(TranslatorTrait::class, $this->tokenizer->getTraits($call->getClass()))) { return; } $string = $call->argument(0)->stringValue(); if (substr($string, 0, 2) == Translator::I18N_PREFIX || substr($string, -2) == Translator::I18N_POSTFIX) { //This string was defined in class attributes $string = substr($string, 2, -2); } $this->translator->translate($call->getClass(), $string); $this->register($call->getFilename(), $call->getLine(), $call->getClass(), $string, $call->getClass()); }
/** * Load all bundle strings from specified language. * * @param string $language * @param string $prefix Only bundle names started with this prefix will be exported. * @return array */ private function loadBundles($language, $prefix = '') { $bundles = $this->files->getFiles($this->translator->config()['languages'][$language]['directory']); $result = []; foreach ($bundles as $filename) { $bundle = substr(basename($filename), 0, -1 * strlen($this->files->extension($filename))); if (!empty($prefix) && stripos($bundle, $prefix) !== 0) { continue; } try { $result[$bundle] = (include $filename); } catch (\Exception $exception) { } } return $result; }
/** * {@inheritdoc} */ public function import($replace = false) { if (empty($this->language)) { throw new ImporterException("Unable to perform bundles import, no language detected."); } if (!isset($this->translator->config()['languages'][$this->language])) { throw new ImporterException("Unable to import language '{$this->language}', no presets found."); } $directory = $this->translator->config()['languages'][$this->language]['directory']; foreach ($this->bundles as $bundle => $strings) { if (!$replace && !empty($existed = $this->memory->loadData($bundle, $directory))) { $strings = $strings + $existed; } $this->memory->saveData($bundle, $strings, $directory); } }
/** * @param Translator $translator */ public function perform(Translator $translator) { $translator->flushLocales()->loadLocales(); $this->writeln("Translation cache has been reloaded."); }