re-add tpl extraction

master
Jean-Christian Paul Denis 2021-09-25 15:15:16 +02:00
parent 5934d267f1
commit c89b162336
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
3 changed files with 59 additions and 11 deletions

View File

@ -8,6 +8,8 @@
- fix superadmin permissions - fix superadmin permissions
- remove modules list tab and add button to existing lists - remove modules list tab and add button to existing lists
- remove multi-modules import/export - remove multi-modules import/export
- .po export become a requirment
- use l10n function sto generate .lang.php files
2021.09.02 2021.09.02
- clean up code and fix typo - clean up code and fix typo

View File

@ -125,22 +125,26 @@ class dcTranslaterLang
public function getMsgIds(): array public function getMsgIds(): array
{ {
$res = []; $res = [];
$files = dcTranslater::scandir($this->module->root);
$scan_ext = ['php']; $scan_ext = ['php'];
if ($this->translater->scan_tpl) { if ($this->translater->scan_tpl) {
// $scan_ext[] = 'html'; $scan_ext[] = 'html';
} }
foreach($files AS $file) { $files = dcTranslater::scandir($this->module->root);
if (is_dir($this->module->root . '/' . $file) foreach($files as $file) {
|| !in_array(files::getExtension($file), $scan_ext)) { $extension = files::getExtension($file);
if (is_dir($this->module->root . '/' . $file) || !in_array($extension, $scan_ext)) {
continue; continue;
} }
$contents = file_get_contents($this->module->root . '/' . $file); $contents = file_get_contents($this->module->root . '/' . $file);
# php files # 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) { foreach($msgs as $msg) {
$res[] = [ $res[] = [
'msgid' => dcTranslater::encodeMsg($msg[0][0]), 'msgid' => dcTranslater::encodeMsg($msg[0][0]),
@ -150,8 +154,6 @@ class dcTranslaterLang
]; ];
} }
//TODO: tpl file extract
unset($contents); unset($contents);
} }
return $res; return $res;

View File

@ -402,7 +402,7 @@ class dcTranslater
* *
* @param string $content The contents * @param string $content The contents
* @param string $func The function name * @param string $func The function name
* @return array The messages * @return array The messages
*/ */
public static function extractPhpMsgs(string $content, string $func = '__'): array public static function extractPhpMsgs(string $content, string $func = '__'): array
{ {
@ -447,6 +447,50 @@ class dcTranslater
return $final_strings; 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 * Encode a string
* *