/** * Perform xgettext style extraction from PHP source files * @todo JavaScript files too * @todo filter on TextDomain? * @return array Loco's internal array format */ public static function xgettext(LocoPackage $package, $relative_to = '') { class_exists('LocoPHPExtractor') or loco_require('build/gettext-compiled'); $extractor = new LocoPHPExtractor(); $export = array(); // extract from PHP sources, as long as source locations exist if ($srcdirs = $package->get_source_dirs()) { foreach ($srcdirs as $dir) { $fileref = loco_relative_path($relative_to, $dir); foreach (self::find_php($dir) as $path) { $source = file_get_contents($path) and $tokens = token_get_all($source) and $export = $extractor->extract($tokens, str_replace($dir, $fileref, $path)); } } } else { if ($po = $package->get_po()) { foreach ($po as $code => $path) { $export = self::parse_po($path); // strip translations, as this is intended as a POT foreach ($export as $i => $message) { $export[$i]['target'] = ''; } break; } } } return $export; }
/** * Perform xgettext style extraction from PHP source files * @todo JavaScript files too * @todo filter on TextDomain? * @return array Loco's internal array format */ public static function xgettext(LocoPackage $package, $relative_to = '') { class_exists('LocoPHPExtractor') or loco_require('build/gettext-compiled'); $extractor = new LocoPHPExtractor(); // parse out header tags in template files if ($package instanceof LocoThemePackage) { $extractor->set_wp_theme(); } else { if ($package instanceof LocoPluginPackage) { $extractor->set_wp_plugin(); } } $export = array(); // extract from PHP sources, as long as source locations exist if ($srcdirs = $package->get_source_dirs()) { foreach ($srcdirs as $dir) { $fileref = loco_relative_path($relative_to, $dir); foreach (self::find_php($dir) as $path) { $source = file_get_contents($path) and $tokens = token_get_all($source) and $export = $extractor->extract($tokens, str_replace($dir, $fileref, $path)); } } } else { if ($path = $package->get_default_file()) { $dir = dirname($path); $fileref = loco_relative_path($relative_to, $dir); $source = file_get_contents($path) and $tokens = token_get_all($source) and $export = $extractor->extract($tokens, str_replace($dir, $fileref, $path)); } else { if ($po = $package->get_po()) { foreach ($po as $code => $path) { $export = self::parse_po($path); // strip translations, as this is intended as a POT foreach ($export as $i => $message) { $export[$i]['target'] = ''; } break; } } } } // add translatable header tags that won't have been in PHP if ($package instanceof LocoThemePackage) { $id = $target = ''; foreach ($package->get_headers() as $tag => $source) { if ($source) { $notes = str_replace('URI', ' URI', $tag) . ' of the theme'; $export[] = compact('id', 'source', 'target', 'notes'); } } } return $export; }
function loco_extract_php(array $tokens, $fileref = '') { $extractor = new LocoPHPExtractor(); return $extractor->extract($tokens, $fileref); }