extract( $this->rootDir, $this->localeDir, $this->viewsDir . '/*/*.twig', ); /** @var PluginWrapper */ foreach ($this->repo as $plugin) { $this->extract( $this->pluginsDir . '/' . $plugin->context->name->value, $this->pluginsDir . '/' . $plugin->context->name->value . '/locale', $this->pluginsDir . '/' . $plugin->context->name->value . '/*/*.twig', $plugin->context->type->value == 'theme' ? 'theme' : $plugin->context->name->value ); } return Command::SUCCESS; } protected function extract( string $baseDir, string $localeDir, string $scanGlobPattern, string $domain = 'messages' ): int { //Create a new scanner, adding a translation for each domain: $scanner = new Scanner( $this->twig, $baseDir, Translations::create($domain) ); //Set a default domain, so any translations with no domain specified, // will be added to that domain $scanner->setDefaultDomain($domain); $scanner->addReferences(true); //Scan files foreach ($this->glob($scanGlobPattern) as $file) { $scanner->scanFile($file); } // Get possible languages $languages = array_map( fn ($dir) => basename($dir), glob($localeDir . '/*', GLOB_ONLYDIR) ); //Save the translations in .po files $generator = new PoGenerator(); $loader = new PoLoader(); foreach ($scanner->getTranslations() as $domain => $translations) { foreach ($languages as $language) { if (!file_exists("{$localeDir}/{$language}/LC_MESSAGES")) { mkdir("{$localeDir}/{$language}/LC_MESSAGES", 0777, true); } if (file_exists("{$localeDir}/{$language}/LC_MESSAGES/{$domain}.po")) { $finalTranslations = $translations->mergeWith( $loader->loadFile("{$localeDir}/{$language}/LC_MESSAGES/{$domain}.po"), Merge::SCAN_AND_LOAD ); } else { $finalTranslations = clone $translations; } $generator->generateFile( $finalTranslations, "{$localeDir}/{$language}/LC_MESSAGES/{$domain}.po" ); } } return Command::SUCCESS; } private function glob($pattern, $flags = 0) { $files = glob($pattern, $flags); foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { $files = array_merge($files, $this->glob($dir . '/' . basename($pattern), $flags)); } return $files; } }