parse(); $headers = $catalog->getHeader(); $messages = array(); foreach ($catalog->getEntries() as $entry) { // msg id json format $msg = $entry->getMsgStr(); if ($entry->isPlural()) $msg = array($msg, $entry->getMsgIdPlural()); $messages[$entry->getMsgId()] = $msg; } return json_encode(array( 'messages' => $messages, 'locale' => $locale, 'domain' => $core?self :: CORE_TEXT_DOMAIN:self :: TEXT_DOMAIN, 'plural_expr' => '(n > 1)', )); } /** * Command to extract messages from PHP/JS & template files and * generate the lang/messages.pot file. * * @param array $command_args The command arguments * @return void */ public static function cli_extract_messages($command_args) { $root_path = Cli::core_mode()?self::$core_root_path:self::$root_path; // Store list of generated POT files $pot_files = array(); if (Cli::core_mode()) { // List EesyPHP PHP files to parse $eesyphp_php_files = run_external_command( array( 'find', '-name', "'*.php'", // Looking for PHP files "-not", "-path", "'.*/vendor/*'", // Exclude vendor directories "-not", "-name", "'*.tpl.php'" // Exclude Smarty cache template files ), null, // no STDIN data false, // do not escape command args (already done) __DIR__ // Run from EesyPHP src directory ); if (!is_array($eesyphp_php_files) || $eesyphp_php_files[0] != 0) { Log :: fatal(self::_("Fail to list EesyPHP PHP files.")); } // Extract messages from EesyPHP PHP files using xgettext $pot_file = "$root_path/php-messages.pot"; $result = run_external_command( array( "xgettext", "--from-code utf-8", "--language=PHP", "-o", $pot_file, // Output "--omit-header", // No POT header "--keyword=___", // Handle custom ___() translation function "--files=-" // Read files to parse from STDIN ), $eesyphp_php_files[1], // Pass PHP files list via STDIN true, // Escape parameters __DIR__ // Run from EesyPHP src directory ); if (!is_array($result) || $result[0] != 0) Log :: fatal(self::_("Fail to extract messages from EesyPHP PHP files using xgettext.")); if (is_file($pot_file)) $pot_files[] = $pot_file; } else { // List application PHP files to parse $php_files = run_external_command( array( 'find', '-name', "'*.php'", // Looking for PHP files "-not", "-path", "'.*/vendor/*'", // Exclude vendor directories "-not", "-name", "'*.tpl.php'" // Exclude Smarty cache template files ), null, // no STDIN data false, // do not escape command args (already done) App :: root_directory_path() // Run from Application root directory ); if (!is_array($php_files) || $php_files[0] != 0) { Log :: fatal(self::_("Fail to list application PHP files.")); } // Extract messages from PHP files using xgettext $pot_file = "$root_path/php-messages.pot"; $result = run_external_command( array( "xgettext", "--from-code utf-8", "--language=PHP", "-o", $pot_file, // Output "--omit-header", // No POT header "--keyword=___", // Handle custom ___() translation function "--files=-" // Read files to parse from STDIN ), $php_files[1], // Pass PHP files list via STDIN true, // Escape parameters App :: root_directory_path() // Run from EesyPHP src directory ); if (!is_array($result) || $result[0] != 0) Log :: fatal(self::_("Fail to extract messages from PHP files using xgettext.")); $pot_files[] = "$root_path/php-messages.pot"; } // Extract messages from JS files using xgettext in each registered static directories foreach(Tpl::static_directories() as $idx => $static_directory) { if (Cli::core_mode() && $static_directory != Tpl::$core_static_directory) continue; if (!Cli::core_mode() && $static_directory == Tpl::$core_static_directory) continue; // List JS files to parse $result = run_external_command( array('find', escapeshellarg(basename($static_directory)), '-name', "'*.js'"), null, // no STDIN data false, // do not escape command args (already done) dirname($static_directory) // Run from parent directory ); if (!is_array($result) || $result[0] != 0) { Log :: fatal(self::_("Fail to list JS files in the directory of static files '%s'."), $static_directory); return; } // Extract messages from JS files using xgettext $pot_file = "$root_path/js-$idx-messages.pot"; $result = run_external_command( array( "xgettext", "--from-code utf-8", "--language=JavaScript", "-o", $pot_file, // Output "--omit-header", // No POT header "--keyword=___", // Handle custom ___() translation function "--files=-" // Read files to parse from STDIN ), $result[1], // Pass JS files list via STDIN true, // Escape arguments dirname($static_directory) // Run from parent directory ); if (!is_array($result) || $result[0] != 0) Log :: fatal( self::_("Fail to extract messages from JS files in the directory of static files '%s' using xgettext."), $static_directory); if (is_file($pot_file)) $pot_files[] = $pot_file; } if (Tpl :: initialized()) { foreach (Tpl :: templates_directories() as $idx => $templates_directory) { if (Cli::core_mode() && $templates_directory != Tpl::$core_templates_directory) continue; if (!Cli::core_mode() && $templates_directory == Tpl::$core_templates_directory) continue; // Extract messages from templates files using tsmarty2c.php $result = run_external_command( array( PHP_BINARY.' '.App :: root_directory_path()."/vendor/smarty-gettext/smarty-gettext/tsmarty2c.php", basename($templates_directory), ), null, // Pass nothing on STDIN true, // Escape arguments dirname($templates_directory) // Run from parent directory ); if (!is_array($result) || $result[0] != 0) Log :: fatal( self::_("Fail to extract messages from templates directory '%s' using tsmarty2c.php script."), $templates_directory ); if (!$result[1]) continue; $pot_file = "$root_path/templates-$idx-messages.pot"; $fd = fopen($pot_file, 'w'); fwrite($fd, $result[1]); fclose($fd); $pot_files[] = $pot_file; } } $fd = fopen("$root_path/headers.pot", 'w'); $headers = array( 'msgid ""', 'msgstr ""', '"POT-Creation-Date: '.date('Y-m-d H:iO').'\n"', '"PO-Revision-Date: '.date('Y-m-d H:iO').'\n"', '"MIME-Version: 1.0\n"', '"Content-Type: text/plain; charset=utf-8\n"', '"Content-Transfer-Encoding: 8bit\n"', ); fwrite($fd, implode("\n", $headers)); fclose($fd); // Merge previous results in messages.pot file using msgcat $result = run_external_command(array_merge( array( 'msgcat', "-t", "utf-8", "--use-first", "-o", "$root_path/messages.pot", "$root_path/headers.pot", ), $pot_files )); if (!is_array($result) || $result[0] != 0) Log :: fatal(self::_("Fail to merge messages using msgcat.")); } /** * Command to update messages from lang/messages.pot file to * all PO file in lang/[lang]/LC_MESSAGES. * * @param array $command_args The command arguments * @return bool */ public static function cli_update_messages($command_args) { $compendium_args = array(); foreach ($command_args as $arg) { if (!file_exists($arg)) Log :: fatal(self::_("Compendium file %s not found."), $arg); else { $compendium_args[] = '-C'; $compendium_args[] = $arg; } } $domain = Cli::core_mode()?self::CORE_TEXT_DOMAIN:self::TEXT_DOMAIN; $root_path = Cli::core_mode()?self::$core_root_path:self::$root_path; $pot_file = "$root_path/messages.pot"; if (!is_file($pot_file)) Log :: fatal(self::_("POT file not found (%s). Please run extract_messages first."), $pot_file); if ($dh = opendir($root_path)) { $error = False; while (($file = readdir($dh)) !== false) { if ( !is_dir("$root_path/$file") || in_array($file, array('.', '..')) || is_link("$root_path/$file") ) continue; Log :: debug(self::_("Lang directory '%s' found"), $file); // Check LC_MESSAGES directory exists $lang = $file; $lang_dir = "$root_path/$file/LC_MESSAGES" ; if (!is_dir($lang_dir)) { Log :: debug(self::_("LC_MESSAGES directory not found in lang '%s' directory, ignore it."), $lang); continue; } $po_file = "$lang_dir/$domain.po"; $created = false; if (!is_file($po_file)) { // Init PO file from POT file using msginit $result = run_external_command( array("msginit", "-i", "$pot_file", "-l", "$lang", "-o", $po_file) ); if (is_array($result) && $result[0] == 0) { $created = true; } else { Log :: error(self::_("Fail to init messages in %s PO file using msginit (%s)."), $lang, $po_file); $error = True; } } // Update messages in PO file from POT file using msgmerge // Note: msginit does not accept compendium files, so we also run // msgmerge on creation with compendium file(s). if (is_file($po_file) && (!$created || $compendium_args)) { $result = run_external_command( array_merge( array("msgmerge", "-q", "-U"), $compendium_args, array($po_file, $pot_file) ) ); if (!is_array($result) || $result[0] != 0) { Log :: error(self::_("Fail to update messages in %s PO file using msgmerge (%s)."), $lang, $po_file); $error = True; } } elseif (!$created) { Log :: debug(self::_("PO file not found in lang '%s' directory, ignore it."), $lang); } } closedir($dh); return !$error; } Log :: fatal(self::_("Fail to open root lang directory (%s)."), App :: root_directory_path()); return false; } /** * Command to compile messages from existing translation PO lang files * to corresponding MO files and as JS catalog (for translation in JS). * * @param array $command_args The command arguments * @return bool */ public static function cli_compile_messages($command_args) { $domain = Cli::core_mode()?self::CORE_TEXT_DOMAIN:self::TEXT_DOMAIN; $root_path = Cli::core_mode()?self::$core_root_path:self::$root_path; if ($dh = opendir($root_path)) { $error = False; while (($file = readdir($dh)) !== false) { if ( !is_dir("$root_path/$file") || in_array($file, array('.', '..')) ) continue; if (is_link("$root_path/$file")) { $real_lang_dir = readlink("$root_path/$file"); if (dirname($real_lang_dir) != '.' || !is_dir("$root_path/$real_lang_dir")) continue; $lang = $file; Log :: debug(self::_("Lang alias symlink found: %s -> %s"), $lang, $real_lang_dir); // Create JS catalog symlink (if not exists) $js_link = "$root_path/$lang.js"; $link_target = "$real_lang_dir.js"; if (!file_exists($js_link)) { if (symlink($link_target, $js_link)) { Log :: info(self::_("JS catalog symlink for %s -> %s created (%s)"), $lang, $real_lang_dir, $js_link); } else { Log :: error(self::_("Fail to create JS catalog symlink for %s -> %s (%s)"), $lang, $real_lang_dir, $js_link); $error = True; } } elseif (readlink($js_link) == $link_target) { Log :: debug(self::_("JS catalog symlink for %s -> %s already exist (%s)"), $lang, $real_lang_dir, $js_link); } else { Log :: warning( self::_("JS catalog file for %s already exist, but it's not a symlink to %s (%s)"), $lang, $real_lang_dir, $js_link ); $error = True; } continue; } Log :: debug(self::_("Lang directory '%s' found"), $file); // Check LC_MESSAGES directory exists $lang = $file; $lang_dir = "$root_path/$file/LC_MESSAGES" ; if (!is_dir($lang_dir)) { Log :: debug(self::_("LC_MESSAGES directory not found in lang '%s' directory, ignore it."), $lang); continue; } // Test .PO file is present $po_file = "$lang_dir/$domain.po"; if (!is_file($po_file)) { Log :: debug(self::_("PO file not found in lang '%s' directory, ignore it."), $lang); continue; } $mo_file = preg_replace('/\.po$/', '.mo', $po_file); // Compile messages from PO file to MO file using msgfmt $result = run_external_command( array("msgfmt", "-o", $mo_file, $po_file) ); if (!is_array($result) || $result[0] != 0) { Log :: error( self::_("Fail to compile messages from %s PO file as MO file using msgfmt (%s)."), $lang, $po_file ); $error = True; } // Compile messages from PO file to JS catalog file $js_catalog = self :: po2json($lang, $po_file, Cli::core_mode()); $js_file = "$root_path/$lang.js"; if(!$fd = fopen($js_file, 'w')) { Log :: error(self::_("Fail to open %s JS catalog file in write mode (%s)."), $lang, $js_file); $error = True; } elseif (fwrite($fd, sprintf("translations_data = %s;", $js_catalog)) === false) { Log :: error(self::_("Fail to write %s JS catalog in file (%s)."), $lang, $js_file); $error = True; } else { Log :: info(self::_("%s JS catalog writed (%s)."), $lang, $js_file); } } closedir($dh); return !$error; } Log :: fatal(self::_("Fail to open root lang directory (%s)."), App :: root_directory_path()); return false; } } # vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab