I18n: add extract_messages_excluded_paths parameter

This commit is contained in:
Benjamin Renard 2023-11-22 12:46:07 +01:00
parent a3156d4d05
commit 5741dbb604
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
3 changed files with 111 additions and 40 deletions

View file

@ -39,11 +39,14 @@ App::init(
"$root_dir_path/static" "$root_dir_path/static"
), ),
), ),
'i18n' => array(
'default_locale' => "en_US.UTF8",
'extract_messages_excluded_paths' => array(
"static/lib"
),
),
'default' => array( 'default' => array(
'upload_max_filesize' => 10485760, 'upload_max_filesize' => 10485760,
'i18n' => array(
'default_locale' => "en_US.UTF8",
),
), ),
), ),
$root_dir_path $root_dir_path

View file

@ -254,4 +254,20 @@ class App {
echo "<h1>".I18n::_("You are now disconnected.")."</h1>"; echo "<h1>".I18n::_("You are now disconnected.")."</h1>";
} }
/**
* Helper method to resolve a given path if need and make it relative
* to application root directory
* @param string|null $path The path to handle (optional, default: current directory)
* @return string|false The relative path if exist and in app directory, absolute path if not in
* app directory or false if not exists.
*/
public static function relative_path($path=null) {
if (is_null($path)) $path = getcwd();
$path = realpath($path);
if (!$path) return false;
if (strpos($path, self::root_directory_path()) !== 0)
return $path;
return substr($path, strlen(self::root_directory_path())+1);
}
} }

View file

