From 13e7fc83e8374dc3913aedb1ee8aa148597b1ddf Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 25 Sep 2021 17:38:46 +0200 Subject: [PATCH] cosmetics --- inc/class.dc.translater.module.php | 12 ++--- inc/class.dc.translater.php | 86 ++++++++++++++++-------------- 2 files changed, 53 insertions(+), 45 deletions(-) diff --git a/inc/class.dc.translater.module.php b/inc/class.dc.translater.module.php index 06dcb94..8c3ceaf 100644 --- a/inc/class.dc.translater.module.php +++ b/inc/class.dc.translater.module.php @@ -283,7 +283,7 @@ class dcTranslaterModule $zip = new fileUnzip($zip_file['tmp_name']); $files = $zip->getFilesList(); - foreach($files AS $file) { + foreach($files as $file) { $f = $this->parseZipFilename($file, true); if (!$this->translater->import_overwrite @@ -300,7 +300,7 @@ class dcTranslaterModule ]; } - foreach ($res AS $rs) { + foreach ($res as $rs) { if (!is_dir($rs['root'])) { files::makeDir($rs['root'], true); } @@ -395,9 +395,9 @@ class dcTranslaterModule * * @param string $file The zip filename * @param boolean $throw Silently failed - * @return mixed Array of file info or false + * @return mixed Array of file info */ - public function parseZipFilename(string $file = '', bool $throw = false) + public function parseZipFilename(string $file = '', bool $throw = false): array { $is_file = preg_match('/^(.*?)\/locales\/(.*?)\/(.*?)(.po|.lang.php)$/', $file, $f); @@ -414,7 +414,7 @@ class dcTranslaterModule __('Zip file %s is not in translater format'), $file )); } - return false; + return []; } return [ 'module' => $module, @@ -433,7 +433,7 @@ class dcTranslaterModule * @param boolean $return_path Return path or name * @return array The lang list */ - public function getLangs(bool $return_path = false) + public function getLangs(bool $return_path = false): array { $res = []; diff --git a/inc/class.dc.translater.php b/inc/class.dc.translater.php index 86505ee..3074fb0 100644 --- a/inc/class.dc.translater.php +++ b/inc/class.dc.translater.php @@ -324,7 +324,7 @@ class dcTranslater * @param array $res Internal recursion * @return array List of path */ - public static function scandir(string $path, string $dir = '', array $res = []) + public static function scandir(string $path, string $dir = '', array $res = []): array { $path = path::real($path, false); if (!is_dir($path) || !is_readable($path)) { @@ -347,18 +347,59 @@ class dcTranslater return $res; } - /* Try if a file is a .lang.php file */ - public static function isLangphpFile($file) + /** + * Encode a string + * + * @param string $str The string to encode + * @return string The encoded string + */ + public static function encodeMsg(string $str): string { - return files::getExtension($file) == 'php' && stristr($file, '.lang.php'); + return text::toUTF8(stripslashes(trim($str))); } - /* Try if a file is a .po file */ - public static function isPoFile($file) + /** + * Clean a po string + * + * @param string $string The string to clean + * @param boolean $reverse Un/escape string + * @return string The cleaned string + */ + public static function poString(string $string, bool $reverse = false): string + { + if ($reverse) { + $smap = array('"', "\n", "\t", "\r"); + $rmap = array('\\"', '\\n"' . "\n" . '"', '\\t', '\\r'); + return trim((string) str_replace($smap, $rmap, $string)); + } else { + $smap = array('/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\"/'); + $rmap = array('', "\n", "\r", "\t", '"'); + return trim((string) preg_replace($smap, $rmap, $string)); + } + } + + /** + * Try if a file is a .po file + * + * @param string $file The path to test + * @return boolean Success + */ + public static function isPoFile(string $file): bool { return files::getExtension($file) == 'po'; } + /** + * Try if a file is a .lang.php file + * + * @param string $file The path to test + * @return boolean Success + */ + public static function isLangphpFile(string $file): bool + { + return files::getExtension($file) == 'php' && stristr($file, '.lang.php'); + } + /** * Check limit number of backup for a module * @@ -445,8 +486,6 @@ class dcTranslater /** * Extract messages from a tpl contents * - * support plurals - * * @param string $content The contents * @param string $func The function name * @return array The messages @@ -485,36 +524,5 @@ class dcTranslater } return $final_strings; } - - /** - * Encode a string - * - * @param string $str The string to encode - * @return string The encoded string - */ - public static function encodeMsg(string $str): string - { - return text::toUTF8(stripslashes(trim($str))); - } - - /** - * Clean a po string - * - * @param string $string The string to clean - * @param boolean $reverse Un/escape string - * @return string The cleaned string - */ - public static function poString(string $string, bool $reverse = false): string - { - if ($reverse) { - $smap = array('"', "\n", "\t", "\r"); - $rmap = array('\\"', '\\n"' . "\n" . '"', '\\t', '\\r'); - return trim((string) str_replace($smap, $rmap, $string)); - } else { - $smap = array('/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\"/'); - $rmap = array('', "\n", "\r", "\t", '"'); - return trim((string) preg_replace($smap, $rmap, $string)); - } - } //@} } \ No newline at end of file