2021-09-24 23:58:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @brief translater, a plugin for Dotclear 2
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-24 23:58:59 +00:00
|
|
|
* @package Dotclear
|
|
|
|
* @subpackage Plugin
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-24 23:58:59 +00:00
|
|
|
* @author Jean-Christian Denis & contributors
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-24 23:58:59 +00:00
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
|
|
|
class dcTranslaterLang
|
|
|
|
{
|
|
|
|
/** @var dcTranslater dcTranslater instance */
|
|
|
|
public $translater = null;
|
|
|
|
/** @var dcTranslaterModule dcTranslaterModule instance */
|
|
|
|
public $module = null;
|
|
|
|
|
|
|
|
/** @var array Lang properies */
|
|
|
|
private $prop = [];
|
|
|
|
|
|
|
|
public function __construct(dcTranslaterModule $module, string $lang)
|
|
|
|
{
|
|
|
|
$this->translater = $module->translater;
|
2021-11-01 21:32:32 +00:00
|
|
|
$this->module = $module;
|
2021-09-24 23:58:59 +00:00
|
|
|
|
2021-11-01 21:32:32 +00:00
|
|
|
$this->prop['code'] = $lang;
|
|
|
|
$this->prop['name'] = l10n::getLanguageName($lang);
|
2021-09-24 23:58:59 +00:00
|
|
|
$this->prop['plural'] = explode(':', l10n::getLanguagePluralExpression($lang));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a lang property
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-24 23:58:59 +00:00
|
|
|
* @param string $key The lang property key
|
|
|
|
* @return mixed The lang property value or null
|
|
|
|
*/
|
|
|
|
public function get(string $key)
|
2021-11-01 21:32:32 +00:00
|
|
|
{
|
2021-09-24 23:58:59 +00:00
|
|
|
return array_key_exists($key, $this->prop) ? $this->prop[$key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic get
|
|
|
|
*/
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
return $this->get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a lang messages
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-24 23:58:59 +00:00
|
|
|
* @return array The messages ids and translations
|
|
|
|
*/
|
|
|
|
public function getMessages(): array
|
|
|
|
{
|
|
|
|
$res = [];
|
|
|
|
$m_msgids = $this->getMsgIds();
|
|
|
|
$m_msgstrs = $this->getMsgStrs();
|
|
|
|
|
2021-11-01 21:32:32 +00:00
|
|
|
foreach ($this->translater->getModules() as $module) {
|
2021-09-24 23:58:59 +00:00
|
|
|
if ($module->id != $this->module->id) {
|
|
|
|
$m_o_msgstrs[$module->id] = $this->translater->getlang($module, $this->code)->getMsgStrs();
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:32:32 +00:00
|
|
|
$dc_module = new dcTranslaterModule($this->translater, ['id' => 'dotclear', 'root' => DC_ROOT]);
|
2022-11-13 17:40:31 +00:00
|
|
|
$dc_lang = new dcTranslaterLang($dc_module, $this->code);
|
2021-09-24 23:58:59 +00:00
|
|
|
$m_o_msgstrs['dotclear'] = $dc_lang->getMsgStrs();
|
|
|
|
|
|
|
|
# From id list
|
2021-11-01 21:32:32 +00:00
|
|
|
foreach ($m_msgids as $rs) {
|
|
|
|
$res[$rs['msgid']]['files'][] = [trim($rs['file'], '/'), $rs['line']];
|
2021-09-24 23:58:59 +00:00
|
|
|
$res[$rs['msgid']]['group'] = 'main';
|
|
|
|
$res[$rs['msgid']]['plural'] = $rs['msgid_plural'];
|
|
|
|
$res[$rs['msgid']]['msgstr'] = [''];
|
|
|
|
$res[$rs['msgid']]['in_dc'] = false;
|
|
|
|
$res[$rs['msgid']]['o_msgstrs'] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
# From str list
|
2021-11-01 21:32:32 +00:00
|
|
|
foreach ($m_msgstrs as $rs) {
|
2021-09-24 23:58:59 +00:00
|
|
|
if (!isset($res[$rs['msgid']])) {
|
|
|
|
$res[$rs['msgid']]['files'][] = [];
|
|
|
|
$res[$rs['msgid']]['in_dc'] = false;
|
|
|
|
$res[$rs['msgid']]['o_msgstrs'] = [];
|
|
|
|
}
|
|
|
|
$res[$rs['msgid']]['group'] = $rs['group'];
|
|
|
|
$res[$rs['msgid']]['plural'] = $rs['msgid_plural'];
|
|
|
|
$res[$rs['msgid']]['msgstr'] = is_array($rs['msgstr']) ? $rs['msgstr'] : [$rs['msgstr']];
|
|
|
|
$res[$rs['msgid']]['in_dc'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
# From others str list
|
2021-11-01 21:32:32 +00:00
|
|
|
foreach ($m_o_msgstrs as $o_module => $o_msgstrs) {
|
|
|
|
foreach ($o_msgstrs as $rs) {
|
2021-09-24 23:58:59 +00:00
|
|
|
if (!isset($res[$rs['msgid']])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res[$rs['msgid']]['o_msgstrs'][] = [
|
|
|
|
'msgstr' => is_array($rs['msgstr']) ? $rs['msgstr'] : [$rs['msgstr']],
|
|
|
|
'module' => $o_module,
|
2022-11-13 17:40:31 +00:00
|
|
|
'file' => $rs['file'],
|
2021-09-24 23:58:59 +00:00
|
|
|
];
|
|
|
|
if ($o_module == 'dotclear') {
|
|
|
|
$res[$rs['msgid']]['in_dc'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:32:32 +00:00
|
|
|
|
2021-09-24 23:58:59 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get messages ids
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-24 23:58:59 +00:00
|
|
|
* @return array The messages ids
|
|
|
|
*/
|
|
|
|
public function getMsgIds(): array
|
|
|
|
{
|
2021-11-01 21:32:32 +00:00
|
|
|
$res = [];
|
2021-09-24 23:58:59 +00:00
|
|
|
$scan_ext = ['php'];
|
|
|
|
if ($this->translater->scan_tpl) {
|
2021-09-25 13:15:16 +00:00
|
|
|
$scan_ext[] = 'html';
|
2021-09-24 23:58:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 13:15:16 +00:00
|
|
|
$files = dcTranslater::scandir($this->module->root);
|
2021-11-01 21:32:32 +00:00
|
|
|
foreach ($files as $file) {
|
2021-09-25 13:15:16 +00:00
|
|
|
$extension = files::getExtension($file);
|
|
|
|
if (is_dir($this->module->root . '/' . $file) || !in_array($extension, $scan_ext)) {
|
2021-09-24 23:58:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$contents = file_get_contents($this->module->root . '/' . $file);
|
2022-11-13 17:40:31 +00:00
|
|
|
$msgs = [];
|
2021-09-24 23:58:59 +00:00
|
|
|
# php files
|
2021-09-25 13:15:16 +00:00
|
|
|
if ($extension == 'php') {
|
|
|
|
$msgs = dcTranslater::extractPhpMsgs($contents);
|
|
|
|
|
|
|
|
# tpl files
|
|
|
|
} elseif ($extension == 'html') {
|
|
|
|
$msgs = dcTranslater::extractTplMsgs($contents);
|
|
|
|
}
|
2021-11-01 21:32:32 +00:00
|
|
|
foreach ($msgs as $msg) {
|
2021-09-24 23:58:59 +00:00
|
|
|
$res[] = [
|
|
|
|
'msgid' => dcTranslater::encodeMsg($msg[0][0]),
|
|
|
|
'msgid_plural' => empty($msg[0][1]) ? '' : dcTranslater::encodeMsg($msg[0][1]),
|
|
|
|
'file' => $file,
|
2022-11-13 17:40:31 +00:00
|
|
|
'line' => $msg[1],
|
2021-09-24 23:58:59 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($contents);
|
|
|
|
}
|
2021-11-01 21:32:32 +00:00
|
|
|
|
2021-09-24 23:58:59 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get messages translations
|
2021-11-01 21:32:32 +00:00
|
|
|
*
|
2021-09-24 23:58:59 +00:00
|
|
|
* @return array The messages translations
|
|
|
|
*/
|
|
|
|
public function getMsgStrs(): array
|
|
|
|
{
|
2021-09-25 12:37:49 +00:00
|
|
|
$res = $exists = $scanned = [];
|
2021-09-24 23:58:59 +00:00
|
|
|
|
|
|
|
$langs = $this->module->getLangs(true);
|
|
|
|
if (!isset($langs[$this->code])) {
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:32:32 +00:00
|
|
|
foreach ($langs[$this->code] as $file) {
|
2021-09-24 23:58:59 +00:00
|
|
|
if (in_array($file, $scanned)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$scanned[] = $file;
|
2021-11-01 21:32:32 +00:00
|
|
|
$path = path::clean($this->module->locales . '/' . $file);
|
2021-09-24 23:58:59 +00:00
|
|
|
|
|
|
|
if (dcTranslater::isPoFile($file)) {
|
|
|
|
$po = l10n::parsePoFile($path);
|
|
|
|
if (!is_array($po)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$entries = $po[1];
|
2021-11-01 21:32:32 +00:00
|
|
|
foreach ($entries as $entry) {
|
2021-09-25 12:37:49 +00:00
|
|
|
$res[] = [
|
2021-11-01 21:32:32 +00:00
|
|
|
'msgid' => $entry['msgid'],
|
2021-09-24 23:58:59 +00:00
|
|
|
'msgid_plural' => $entry['msgid_plural'] ?? '',
|
2021-11-01 21:32:32 +00:00
|
|
|
'msgstr' => is_array($entry['msgstr']) ? $entry['msgstr'] : [$entry['msgstr']],
|
|
|
|
'lang' => $this->code,
|
|
|
|
'type' => 'po',
|
|
|
|
'path' => $path,
|
|
|
|
'file' => basename($file),
|
2022-11-13 17:40:31 +00:00
|
|
|
'group' => str_replace('.po', '', basename($file)),
|
2021-09-25 12:37:49 +00:00
|
|
|
];
|
|
|
|
$exists[] = $entry['msgid'];
|
2021-09-24 23:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:32:32 +00:00
|
|
|
|
2021-09-24 23:58:59 +00:00
|
|
|
return $res;
|
|
|
|
}
|
2021-11-01 21:32:32 +00:00
|
|
|
}
|