re-add tpl extraction
This commit is contained in:
parent
5934d267f1
commit
c89b162336
3 changed files with 59 additions and 11 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue