Barryvdh\TranslationManager\Manager::findTranslations PHP Method

findTranslations() public method

public findTranslations ( $path = null )
    public function findTranslations($path = null)
    {
        $path = $path ?: base_path();
        $keys = array();
        $functions = array('trans', 'trans_choice', 'Lang::get', 'Lang::choice', 'Lang::trans', 'Lang::transChoice', '@lang', '@choice');
        $pattern = "[^\\w|>]" . "(" . implode('|', $functions) . ")" . "\\(" . "[\\'\"]" . "(" . "[a-zA-Z0-9_-]+" . "([.][^)]+)+" . ")" . "[\\'\"]" . "[\\),]";
        // Close parentheses or new parameter
        // Find all PHP + Twig files in the app folder, except for storage
        $finder = new Finder();
        $finder->in($path)->exclude('storage')->name('*.php')->name('*.twig')->files();
        /** @var \Symfony\Component\Finder\SplFileInfo $file */
        foreach ($finder as $file) {
            // Search the current file for the pattern
            if (preg_match_all("/{$pattern}/siU", $file->getContents(), $matches)) {
                // Get all matches
                foreach ($matches[2] as $key) {
                    $keys[] = $key;
                }
            }
        }
        // Remove duplicates
        $keys = array_unique($keys);
        // Add the translations to the database, if not existing.
        foreach ($keys as $key) {
            // Split the group and item
            list($group, $item) = explode('.', $key, 2);
            $this->missingKey('', $group, $item);
        }
        // Return the number of found translations
        return count($keys);
    }

Usage Example

 /**
  * Execute the console command.
  *
  * @return void
  */
 public function fire()
 {
     $counter = $this->manager->findTranslations();
     $this->info('Done importing, processed ' . $counter . ' items!');
 }
All Usage Examples Of Barryvdh\TranslationManager\Manager::findTranslations