From c89b162336de480ee80b3e649d07d8db3ac71eab Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 25 Sep 2021 15:15:16 +0200 Subject: [PATCH] re-add tpl extraction --- CHANGELOG.md | 2 ++ inc/class.dc.translater.lang.php | 22 ++++++++------- inc/class.dc.translater.php | 46 +++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89ea78e..37929a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ - fix superadmin permissions - remove modules list tab and add button to existing lists - remove multi-modules import/export +- .po export become a requirment +- use l10n function sto generate .lang.php files 2021.09.02 - clean up code and fix typo diff --git a/inc/class.dc.translater.lang.php b/inc/class.dc.translater.lang.php index 701ef13..0b52ac6 100644 --- a/inc/class.dc.translater.lang.php +++ b/inc/class.dc.translater.lang.php @@ -125,22 +125,26 @@ class dcTranslaterLang public function getMsgIds(): array { $res = []; - $files = dcTranslater::scandir($this->module->root); - $scan_ext = ['php']; if ($this->translater->scan_tpl) { -// $scan_ext[] = 'html'; + $scan_ext[] = 'html'; } - foreach($files AS $file) { - if (is_dir($this->module->root . '/' . $file) - || !in_array(files::getExtension($file), $scan_ext)) { + $files = dcTranslater::scandir($this->module->root); + foreach($files as $file) { + $extension = files::getExtension($file); + if (is_dir($this->module->root . '/' . $file) || !in_array($extension, $scan_ext)) { continue; } - $contents = file_get_contents($this->module->root . '/' . $file); # php files - $msgs = dcTranslater::extractPhpMsgs($contents); + if ($extension == 'php') { + $msgs = dcTranslater::extractPhpMsgs($contents); + + # tpl files + } elseif ($extension == 'html') { + $msgs = dcTranslater::extractTplMsgs($contents); + } foreach($msgs as $msg) { $res[] = [ 'msgid' => dcTranslater::encodeMsg($msg[0][0]), @@ -150,8 +154,6 @@ class dcTranslaterLang ]; } - //TODO: tpl file extract - unset($contents); } return $res; diff --git a/inc/class.dc.translater.php b/inc/class.dc.translater.php index 5e9b776..c0ada4e 100644 --- a/inc/class.dc.translater.php +++ b/inc/class.dc.translater.php @@ -402,7 +402,7 @@ class dcTranslater * * @param string $content The contents * @param string $func The function name - * @return array The messages + * @return array The messages */ public static function extractPhpMsgs(string $content, string $func = '__'): array { @@ -447,6 +447,50 @@ class dcTranslater return $final_strings; } + /** + * Extract messages from a tpl contents + * + * support plurals + * + * @param string $content The contents + * @param string $func The function name + * @return array The messages + */ + public static function extractTplMsgs(string $content, string $func = 'tpl:lang'): array + { + $duplicate = $final_strings = $lines = []; + // split content by line to combine match/line on the end + $content = str_replace("\r\n", "\n", $content); + $o = 0; + $parts = explode("\n", $content); + foreach($parts as $li => $part) { + $m = explode('{{' . $func . ' ', $part); + for($i = 1; $i < count($m); $i++) { + $lines[$o] = $li+1; + $o++; + } + } + // split content by translation function + if (!preg_match_all('/\{\{' . preg_quote($func) . '\s([^}]+)\}\}/', $content, $parts)) { + return $final_strings; + } + // walk through parts + $p = 0; + foreach($parts[1] as $part) { + // strings exist + if (!empty($part)) { + // filter duplicate strings + if (!in_array($part, $duplicate)) { + // fill final array + $final_strings[] = [[$part], $lines[$p]]; + $duplicate[] = $part; + } + } + $p++; + } + return $final_strings; + } + /** * Encode a string *