@ -44,6 +44,7 @@ class I18n {
array( array(
'root_directory' => '${root_directory_path}/locales', 'root_directory' => '${root_directory_path}/locales',
'default_locale' => null, 'default_locale' => null,
'extract_messages_excluded_paths' => ['.*/vendor/*'],
) )
); );
@ -452,17 +453,23 @@ class I18n {
// Store list of generated POT files // Store list of generated POT files
$pot_files = array(); $pot_files = array();
$root_directory_path = App::root_directory_path();
$excluded_paths = App::get('i18n.extract_messages_excluded_paths', null, 'array');
if (Cli::core_mode()) { if (Cli::core_mode()) {
// List EesyPHP PHP files to parse // List EesyPHP PHP files to parse
$cmd = array(
'find', '-name', "'*.php'", '-type', 'f', // Looking for PHP files
"-not", "-name", "'*.tpl.php'", // Exclude Smarty cache template files
);
if ($excluded_paths)
foreach($excluded_paths as $path)
array_push($cmd, "-not", "-path", "'$path'");
$eesyphp_php_files = run_external_command( $eesyphp_php_files = run_external_command(
array( $cmd,
'find', '-name', "'*.php'", // Looking for PHP files null, // no STDIN data
"-not", "-path", "'.*/vendor/*'", // Exclude vendor directories false, // do not escape command args (already done)
"-not", "-name", "'*.tpl.php'" // Exclude Smarty cache template files "$root_directory_path/src" // Run from EesyPHP src directory
),
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) if (!is_array($eesyphp_php_files) || $eesyphp_php_files[0] != 0)
Log :: fatal(self::_("Fail to list EesyPHP PHP files.")); Log :: fatal(self::_("Fail to list EesyPHP PHP files."));
@ -474,15 +481,15 @@ class I18n {
"xgettext", "xgettext",
"--from-code utf-8", "--from-code utf-8",
"--language=PHP", "--language=PHP",
"-o", $pot_file, // Output "-o", $pot_file, // Output
"--omit-header", // No POT header "--omit-header", // No POT header
"--keyword=___", // Handle custom ___() translation function "--keyword=___", // Handle custom ___() translation function
"--files=-", // Read files to parse from STDIN "--files=-", // Read files to parse from STDIN
"--force-po", // Write PO file even if empty "--force-po", // Write PO file even if empty
), ),
$eesyphp_php_files[1], // Pass PHP files list via STDIN $eesyphp_php_files[1], // Pass PHP files list via STDIN
true, // Escape parameters true, // Escape parameters
__DIR__ // Run from EesyPHP src directory "$root_directory_path/src" // Run from EesyPHP src directory
); );
if (!is_array($result) || $result[0] != 0) if (!is_array($result) || $result[0] != 0)
Log :: fatal(self::_("Fail to extract messages from EesyPHP PHP files using xgettext.")); Log :: fatal(self::_("Fail to extract messages from EesyPHP PHP files using xgettext."));
@ -491,15 +498,17 @@ class I18n {
} }
else { else {
// List application PHP files to parse // List application PHP files to parse
$cmd = array(
'find', '-name', "'*.php'", '-type', 'f', // Looking for PHP files
"-not", "-name", "'*.tpl.php'", // Exclude Smarty cache template files
);
foreach($excluded_paths as $path)
array_push($cmd, "-not", "-path", "'$path'");
$php_files = run_external_command( $php_files = run_external_command(
array( $cmd,
'find', '-name', "'*.php'", // Looking for PHP files null, // no STDIN data
"-not", "-path", "'.*/vendor/*'", // Exclude vendor directories false, // do not escape command args (already done)
"-not", "-name", "'*.tpl.php'" // Exclude Smarty cache template files $root_directory_path // Run from application root directory
),
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) if (!is_array($php_files) || $php_files[0] != 0)
Log :: fatal(self::_("Fail to list application PHP files.")); Log :: fatal(self::_("Fail to list application PHP files."));
@ -519,7 +528,7 @@ class I18n {
), ),
$php_files[1], // Pass PHP files list via STDIN $php_files[1], // Pass PHP files list via STDIN
true, // Escape parameters true, // Escape parameters
App :: root_directory_path() // Run from EesyPHP src directory $root_directory_path // Run from application root directory
); );
if (!is_array($result) || $result[0] != 0) if (!is_array($result) || $result[0] != 0)
Log :: fatal(self::_("Fail to extract messages from PHP files using xgettext.")); Log :: fatal(self::_("Fail to extract messages from PHP files using xgettext."));
@ -530,12 +539,24 @@ class I18n {
foreach(Tpl::static_directories() as $idx => $static_directory) { 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;
if (!Cli::core_mode() && $static_directory == Tpl::$core_static_directory) continue; if (!Cli::core_mode() && $static_directory == Tpl::$core_static_directory) continue;
// Make path relative to application root directory
$relative_static_directory = App::relative_path($static_directory);
if (!$relative_static_directory) {
Log::debug("Static directory '%s' does not exist, ignore it.", $static_directory);
continue;
}
// List JS files to parse // List JS files to parse
$cmd = array(
'find', escapeshellarg($relative_static_directory),
'-name', "'*.tpl'", '-type', 'f'
);
foreach($excluded_paths as $path)
array_push($cmd, "-not", "-path", "'$path'");
$result = run_external_command( $result = run_external_command(
array('find', escapeshellarg(basename($static_directory)), '-name', "'*.js'"), $cmd,
null, // no STDIN data null, // no STDIN data
false, // do not escape command args (already done) false, // do not escape command args (already done)
dirname($static_directory) // Run from parent directory $root_directory_path // Run from application root directory
); );
if (!is_array($result) || $result[0] != 0) if (!is_array($result) || $result[0] != 0)
Log :: fatal( Log :: fatal(
@ -557,7 +578,7 @@ class I18n {
), ),
$result[1], // Pass JS files list via STDIN $result[1], // Pass JS files list via STDIN
true, // Escape arguments true, // Escape arguments
dirname($static_directory) // Run from parent directory $root_directory_path // Run from application root directory
); );
if (!is_array($result) || $result[0] != 0) if (!is_array($result) || $result[0] != 0)
Log :: fatal( Log :: fatal(
@ -571,15 +592,46 @@ class I18n {
foreach (Tpl :: templates_directories() as $idx => $templates_directory) { 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;
if (!Cli::core_mode() && $templates_directory == Tpl::$core_templates_directory) continue; if (!Cli::core_mode() && $templates_directory == Tpl::$core_templates_directory) continue;
// Make path relative to application root directory
$relative_templates_directory = App::relative_path($templates_directory);
if (!$relative_templates_directory) {
Log::debug("Templates directory '%s' does not exist, ignore it.", $templates_directory);
continue;
}
// List templates files to parse
$cmd = array(
'find', escapeshellarg($relative_templates_directory),
'-name', "'*.tpl'", '-type', 'f'
);
foreach($excluded_paths as $path)
array_push($cmd, "-not", "-path", "'$path'");
$templates_files = run_external_command(
$cmd,
null, // no STDIN data
false, // do not escape command args (already done)
dirname($templates_directory) // Run from parent directory
);
if (!is_array($templates_files) || $templates_files[0] != 0)
Log :: fatal(
self::_("Fail to list templates files in directory %s."),
$templates_directory
);
// Compute template files file from find command output
$templates_files = explode("\n", $templates_files[1]);
// Extract messages from templates files using tsmarty2c.php // Extract messages from templates files using tsmarty2c.php
$cmd = array(
PHP_BINARY.' '.App :: root_directory_path().
"/vendor/smarty-gettext/smarty-gettext/tsmarty2c.php",
);
array_push($cmd, ...$templates_files);
$result = run_external_command( $result = run_external_command(
array( $cmd,
PHP_BINARY.' '.App :: root_directory_path()."/vendor/smarty-gettext/smarty-gettext/tsmarty2c.php", null, // Pass nothing on STDIN
basename($templates_directory), true, // Escape arguments
), $root_directory_path // Run from application root directory
null, // Pass nothing on STDIN
true, // Escape arguments
dirname($templates_directory) // Run from parent directory
); );
if (!is_array($result) || $result[0] != 0) if (!is_array($result) || $result[0] != 0)
Log :: fatal( Log :: fatal(