Extracts messages to be translated from source code.

This command will search through source code files and extract
messages that need to be translated in different languages.

@throws Exception on failure.
@return array
9220
public function extract()
    {
        if (!isset($this->config['sourcePath'], $this->config['languages'])) {
            throw new Exception('The configuration must specify "sourcePath" and "languages".');
        }

        foreach ($this->config['sourcePath'] as $sourcePath) {
            if (!is_dir($sourcePath)) {
                throw new Exception("The source path {$sourcePath} is not a valid directory.");
            }
        }

        if (empty($this->config['format']) || !in_array($this->config['format'], ['php', 'po', 'db'])) {
            throw new Exception('Format should be either "php", "po" or "db".');
        }
        if (in_array($this->config['format'], ['php', 'po'])) {
            if (!isset($this->config['messagePath'])) {
                throw new Exception('The configuration file must specify "messagePath".');
            } elseif (!is_dir($this->config['messagePath'])) {
                throw new Exception("The message path {$this->config['messagePath']} is not a valid directory.");
            }
        }
        if (empty($this->config['languages'])) {
            throw new Exception("Languages cannot be empty.");
        }

        $files = [];
        foreach ( $this->config['sourcePath'] as $sourcePath ) {
            $files = array_merge(
                array_values($files),
                array_values(FileHelper::findFiles(realpath($sourcePath), $this->config))
            );
        }

        $messages = [];
        foreach ($files as $file) {
            $messages = array_merge_recursive($messages, $this->extractMessages($file, $this->config['translator']));
        }
        if (in_array($this->config['format'], ['php', 'po'])) {
            foreach ($this->config['languages'] as $language) {
                $dir = $this->config['messagePath'] . DIRECTORY_SEPARATOR . $language;
                if (!is_dir($dir)) {
                    @mkdir($dir);
                }
                if ($this->config['format'] === 'po') {
                    $catalog = isset($this->config['catalog']) ? $this->config['catalog'] : 'messages';
                    return $this->saveMessagesToPO($messages, $dir, $this->config['overwrite'], $this->config['removeUnused'], $this->config['sort'], $catalog);
                } else {
                    return $this->saveMessagesToPHP($messages, $dir, $this->config['overwrite'], $this->config['removeUnused'], $this->config['sort']);
                }
            }
        } elseif ($this->config['format'] === 'db') {
            $db = \Yii::$app->get(isset($this->config['db']) ? $this->config['db'] : 'db');
            if (!$db instanceof \yii\db\Connection) {
                throw new Exception('The "db" option must refer to a valid database application component.');
            }
            $sourceMessageTable = isset($this->config['sourceMessageTable']) ? $this->config['sourceMessageTable'] : '{{%source_message}}';
            $messageTable = isset($this->config['messageTable']) ? $this->config['messageTable'] : '{{%message}}';
            return $this->saveMessagesToDb(
                $messages,
                $db,
                $sourceMessageTable,
                $messageTable,
                $this->config['removeUnused'],
                $this->config['languages']
            );
        }
    }
946371
public function extract() {
        foreach ($this->log as $line) {
            foreach ($this->messageGroupsJoined as $header => $keywords) {
                $pattern = $this->getPattern($keywords);

                if (preg_match($pattern, $line)) {
                    $this->appendToGroup($header, $line, $pattern);
                }
            }
        }

        // Remove empty groups
        foreach (array_keys($this->messageGroups) as $groupKey) {
            if (true === empty($this->groups[$groupKey])) {
                unset($this->messageGroups[$groupKey]);
            }
        }

        // The array_merge sorts $messageGroups basing on $groups
        return array_merge($this->messageGroups, $this->groups);
    }
129168
public function getMessages() : array
    {
        $messages = [];

        foreach ($this->getLogs() as $log) {
            foreach ($log->getMessages() as $message) {
                $messages[] = $message;
            }
        }

        return $messages;
    }
99950
protected function getLines()
    {
        $ret = file($this->fileName);
        if ($ret === false) {
            throw new Exception('Unable to read file');
        }
        $lines = array();
        foreach ($ret as $line) {
            $lines[] = rtrim($line);
        }

        return $lines;
    }
