MAIN_DIR, "admin" => MAIN_DIR.DIR."admin" ); # Array: $exclude # Paths to be excluded from directory recursion. $exclude = array( MAIN_DIR.DIR."admin", MAIN_DIR.DIR."modules", MAIN_DIR.DIR."feathers", MAIN_DIR.DIR."themes", MAIN_DIR.DIR."tools", MAIN_DIR.DIR."uploads", MAIN_DIR.DIR."includes".DIR."caches", MAIN_DIR.DIR."includes".DIR."lib".DIR."Twig", MAIN_DIR.DIR."includes".DIR."lib".DIR."IXR", MAIN_DIR.DIR."includes".DIR."lib".DIR."cebe" ); # Array: $strings # Contains the translations for each gettext domain. $strings = array(); # String: $str_reg # Regular expression representing a string. $str_reg = '(\"[^\"]+\"|\'[^\']+\')'; /** * Function: find_domains * Find domains for installed extensions. */ function find_domains() { global $domains; global $strings; $modules_dir = new DirectoryIterator(MODULES_DIR); foreach ($modules_dir as $item) { if ($item->isDir() and !$item->isDot()) $domains[$item->getFilename()] = $item->getPathname(); } $feathers_dir = new DirectoryIterator(FEATHERS_DIR); foreach ($feathers_dir as $item) { if ($item->isDir() and !$item->isDot()) $domains[$item->getFilename()] = $item->getPathname(); } $themes_dir = new DirectoryIterator(THEMES_DIR); foreach ($themes_dir as $item) { if ($item->isDir() and !$item->isDot()) $domains[$item->getFilename()] = $item->getPathname(); } foreach ($domains as $filename => $pathname) $strings[$filename] = array(); } /** * Function: scan_dir * Scans a directory in search of files or subdirectories. */ function scan_dir($domain, $pathname) { global $exclude; $dir = new DirectoryIterator($pathname); foreach ($dir as $item) { if (!$item->isDot()) { $item_path = $item->getPathname(); $extension = $item->getExtension(); switch ($item->getType()) { case "file": scan_file($domain, $item_path, $extension); break; case "dir": if (!in_array($item_path, $exclude)) scan_dir($domain, $item_path); break; } } } } /** * Function: scan_file * Scans a file in search of translation strings. */ function scan_file($domain, $pathname, $extension) { if ($extension != "php" and $extension != "twig") return; $file = fopen($pathname, "r"); $line = 1; if ($file === false) return; while (!feof($file)) { $text = fgets($file); switch ($extension) { case "php": scan__($domain, $pathname, $line, $text); scan_f($domain, $pathname, $line, $text); scan_p($domain, $pathname, $line, $text); break; case "twig": scan_translate($domain, $pathname, $line, $text); scan_translate_format($domain, $pathname, $line, $text); scan_translate_plural($domain, $pathname, $line, $text); break; } $line++; } fclose($file); } /** * Function: make_place * Makes a string detailing the place a translation was found. */ function make_place($pathname, $line) { return str_replace( array(MAIN_DIR.DIR, DIR), array("", "/"), $pathname ).":".$line; } /** * Function: is_theme * Checks if a pathname is part of a theme. */ function is_theme($pathname) { return ( strpos($pathname, THEMES_DIR) === 0 or strpos($pathname, MAIN_DIR.DIR."admin") === 0 ); } /** * Function: scan__ * Scans text for occurrences of the __() function. */ function scan__($domain, $pathname, $line, $text) { global $domains; global $strings; global $str_reg; $escaped = preg_quote($domain, "/"); $dom_reg = ($domain == "chyrp") ? '' : ',\s*(\"'.$escaped.'\"|\''.$escaped.'\')' ; if ( preg_match_all( "/__\($str_reg$dom_reg\)/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $string = trim($match[1], "'\""); if (isset($strings[$domain][$string])) $strings[$domain][$string]["places"][] = make_place( $pathname, $line ); else $strings[$domain][$string] = array( "places" => array(make_place($pathname, $line)), "filter" => false, "plural" => false ); } } } /** * Function: scan_f * Scans text for occurrences of the _f() function. */ function scan_f($domain, $pathname, $line, $text) { global $domains; global $strings; global $str_reg; $escaped = preg_quote($domain, "/"); $dom_reg = ($domain == "chyrp") ? '.+?' : '.+?,\s*(\"'.$escaped.'\"|\''.$escaped.'\')' ; if ( preg_match_all( "/_f\($str_reg$dom_reg\)/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $string = trim($match[1], "'\""); if (isset($strings[$domain][$string])) $strings[$domain][$string]["places"][] = make_place( $pathname, $line ); else $strings[$domain][$string] = array( "places" => array(make_place($pathname, $line)), "filter" => true, "plural" => false ); } } } /** * Function: scan_p * Scans text for occurrences of the _p() function. */ function scan_p($domain, $pathname, $line, $text) { global $domains; global $strings; global $str_reg; $escaped = preg_quote($domain, "/"); $dom_reg = ($domain == "chyrp") ? '.+?' : '.+?,\s*(\"'.$escaped.'\"|\''.$escaped.'\')' ; if ( preg_match_all( "/_p\($str_reg,\s*$str_reg$dom_reg\)/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $string = trim($match[1], "'\""); $plural = trim($match[2], "'\""); if (isset($strings[$domain][$string])) $strings[$domain][$string]["places"][] = make_place( $pathname, $line ); else $strings[$domain][$string] = array( "places" => array(make_place($pathname, $line)), "filter" => true, "plural" => $plural ); } } } /** * Function: scan_translate * Scans text for occurrences of the translate() filter. */ function scan_translate($domain, $pathname, $line, $text) { global $domains; global $strings; global $str_reg; $escaped = preg_quote($domain, "/"); $dom_reg = '(\(\s*(\"'.$escaped.'\"|\''.$escaped.'\')\s*\))'; if (is_theme($pathname)) $dom_reg.= '?'; if ( preg_match_all( "/$str_reg\s*\|\s*translate(?!_plural)$dom_reg(?!\s*\|\s*format)/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $string = trim($match[1], "'\""); if (isset($strings[$domain][$string])) $strings[$domain][$string]["places"][] = make_place( $pathname, $line ); else $strings[$domain][$string] = array( "places" => array(make_place($pathname, $line)), "filter" => false, "plural" => false ); } } } /** * Function: scan_translate_format * Scans text for occurrences of the translate() | format() filter combination. */ function scan_translate_format($domain, $pathname, $line, $text) { global $domains; global $strings; global $str_reg; $escaped = preg_quote($domain, "/"); $dom_reg = '(\(\s*(\"'.$escaped.'\"|\''.$escaped.'\')\s*\))'; if (is_theme($pathname)) $dom_reg.= '?'; if ( preg_match_all( "/$str_reg\s*\|\s*translate$dom_reg\s*\|\s*format/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $string = trim($match[1], "'\""); if (isset($strings[$domain][$string])) $strings[$domain][$string]["places"][] = make_place( $pathname, $line ); else $strings[$domain][$string] = array( "places" => array(make_place($pathname, $line)), "filter" => true, "plural" => false ); } } } /** * Function: scan_translate_plural * Scans text for occurrences of the translate_plural() filter. */ function scan_translate_plural($domain, $pathname, $line, $text) { global $domains; global $strings; global $str_reg; $escaped = preg_quote($domain, "/"); $dom_reg = '(,\s*(\"'.$escaped.'\"|\''.$escaped.'\'))'; if (is_theme($pathname)) $dom_reg.= '?'; if ( preg_match_all( "/$str_reg\s*\|\s*translate_plural\(\s*$str_reg\s*,.+?$dom_reg\s*\)\s*\|\s*format/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $string = trim($match[1], "'\""); $plural = trim($match[2], "'\""); if (isset($strings[$domain][$string])) $strings[$domain][$string]["places"][] = make_place( $pathname, $line ); else $strings[$domain][$string] = array( "places" => array(make_place($pathname, $line)), "filter" => true, "plural" => $plural ); } } } /** * Function: create_files * Generates the .pot files and writes them to disk. */ function create_files() { global $domains; global $strings; foreach ($domains as $filename => $pathname) { $contents = "#. This file is distributed under the". " same license as the Chyrp Lite package.\n\n"; foreach ($strings[$filename] as $string => $attributes) { foreach ($attributes["places"] as $place) $contents.= "#: ".$place."\n"; if ($attributes["filter"] === true) $contents.= "#, php-format\n"; $contents.= "msgid \"".$string."\"\n"; if ($attributes["plural"] !== false) { $contents.= "msgid_plural \"".$attributes["plural"]."\"\n"; $contents.= "msgstr[0] \"\"\n"; $contents.= "msgstr[1] \"\"\n"; } else { $contents.= "msgstr \"\"\n"; } $contents.= "\n"; } $pot_file = $pathname.DIR. (($filename == "chyrp") ? "includes".DIR : ""). "locale".DIR."en_US".DIR."LC_MESSAGES".DIR. $filename.".pot"; $result = @file_put_contents($pot_file, $contents); echo $filename.".pot: ".( ($result === false) ? 'Boo!' : 'Yay!' )."\n"; } } #--------------------------------------------- # Output Starts #--------------------------------------------- ?> <?php echo "Gettext"; ?>
 $pathname)
        scan_dir($filename, $pathname);

    create_files();

    #---------------------------------------------
    # Processing Ends
    #---------------------------------------------

            ?>