/** * Index translator related function calls. * * @param ReflectionCall[] $calls * @throws IndexerException */ private function indexCalls(array $calls) { foreach ($calls as $call) { $firstArgument = $call->argument(0); if (empty($firstArgument) || $firstArgument->getType() != ReflectionArgument::STRING) { //Every translation function require first argument to be a string, not expression continue; } if ($call->getName() == 'p' || $call->getName() == 'pluralize') { $this->translator->pluralize($firstArgument->stringValue(), 0); //Registering plural usage $this->register($call->getFilename(), $call->getLine(), $this->translator->config()['plurals'], $firstArgument->stringValue()); } if ($call->getName() == 'l') { $this->translator->translate(Translator::DEFAULT_BUNDLE, $firstArgument->stringValue()); //Translate using default bundle $this->register($call->getFilename(), $call->getLine(), Translator::DEFAULT_BUNDLE, $firstArgument->stringValue()); } if ($call->getName() == 'translate') { $secondArgument = $call->argument(1); if (empty($secondArgument) || $secondArgument->getType() != ReflectionArgument::STRING) { //We can only use static strings continue; } //Registering string $this->translator->translate($firstArgument->stringValue(), $secondArgument->stringValue()); //Translate with specified bundle $this->register($call->getFilename(), $call->getLine(), $firstArgument->stringValue(), $secondArgument->stringValue()); } } }
/** * 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); } }