396735
public function getAllLanguages() {
		
		if (is_array($this->alllanguages)) {
			return $this->alllanguages;
		}
		
		// find languages file in a locales path
		$langfile = $this->localespath.'languages.txt';

		if (!file_exists($langfile)) {
			// if no in locales then use default from this package
			$langfile = dirname(__FILE__).'/languages.txt';
		}

		if (!file_exists($langfile)) {
			// if not found then show error or return empty array
			if ($this->errorondatamissed) {
				throw new \Exception('No languages file found');
			}
			return array();
		}
		
		// load a file
		$list = @file_get_contents($langfile);
		
		if (!$list) {
			// if somethign went wrong throw error or return empty array
			if ($this->errorondatamissed) {
				throw new \Exception('Can not load languages list');
			}
			return array();
		}
		
		$languages = array();
		
		foreach (preg_split('!\\r?\\n!',$list) as $line) {
			
			list ($code,$name) = explode('=',$line);
			
			$code = trim($code);
			$name = trim($name);
			
			if ($code == '' || $name == '') {
				continue;
			}
			
			$origname = $name;
			$tags = array();
			
			$splitsettings = explode(':',$name,3);
			
			if (count($splitsettings) > 1) {
				$name = $splitsettings[0];
				$origname = $splitsettings[1];
				
				if (isset($splitsettings[2]) && $splitsettings[2] != '') {
					$tags = explode(',',$splitsettings[2]);
				}
			}
			
			$flagfile = $this->localespath.'flags/small/'.$code.'.png';
			
			$hasflag = file_exists($flagfile);
			
			$languages[$code] = array(
				'code'=>$code,
				'name'=>$name,
				'origname'=>$origname,
				'hasflag'=>$hasflag,
				'tags' => $tags
				);
		}
		return $languages;
	}
394122
public function extract()
    {
        if ($this->extractedWorkDir !== null) {
            return;
        }
        if (!is_file($this->packageArchive)) {
            throw new UserMessageException(t('Archive not found: %s', $this->packageArchive));
        }
        try {
            $workDir = $this->getVolatileDirectory()->getPath();
            try {
                $this->app->make('helper/zip')->unzip($this->packageArchive, $workDir);
            } catch (\Exception $x) {
                throw new UserMessageException($x->getMessage());
            }
            $fs = $this->getVolatileDirectory()->getFilesystem();
            $dirs = array_filter($fs->directories($workDir), function ($dn) {
                return strpos(basename($dn), '.') !== 0;
            });
            if (count($dirs) === 1) {
                $someFile = false;
                foreach ($fs->files($workDir) as $fn) {
                    if (strpos(basename($fn), '.') !== 0) {
                        $someFile = true;
                        break;
                    }
                }
                if ($someFile === false) {
                    $workDir = $dirs[0];
                }
            }
        } catch (\Exception $x) {
            $this->volatileDirectory = null;
            throw $x;
        }
        $this->extractedWorkDir = $workDir;
    }
355101
public function getExceptionMessages()
    {
        $messages = array();
        foreach( $this->__get('exceptionsFlat') as $exception ) {
            $messages[] = $exception->getMessage();
        }
        return $messages;
    }
625970
public function getAllTranslationByLanguage()
    {
        $messages = array();
        foreach ($this->getLanguageList() as $language) {
            $messages[$language] = $this->getTranslationsForLanguage($language);
        }

        return $messages;
    }
69700
public function loadAllMessages() {
		$files = glob(ROOT_PATH.$this->i18nMessagePath.'message*.php');

		$defaultFound = false;
		foreach ($files as $file) {
			$base = basename($file);
			if ($base == "message.php") {
				$messageFile = new FineMessageLanguage();
				$messageFile->loadAllFile(ROOT_PATH.$this->i18nMessagePath);
				$this->messages['default'] = $messageFile;
				$defaultFound = true;
			} else {
				if(strpos($base, '_custom') === false) {
					$phpPos = strpos($base, '.php');
					$language = substr($base, 8, $phpPos-8);
					$messageLanguage = new FineMessageLanguage();
					$messageLanguage->loadAllFile(ROOT_PATH.$this->i18nMessagePath, $language);
					$this->messages[$language] = $messageLanguage;
				}
			}
		}
		if (!$defaultFound) {
			$messageFile = new FineMessageLanguage();
			$messageFile->loadAllFile(ROOT_PATH.$this->i18nMessagePath);
			$this->messages['default'] = $messageFile;
		}
	}
451991
public function getJsTranslations()
    {
        $translations = [];
        foreach ($this->_jsTranslations as $module => $data) {
            foreach ($data as $key) {
                $translations[$key] = Yii::t($module, $key, [], Yii::$app->language);
            }
        }
        return $translations;
    }
394980
public List<String> messageSources() {
        if (null == _messageSources) {
            _messageSources = Arrays.asList(get(I18N_MESSAGE_SOURCES).toString().split("[, \\t]+"));
        }
        return _messageSources;
    }
844171
protected function _extract() {
		$message = [];

		$message[] = 'A `Catalog` class configuration with an adapter that is capable of';
		$message[] = 'handling read requests for the `messageTemplate` category is needed';
		$message[] = 'in order to proceed. This may also be referred to as `extractor`.';
		$this->out($message);
		$this->out();

		$name = $this->_configuration([
			'adapter' => 'Code',
			'path' => $this->source,
			'scope' => $this->scope
		]);
		$configs = Catalog::config();

		try {
			return Catalog::read($name, 'messageTemplate', 'root', [
				'scope' => $configs[$name]['scope'],
				'lossy' => false
			]);
		} catch (Exception $e) {
			return false;
		}
	}
781803
public function extractHeaders(){
        $absoluteUrl = $this->getAbsoluteUrl();
        $headersArrRaw = get_headers($absoluteUrl, 1); 
        
        if($headersArrRaw === false){
            throw new \Exception("cannot get headers for {$absoluteUrl}");
        }
        
        $headersArr = array_change_key_case($headersArrRaw, CASE_LOWER);        
        if(isset($headersArr[0]) === true && strpos($headersArr[0], 'HTTP/') !== false){
            $statusStmt = $headersArr[0];
            $statusParts = explode(' ', $statusStmt);
            $headersArr['status-code'] = $statusParts[1];
            
            $statusIndex = strrpos($statusStmt, $statusParts[1])+ strlen($statusParts[1])+1;
            $headersArr['status'] = trim(substr($statusStmt, $statusIndex));
        }
        if(is_array($headersArr['content-type']) === true){
            $headersArr['content-type'] = end($headersArr['content-type']);
        }
        
        return $headersArr;
    }
519915
private function gatherTranslatableStrings()
    {
        $this->translatables = [];

        $this->scanTwigFiles();
        $this->scanPhpFiles();

        ksort($this->translatables);
    }
449373
protected function extractExceptionData(Exception $exception)
    {
        $data = [
            'exception' => get_class($exception),
            'message'   => $exception->getMessage(),
            'code'      => $exception->getCode(),
            'file'      => $exception->getFile(),
            'line'      => $exception->getLine(),
            'trace'     => []
        ];

        foreach ($exception->getTrace() as $item) {
            if (isset($item['args']) && is_array($item['args'])) {
                $item['args'] = $this->cleanTraceArgs($item['args']);
            }
            $data['trace'][] = $item;
        }

        return $data;
    }
500251
public static function getTranslationMessages()
    {
        return [
            new Message('menu.page', 'victoire'),
            new Message('menu.preview', 'victoire'),
            new Message('menu.page.settings', 'victoire'),
            new Message('menu.page.template', 'victoire'),
            new Message('menu.page.new', 'victoire'),
            new Message('menu.template', 'victoire'),
            new Message('menu.template.settings', 'victoire'),
            new Message('menu.template.new', 'victoire'),
            new Message('menu.sitemap', 'victoire'),
            new Message('menu.media', 'victoire'),
            new Message('menu.parent', 'victoire'),
            new Message('menu.business_template', 'victoire'),
            new Message('modal.button.create.title', 'victoire'),
            new Message('modal.button.update.title', 'victoire'),
            new Message('menu.leftnavbar.stats.label', 'victoire'),
            new Message('menu.leftnavbar.target.label', 'victoire'),
            new Message('menu.leftnavbar.lang.label', 'victoire'),
            new Message('menu.leftnavbar.network.label', 'victoire'),
            new Message('menu.leftnavbar.todo.label', 'victoire'),
            new Message('menu.leftnavbar.img.label', 'victoire'),
            new Message('menu.leftnavbar.user.label', 'victoire'),
            new Message('menu.leftnavbar.404.label', 'victoire'),
            new Message('menu.leftnavbar.sitemap.label', 'victoire'),
            new Message('menu.leftnavbar.sandbox.label', 'victoire'),
        ];
    }
942467
public function getAllLangs(): array
    {
        $domainInfo = $this->intuition->getDomainInfo($this->domain);
        $messageFiles = glob($domainInfo['dir'].'/*.json');
        $languages = array_values(array_unique(array_map(
            function ($filename) {
                return basename($filename, '.json');
            },
            $messageFiles
        )));
        $availableLanguages = [];
        foreach ($languages as $lang) {
            $availableLanguages[$lang] = $this->intuition->getLangName($lang);
        }
        ksort($availableLanguages);
        return $availableLanguages;
    }