update PSR CS

master
Jean-Christian Paul Denis 2021-11-01 22:32:32 +01:00
parent ba70d79862
commit ce6b10dd3c
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
16 changed files with 644 additions and 582 deletions

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return; return;
} }
@ -24,7 +23,7 @@ $_menu['Plugins']->addItem(
$core->adminurl->get('translater'), $core->adminurl->get('translater'),
dcPage::getPF('translater/icon.png'), dcPage::getPF('translater/icon.png'),
preg_match( preg_match(
'/' . preg_quote($core->adminurl->get('translater')) . '(&.*)?$/', '/' . preg_quote($core->adminurl->get('translater')) . '(&.*)?$/',
$_SERVER['REQUEST_URI'] $_SERVER['REQUEST_URI']
), ),
$core->auth->isSuperAdmin() $core->auth->isSuperAdmin()
@ -37,7 +36,7 @@ class translaterAdminBehaviors
/** /**
* Create instance of dcTranslater once * Create instance of dcTranslater once
* *
* @param dCore $core dcCore instance * @param dCore $core dcCore instance
* @return dctranslater dcTranslater instance * @return dctranslater dcTranslater instance
*/ */
@ -46,12 +45,13 @@ class translaterAdminBehaviors
if (!(self::$translater instanceof dcTranslater)) { if (!(self::$translater instanceof dcTranslater)) {
self::$translater = new dcTranslater($core, false); self::$translater = new dcTranslater($core, false);
} }
return self::$translater; return self::$translater;
} }
/** /**
* Add button to go to module translation * Add button to go to module translation
* *
* @param object $list adminModulesList instance * @param object $list adminModulesList instance
* @param string $id Module id * @param string $id Module id
* @param arrray $prop Module properties * @param arrray $prop Module properties
@ -59,27 +59,27 @@ class translaterAdminBehaviors
*/ */
public static function adminModulesGetActions(adminModulesList $list, string $id, array $prop): ?string public static function adminModulesGetActions(adminModulesList $list, string $id, array $prop): ?string
{ {
if ($list->getList() != $prop['type'] . '-activate' if ($list->getList() != $prop['type'] . '-activate'
|| !self::translater($list->core)->getSetting($prop['type'] . '_menu') || !self::translater($list->core)->getSetting($prop['type'] . '_menu')
|| !$list->core->auth->isSuperAdmin() || !$list->core->auth->isSuperAdmin()
) { ) {
return null; return null;
} }
if (self::translater($list->core)->getSetting('hide_default') if (self::translater($list->core)->getSetting('hide_default')
&& in_array($id, dctranslater::$default_distrib_modules[$prop['type']]) && in_array($id, dctranslater::$default_distrib_modules[$prop['type']])
) { ) {
return null; return null;
} }
return return
' <input type="submit" name="translater[' . ' <input type="submit" name="translater[' .
html::escapeHTML($id) . html::escapeHTML($id) .
']" value="' . __('Translate') . '" /> '; ']" value="' . __('Translate') . '" /> ';
} }
/** /**
* Redirect to module translation * Redirect to module translation
* *
* @param adminModulesList $list adminModulesList instance * @param adminModulesList $list adminModulesList instance
* @param array $modules Selected modules ids * @param array $modules Selected modules ids
* @param string $type List type (plugin|theme) * @param string $type List type (plugin|theme)
@ -91,7 +91,7 @@ class translaterAdminBehaviors
} }
$list->core->adminurl->redirect( $list->core->adminurl->redirect(
'translater', 'translater',
['part' => 'module', 'type' => $type, 'module' => key($_POST['translater'])], ['part' => 'module', 'type' => $type, 'module' => key($_POST['translater'])],
'#module-lang' '#module-lang'
); );
@ -99,7 +99,7 @@ class translaterAdminBehaviors
/** /**
* Add dashboard favorites icon * Add dashboard favorites icon
* *
* @param dcCore $core dcCore instance * @param dcCore $core dcCore instance
* @param dcFavorites $favs dcFavorites instance * @param dcFavorites $favs dcFavorites instance
*/ */
@ -113,4 +113,4 @@ class translaterAdminBehaviors
'permissions' => $core->auth->isSuperAdmin() 'permissions' => $core->auth->isSuperAdmin()
]); ]);
} }
} }

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_MODULE')) { if (!defined('DC_CONTEXT_MODULE')) {
return null; return null;
} }
@ -19,14 +18,14 @@ $translater = new dcTranslater($core);
if (!empty($_POST['save'])) { if (!empty($_POST['save'])) {
try { try {
foreach($translater->getDefaultSettings() as $k => $v) { foreach ($translater->getDefaultSettings() as $k => $v) {
$translater->setSetting($k, (isset($_POST[$k]) ? $_POST[$k] : '')); $translater->setSetting($k, ($_POST[$k] ?? ''));
} }
dcPage::addSuccessNotice( dcPage::addSuccessNotice(
__('Configuration successfully updated.') __('Configuration successfully updated.')
); );
$core->adminurl->redirect( $core->adminurl->redirect(
'admin.plugins', 'admin.plugins',
['module' => 'translater', 'conf' => 1, 'redir' => $list->getRedir()] ['module' => 'translater', 'conf' => 1, 'redir' => $list->getRedir()]
); );
} catch (Exception $e) { } catch (Exception $e) {
@ -42,7 +41,7 @@ __('Write .lang.php files') . '</label></p>
<p><label for="scan_tpl">' . <p><label for="scan_tpl">' .
form::checkbox('scan_tpl', '1', $translater->scan_tpl) . form::checkbox('scan_tpl', '1', $translater->scan_tpl) .
__('Translate also strings of template files') . '</label></p> __('Translate also strings of template files') . '</label></p>
<p><label for="parse_nodc">' . <p><label for="parse_nodc">' .
form::checkbox('parse_nodc', '1', $translater->parse_nodc) . form::checkbox('parse_nodc', '1', $translater->parse_nodc) .
__('Translate only unknow strings') . '</label></p> __('Translate only unknow strings') . '</label></p>
<p><label for="hide_default">' . <p><label for="hide_default">' .
@ -52,18 +51,20 @@ __('Hide default modules of Dotclear') . '</label></p>
form::checkbox('parse_comment', '1', $translater->parse_comment) . form::checkbox('parse_comment', '1', $translater->parse_comment) .
__('Write comments in files') . '</label></p> __('Write comments in files') . '</label></p>
<p><label for="parse_user">' . <p><label for="parse_user">' .
form::checkbox('parse_user', '1', $translater->parse_user) . form::checkbox('parse_user', '1', $translater->parse_user) .
__('Write informations about author in files') . '</label></p> __('Write informations about author in files') . '</label></p>
<p><label for="parse_userinfo">' . __('User info:') . '</label>' . <p><label for="parse_userinfo">' . __('User info:') . '</label>' .
form::field('parse_userinfo', 65, 255, $translater->parse_userinfo) . '</p> form::field('parse_userinfo', 65, 255, $translater->parse_userinfo) . '</p>
<p class="form-note">' . sprintf( <p class="form-note">' . sprintf(
__('Following informations can be used: %s'), implode(', ', $translater::$allowed_user_informations)) . ' __('Following informations can be used: %s'),
implode(', ', $translater::$allowed_user_informations)
) . '
</p> </p>
</div> </div>
<div class="fieldset"><h4>' . __('Import/Export') . '</h4> <div class="fieldset"><h4>' . __('Import/Export') . '</h4>
<p><label for="import_overwrite">' . <p><label for="import_overwrite">' .
form::checkbox('import_overwrite', '1', $translater->import_overwrite) . form::checkbox('import_overwrite', '1', $translater->import_overwrite) .
__('Overwrite existing languages') . '</label></p> __('Overwrite existing languages') . '</label></p>
<p><label for="export_filename">' . __('Name of exported package:') . '</label>' . <p><label for="export_filename">' . __('Name of exported package:') . '</label>' .
form::field('export_filename', 65, 255, $translater->export_filename) . '</p> form::field('export_filename', 65, 255, $translater->export_filename) . '</p>
@ -73,25 +74,27 @@ form::field('export_filename', 65, 255, $translater->export_filename) . '</p>
<p><label for="backup_auto">' . <p><label for="backup_auto">' .
form::checkbox('backup_auto', '1', $translater->backup_auto) . form::checkbox('backup_auto', '1', $translater->backup_auto) .
__('Make backups when changes are made') . '</label></p> __('Make backups when changes are made') . '</label></p>
<p><label for="backup_limit" class="classic">' . sprintf(__('Limit backups to %s files per module'), <p><label for="backup_limit" class="classic">' . sprintf(
form::number('backup_limit', ['min' => 0, 'max' => 50, 'default' => $translater->backup_limit])) . '</label></p> __('Limit backups to %s files per module'),
form::number('backup_limit', ['min' => 0, 'max' => 50, 'default' => $translater->backup_limit])
) . '</label></p>
<p><label for="backup_folder">' . __('Store backups in:') . '</label>' . <p><label for="backup_folder">' . __('Store backups in:') . '</label>' .
form::combo('backup_folder', $translater::$allowed_backup_folders, $translater->backup_folder) . '</p> form::combo('backup_folder', $translater::$allowed_backup_folders, $translater->backup_folder) . '</p>
</div> </div>
<div class="fieldset"><h4>' . __('Behaviors') . '</h4> <div class="fieldset"><h4>' . __('Behaviors') . '</h4>
<p><label for="start_page">' . __('Default start menu:') . '</label>' . <p><label for="start_page">' . __('Default start menu:') . '</label>' .
form::combo('start_page',[ form::combo('start_page', [
__('Plugins') => 'plugin', __('Plugins') => 'plugin',
__('Themes') => 'theme', __('Themes') => 'theme',
__('Home') => '-' __('Home') => '-'
], $translater->start_page) . '</p> ], $translater->start_page) . '</p>
<p><label for="plugin_menu">' . <p><label for="plugin_menu">' .
form::checkbox('plugin_menu', '1', $translater->plugin_menu) . form::checkbox('plugin_menu', '1', $translater->plugin_menu) .
__('Enable menu on plugins page') . '</label></p> __('Enable menu on plugins page') . '</label></p>
<p><label for="theme_menu">' . <p><label for="theme_menu">' .
form::checkbox('theme_menu', '1', $translater->theme_menu) . form::checkbox('theme_menu', '1', $translater->theme_menu) .
__('Enable menu on themes page') . '</label></p> __('Enable menu on themes page') . '</label></p>
</div>'; </div>';
dcPage::helpBlock('translater.config'); dcPage::helpBlock('translater.config');

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { if (!defined('DC_RC_PATH')) {
return; return;
} }
@ -21,11 +20,11 @@ $this->registerModule(
'Jean-Christian Denis & contributors', 'Jean-Christian Denis & contributors',
'2021.09.28', '2021.09.28',
[ [
'requires' => [['core', '2.19']], 'requires' => [['core', '2.19']],
'permissions' => null, 'permissions' => null,
'type' => 'plugin', 'type' => 'plugin',
'support' => 'http://forum.dotclear.org/viewtopic.php?id=39220', 'support' => 'http://forum.dotclear.org/viewtopic.php?id=39220',
'details' => 'https://plugins.dotaddict.org/dc2/details/translater', 'details' => 'https://plugins.dotaddict.org/dc2/details/translater',
'repository' => 'https://raw.githubusercontent.com/JcDenis/translater/master/dcstore.xml' 'repository' => 'https://raw.githubusercontent.com/JcDenis/translater/master/dcstore.xml'
] ]
); );

View File

@ -1,27 +1,27 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return null; return null;
} }
$id = 'translater'; $id = 'translater';
try { try {
if (version_compare($core->getVersion($id), $core->plugins->moduleInfo($id, 'version'), '>=')) { if (version_compare($core->getVersion($id), $core->plugins->moduleInfo($id, 'version'), '>=')) {
return null; return null;
} }
$translater = new dcTranslater($core, false); $translater = new dcTranslater($core, false);
$settings = $translater->getDefaultSettings(); $settings = $translater->getDefaultSettings();
foreach($settings as $key => $setting) { foreach ($settings as $key => $setting) {
$translater->setSetting($key, $setting['value'], false); $translater->setSetting($key, $setting['value'], false);
} }
$core->setVersion($id, $core->plugins->moduleInfo($id, 'version')); $core->setVersion($id, $core->plugins->moduleInfo($id, 'version'));
@ -30,4 +30,5 @@ try {
} catch (Exception $e) { } catch (Exception $e) {
$core->error->add($e->getMessage()); $core->error->add($e->getMessage());
} }
return false;
return false;

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { if (!defined('DC_RC_PATH')) {
return; return;
} }
@ -24,4 +23,4 @@ $__autoload['translaterRest'] = $d . 'class.translater.rest.php';
if (isset($core->adminurl)) { if (isset($core->adminurl)) {
$core->adminurl->register('translater', 'plugin.php', ['p' => 'translater']); $core->adminurl->register('translater', 'plugin.php', ['p' => 'translater']);
} }

View File

@ -1,58 +1,81 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return; return;
} }
$this->addUserAction( $this->addUserAction(
/* type */ 'settings', /* type */
/* action */ 'delete_all', 'settings',
/* ns */ 'translater', /* action */
/* description */ __('delete all settings') 'delete_all',
/* ns */
'translater',
/* description */
__('delete all settings')
); );
$this->addUserAction( $this->addUserAction(
/* type */ 'plugins', /* type */
/* action */ 'delete', 'plugins',
/* ns */ 'translater', /* action */
/* description */ __('delete plugin files') 'delete',
/* ns */
'translater',
/* description */
__('delete plugin files')
); );
$this->addUserAction( $this->addUserAction(
/* type */ 'versions', /* type */
/* action */ 'delete', 'versions',
/* ns */ 'translater', /* action */
/* description */ __('delete the version number') 'delete',
/* ns */
'translater',
/* description */
__('delete the version number')
); );
$this->addDirectAction( $this->addDirectAction(
/* type */ 'settings', /* type */
/* action */ 'delete_all', 'settings',
/* ns */ 'translater', /* action */
/* description */ sprintf(__('delete all %s settings'), 'translater') 'delete_all',
/* ns */
'translater',
/* description */
sprintf(__('delete all %s settings'), 'translater')
); );
$this->addDirectAction( $this->addDirectAction(
/* type */ 'plugins', /* type */
/* action */ 'delete', 'plugins',
/* ns */ 'translater', /* action */
/* description */ sprintf(__('delete %s plugin files'), 'translater') 'delete',
/* ns */
'translater',
/* description */
sprintf(__('delete %s plugin files'), 'translater')
); );
$this->addDirectAction( $this->addDirectAction(
/* type */ 'versions', /* type */
/* action */ 'delete', 'versions',
/* ns */ 'translater', /* action */
/* description */ sprintf(__('delete %s version number'), 'translater') 'delete',
); /* ns */
'translater',
/* description */
sprintf(__('delete %s version number'), 'translater')
);

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
class dcTranslaterLang class dcTranslaterLang
{ {
/** @var dCore dcCore instance */ /** @var dCore dcCore instance */
@ -25,23 +24,23 @@ class dcTranslaterLang
public function __construct(dcTranslaterModule $module, string $lang) public function __construct(dcTranslaterModule $module, string $lang)
{ {
$this->core = $module->core; $this->core = $module->core;
$this->translater = $module->translater; $this->translater = $module->translater;
$this->module = $module; $this->module = $module;
$this->prop['code'] = $lang; $this->prop['code'] = $lang;
$this->prop['name'] = l10n::getLanguageName($lang); $this->prop['name'] = l10n::getLanguageName($lang);
$this->prop['plural'] = explode(':', l10n::getLanguagePluralExpression($lang)); $this->prop['plural'] = explode(':', l10n::getLanguagePluralExpression($lang));
} }
/** /**
* Get a lang property * Get a lang property
* *
* @param string $key The lang property key * @param string $key The lang property key
* @return mixed The lang property value or null * @return mixed The lang property value or null
*/ */
public function get(string $key) public function get(string $key)
{ {
return array_key_exists($key, $this->prop) ? $this->prop[$key] : null; return array_key_exists($key, $this->prop) ? $this->prop[$key] : null;
} }
@ -55,7 +54,7 @@ class dcTranslaterLang
/** /**
* Get a lang messages * Get a lang messages
* *
* @return array The messages ids and translations * @return array The messages ids and translations
*/ */
public function getMessages(): array public function getMessages(): array
@ -64,18 +63,18 @@ class dcTranslaterLang
$m_msgids = $this->getMsgIds(); $m_msgids = $this->getMsgIds();
$m_msgstrs = $this->getMsgStrs(); $m_msgstrs = $this->getMsgStrs();
foreach($this->translater->getModules() as $module) { foreach ($this->translater->getModules() as $module) {
if ($module->id != $this->module->id) { if ($module->id != $this->module->id) {
$m_o_msgstrs[$module->id] = $this->translater->getlang($module, $this->code)->getMsgStrs(); $m_o_msgstrs[$module->id] = $this->translater->getlang($module, $this->code)->getMsgStrs();
} }
} }
$dc_module = new dcTranslaterModule($this->translater, ['id' => 'dotclear', 'root' => DC_ROOT]); $dc_module = new dcTranslaterModule($this->translater, ['id' => 'dotclear', 'root' => DC_ROOT]);
$dc_lang = new dctranslaterLang($dc_module, $this->code); $dc_lang = new dctranslaterLang($dc_module, $this->code);
$m_o_msgstrs['dotclear'] = $dc_lang->getMsgStrs(); $m_o_msgstrs['dotclear'] = $dc_lang->getMsgStrs();
# From id list # From id list
foreach($m_msgids as $rs) { foreach ($m_msgids as $rs) {
$res[$rs['msgid']]['files'][] = [trim($rs['file'],'/'), $rs['line']]; $res[$rs['msgid']]['files'][] = [trim($rs['file'], '/'), $rs['line']];
$res[$rs['msgid']]['group'] = 'main'; $res[$rs['msgid']]['group'] = 'main';
$res[$rs['msgid']]['plural'] = $rs['msgid_plural']; $res[$rs['msgid']]['plural'] = $rs['msgid_plural'];
$res[$rs['msgid']]['msgstr'] = ['']; $res[$rs['msgid']]['msgstr'] = [''];
@ -84,8 +83,7 @@ class dcTranslaterLang
} }
# From str list # From str list
foreach($m_msgstrs as $rs) { foreach ($m_msgstrs as $rs) {
if (!isset($res[$rs['msgid']])) { if (!isset($res[$rs['msgid']])) {
$res[$rs['msgid']]['files'][] = []; $res[$rs['msgid']]['files'][] = [];
$res[$rs['msgid']]['in_dc'] = false; $res[$rs['msgid']]['in_dc'] = false;
@ -98,8 +96,8 @@ class dcTranslaterLang
} }
# From others str list # From others str list
foreach($m_o_msgstrs as $o_module => $o_msgstrs) { foreach ($m_o_msgstrs as $o_module => $o_msgstrs) {
foreach($o_msgstrs as $rs) { foreach ($o_msgstrs as $rs) {
if (!isset($res[$rs['msgid']])) { if (!isset($res[$rs['msgid']])) {
continue; continue;
} }
@ -114,24 +112,25 @@ class dcTranslaterLang
} }
} }
} }
return $res; return $res;
} }
/** /**
* Get messages ids * Get messages ids
* *
* @return array The messages ids * @return array The messages ids
*/ */
public function getMsgIds(): array public function getMsgIds(): array
{ {
$res = []; $res = [];
$scan_ext = ['php']; $scan_ext = ['php'];
if ($this->translater->scan_tpl) { if ($this->translater->scan_tpl) {
$scan_ext[] = 'html'; $scan_ext[] = 'html';
} }
$files = dcTranslater::scandir($this->module->root); $files = dcTranslater::scandir($this->module->root);
foreach($files as $file) { foreach ($files as $file) {
$extension = files::getExtension($file); $extension = files::getExtension($file);
if (is_dir($this->module->root . '/' . $file) || !in_array($extension, $scan_ext)) { if (is_dir($this->module->root . '/' . $file) || !in_array($extension, $scan_ext)) {
continue; continue;
@ -145,7 +144,7 @@ class dcTranslaterLang
} elseif ($extension == 'html') { } elseif ($extension == 'html') {
$msgs = dcTranslater::extractTplMsgs($contents); $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]),
'msgid_plural' => empty($msg[0][1]) ? '' : dcTranslater::encodeMsg($msg[0][1]), 'msgid_plural' => empty($msg[0][1]) ? '' : dcTranslater::encodeMsg($msg[0][1]),
@ -156,12 +155,13 @@ class dcTranslaterLang
unset($contents); unset($contents);
} }
return $res; return $res;
} }
/** /**
* Get messages translations * Get messages translations
* *
* @return array The messages translations * @return array The messages translations
*/ */
public function getMsgStrs(): array public function getMsgStrs(): array
@ -173,12 +173,12 @@ class dcTranslaterLang
return $res; return $res;
} }
foreach($langs[$this->code] as $file) { foreach ($langs[$this->code] as $file) {
if (in_array($file, $scanned)) { if (in_array($file, $scanned)) {
continue; continue;
} }
$scanned[] = $file; $scanned[] = $file;
$path = path::clean($this->module->locales . '/' . $file); $path = path::clean($this->module->locales . '/' . $file);
if (dcTranslater::isPoFile($file)) { if (dcTranslater::isPoFile($file)) {
$po = l10n::parsePoFile($path); $po = l10n::parsePoFile($path);
@ -186,22 +186,22 @@ class dcTranslaterLang
continue; continue;
} }
$entries = $po[1]; $entries = $po[1];
foreach($entries as $entry) { foreach ($entries as $entry) {
$res[] = [ $res[] = [
'msgid' => $entry['msgid'], 'msgid' => $entry['msgid'],
'msgid_plural' => $entry['msgid_plural'] ?? '', 'msgid_plural' => $entry['msgid_plural'] ?? '',
'msgstr' => is_array($entry['msgstr']) ? $entry['msgstr'] : [$entry['msgstr']], 'msgstr' => is_array($entry['msgstr']) ? $entry['msgstr'] : [$entry['msgstr']],
'lang' => $this->code, 'lang' => $this->code,
'type' => 'po', 'type' => 'po',
'path' => $path, 'path' => $path,
'file' => basename($file), 'file' => basename($file),
'group'=> str_replace('.po', '', basename($file)) 'group' => str_replace('.po', '', basename($file))
]; ];
$exists[] = $entry['msgid']; $exists[] = $entry['msgid'];
} }
} }
} }
return $res; return $res;
} }
} }

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return; return;
} }
@ -40,20 +39,20 @@ class dcTranslaterModule
$this->translater = $translater; $this->translater = $translater;
$this->prop = $module; $this->prop = $module;
$this->prop['root'] = path::real($this->prop['root']); $this->prop['root'] = path::real($this->prop['root']);
$i = path::info($this->prop['root']); $i = path::info($this->prop['root']);
$this->prop['basename'] = $i['basename']; $this->prop['basename'] = $i['basename'];
$this->prop['locales'] = $this->prop['root'] . '/locales'; $this->prop['locales'] = $this->prop['root'] . '/locales';
} }
/** /**
* Get a module property * Get a module property
* *
* @param string $key The module property key * @param string $key The module property key
* @return mixed The module property value or null * @return mixed The module property value or null
*/ */
public function get(string $key) public function get(string $key)
{ {
return array_key_exists($key, $this->prop) ? $this->prop[$key] : null; return array_key_exists($key, $this->prop) ? $this->prop[$key] : null;
} }
@ -69,18 +68,19 @@ class dcTranslaterModule
//@{ //@{
/** /**
* Find backup folder of a module * Find backup folder of a module
* *
* @param boolean $throw Silently failed * @param boolean $throw Silently failed
* @return mixed The backup folder directory or false * @return mixed The backup folder directory or false
*/ */
public function getBackupRoot(bool $throw = false) public function getBackupRoot(bool $throw = false)
{ {
$dir = false; $dir = false;
switch($this->translater->backup_folder) { switch ($this->translater->backup_folder) {
case 'module': case 'module':
if ($this->prop['root_writable']) { if ($this->prop['root_writable']) {
$dir = $this->prop['locales']; $dir = $this->prop['locales'];
} }
break; break;
case 'plugin': case 'plugin':
@ -88,6 +88,7 @@ class dcTranslaterModule
if ($tmp && is_writable($tmp)) { if ($tmp && is_writable($tmp)) {
$dir = $tmp; $dir = $tmp;
} }
break; break;
case 'public': case 'public':
@ -95,6 +96,7 @@ class dcTranslaterModule
if ($tmp && is_writable($tmp)) { if ($tmp && is_writable($tmp)) {
$dir = $tmp; $dir = $tmp;
} }
break; break;
case 'cache': case 'cache':
@ -103,6 +105,7 @@ class dcTranslaterModule
@mkDir($tmp . '/l10n'); @mkDir($tmp . '/l10n');
$dir = $tmp . '/l10n'; $dir = $tmp . '/l10n';
} }
break; break;
case 'translater': case 'translater':
@ -111,11 +114,13 @@ class dcTranslaterModule
@mkDir($tmp . '/locales'); @mkDir($tmp . '/locales');
$dir = $tmp . '/locales'; $dir = $tmp . '/locales';
} }
break; break;
} }
if (!$dir && $throw) { if (!$dir && $throw) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Failed to find backups folder for module %s'), $id __('Failed to find backups folder for module %s'),
$id
)); ));
} }
@ -124,7 +129,7 @@ class dcTranslaterModule
/** /**
* Get a list of available backups * Get a list of available backups
* *
* @param boolean $return_filename Return only filenames * @param boolean $return_filename Return only filenames
* @return array The module backups info * @return array The module backups info
*/ */
@ -135,13 +140,13 @@ class dcTranslaterModule
return []; return [];
} }
$res = []; $res = [];
$files = dcTranslater::scandir($backup); $files = dcTranslater::scandir($backup);
foreach($files AS $file) { foreach ($files as $file) {
$is_backup = preg_match(sprintf($this->backup_file_regexp, preg_quote($this->prop['id'])), $file, $m); $is_backup = preg_match(sprintf($this->backup_file_regexp, preg_quote($this->prop['id'])), $file, $m);
if (is_dir($backup . '/' . $file) if (is_dir($backup . '/' . $file)
|| !$is_backup || !$is_backup
|| !l10n::isCode($m[1]) || !l10n::isCode($m[1])
) { ) {
continue; continue;
@ -158,12 +163,13 @@ class dcTranslaterModule
$res[$m[1]][$file]['module'] = $this->prop['id']; $res[$m[1]][$file]['module'] = $this->prop['id'];
} }
} }
return $res; return $res;
} }
/** /**
* Create a backup * Create a backup
* *
* @param string $lang The backup lang * @param string $lang The backup lang
* @return boolean True on success * @return boolean True on success
*/ */
@ -173,18 +179,18 @@ class dcTranslaterModule
if (!is_dir($this->prop['locales'] . '/' . $lang)) { if (!is_dir($this->prop['locales'] . '/' . $lang)) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Failed to find language %s'), $lang __('Failed to find language %s'),
$lang
)); ));
} }
$res = []; $res = [];
$files = dcTranslater::scandir($this->prop['locales'] . '/' . $lang); $files = dcTranslater::scandir($this->prop['locales'] . '/' . $lang);
foreach($files as $file) { foreach ($files as $file) {
if (!is_dir($this->prop['locales'] . '/' . $lang . '/' . $file) if (!is_dir($this->prop['locales'] . '/' . $lang . '/' . $file)
&& (dcTranslater::isLangphpFile($file) || dcTranslater::isPoFile($file)) && (dcTranslater::isLangphpFile($file) || dcTranslater::isPoFile($file))
) { ) {
$res[$this->prop['locales'] . '/' . $lang . '/' .$file] = $res[$this->prop['locales'] . '/' . $lang . '/' . $file] = $this->prop['id'] . '/locales/' . $lang . '/' . $file;
$this->prop['id'] . '/locales/' . $lang . '/' . $file;
} }
} }
@ -192,9 +198,9 @@ class dcTranslaterModule
dcTranslater::isBackupLimit($backup, $this->translater->backup_limit, true); dcTranslater::isBackupLimit($backup, $this->translater->backup_limit, true);
@set_time_limit(300); @set_time_limit(300);
$fp = fopen($backup . '/l10n-' . $this->prop['id'] . '-' . $lang . '-' . time() . '.bck.zip', 'wb'); $fp = fopen($backup . '/l10n-' . $this->prop['id'] . '-' . $lang . '-' . time() . '.bck.zip', 'wb');
$zip = new fileZip($fp); $zip = new fileZip($fp);
foreach($res AS $from => $to) { foreach ($res as $from => $to) {
$zip->addFile($from, $to); $zip->addFile($from, $to);
} }
$zip->write(); $zip->write();
@ -207,7 +213,7 @@ class dcTranslaterModule
/** /**
* Retore a backup * Retore a backup
* *
* @param string $file The backup filename * @param string $file The backup filename
* @return boolean True on success * @return boolean True on success
*/ */
@ -217,14 +223,15 @@ class dcTranslaterModule
if (!file_exists($backup . '/' . $file)) { if (!file_exists($backup . '/' . $file)) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Failed to find file %s'), $file __('Failed to find file %s'),
$file
)); ));
} }
$zip = new fileUnzip($backup . '/' . $file); $zip = new fileUnzip($backup . '/' . $file);
$zip_files = $zip->getFilesList(); $zip_files = $zip->getFilesList();
foreach($zip_files AS $zip_file) { foreach ($zip_files as $zip_file) {
$f = $this->parseZipFilename($zip_file, true); $f = $this->parseZipFilename($zip_file, true);
$zip->unzip($zip_file, $this->prop['locales'] . '/' . $f['lang'] . '/' . $f['group'] . $f['ext']); $zip->unzip($zip_file, $this->prop['locales'] . '/' . $f['lang'] . '/' . $f['group'] . $f['ext']);
$done = true; $done = true;
@ -237,7 +244,7 @@ class dcTranslaterModule
/** /**
* Delete a module backup * Delete a module backup
* *
* @param string $file The backup filename * @param string $file The backup filename
* @return boolean True on success * @return boolean True on success
*/ */
@ -247,8 +254,8 @@ class dcTranslaterModule
$is_backup = preg_match(sprintf($this->backup_file_regexp, preg_quote($this->prop['id'])), $file, $m); $is_backup = preg_match(sprintf($this->backup_file_regexp, preg_quote($this->prop['id'])), $file, $m);
if (!file_exists($backup . '/' . $file) if (!file_exists($backup . '/' . $file)
|| !$is_backup || !$is_backup
|| !l10n::isCode($m[1]) || !l10n::isCode($m[1])
) { ) {
return false; return false;
@ -256,7 +263,8 @@ class dcTranslaterModule
if (!files::isDeletable($backup . '/' . $file)) { if (!files::isDeletable($backup . '/' . $file)) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Failed to delete file %s'), $file __('Failed to delete file %s'),
$file
)); ));
} }
@ -267,7 +275,7 @@ class dcTranslaterModule
/** /**
* Import a language pack * Import a language pack
* *
* @param array $zip_file The uploaded file info * @param array $zip_file The uploaded file info
* @return boolean True on success * @return boolean True on success
*/ */
@ -275,27 +283,28 @@ class dcTranslaterModule
{ {
files::uploadStatus($zip_file); files::uploadStatus($zip_file);
$imported = false; $imported = false;
$not_overwrited = []; $not_overwrited = [];
$res = []; $res = [];
# Load Unzip object # Load Unzip object
$zip = new fileUnzip($zip_file['tmp_name']); $zip = new fileUnzip($zip_file['tmp_name']);
$files = $zip->getFilesList(); $files = $zip->getFilesList();
foreach($files as $file) { foreach ($files as $file) {
$f = $this->parseZipFilename($file, true); $f = $this->parseZipFilename($file, true);
if (!$this->translater->import_overwrite if (!$this->translater->import_overwrite
&& file_exists($this->prop['locales'] . '/' . $f['lang'] . '/' . $f['group'] . $f['ext']) && file_exists($this->prop['locales'] . '/' . $f['lang'] . '/' . $f['group'] . $f['ext'])
) { ) {
$not_overwrited[] = implode('-', [$f['lang'], $f['group'], $f['ext']]); $not_overwrited[] = implode('-', [$f['lang'], $f['group'], $f['ext']]);
continue; continue;
} }
$res[] = [ $res[] = [
'from' => $file, 'from' => $file,
'root' => $this->prop['locales'] . '/' . $f['lang'], 'root' => $this->prop['locales'] . '/' . $f['lang'],
'to' => $this->prop['locales'] . '/' . $f['lang'] . '/' . $f['group'] . $f['ext'] 'to' => $this->prop['locales'] . '/' . $f['lang'] . '/' . $f['group'] . $f['ext']
]; ];
} }
@ -313,19 +322,22 @@ class dcTranslaterModule
if (!empty($not_overwrited)) { if (!empty($not_overwrited)) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Some languages has not been overwrited %s'), implode(', ', $not_overwrited) __('Some languages has not been overwrited %s'),
implode(', ', $not_overwrited)
)); ));
} elseif (!$done) { } elseif (!$done) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Nothing to import from %s'), $zip_file['name'] __('Nothing to import from %s'),
$zip_file['name']
)); ));
} }
return true; return true;
} }
/** /**
* Export (to output) language pack * Export (to output) language pack
* *
* @param array $langs Langs to export * @param array $langs Langs to export
*/ */
public function exportPack(array $langs) public function exportPack(array $langs)
@ -344,23 +356,21 @@ class dcTranslaterModule
} }
$res = []; $res = [];
foreach($langs AS $lang) { foreach ($langs as $lang) {
if (!is_dir($this->prop['locales'] . '/' . $lang)) { if (!is_dir($this->prop['locales'] . '/' . $lang)) {
continue; continue;
} }
$files = dcTranslater::scandir($this->prop['locales'] . '/' . $lang); $files = dcTranslater::scandir($this->prop['locales'] . '/' . $lang);
foreach($files as $file) { foreach ($files as $file) {
if (is_dir($this->prop['locales'] . '/' . $lang . '/' . $file)
if (is_dir($this->prop['locales'] . '/' . $lang . '/' . $file) || !dcTranslater::isLangphpFile($file)
|| !dcTranslater::isLangphpFile($file)
&& !dcTranslater::isPoFile($file) && !dcTranslater::isPoFile($file)
) { ) {
continue; continue;
} }
$res[$this->prop['locales'] . '/' . $lang . '/' . $file] = $res[$this->prop['locales'] . '/' . $lang . '/' . $file] = $this->prop['id'] . '/locales/' . $lang . '/' . $file;
$this->prop['id'] . '/locales/' . $lang . '/' . $file;
} }
} }
@ -371,9 +381,9 @@ class dcTranslaterModule
} }
@set_time_limit(300); @set_time_limit(300);
$fp = fopen('php://output', 'wb'); $fp = fopen('php://output', 'wb');
$zip = new fileZip($fp); $zip = new fileZip($fp);
foreach($res as $from => $to) { foreach ($res as $from => $to) {
$zip->addFile($from, $to); $zip->addFile($from, $to);
} }
@ -392,7 +402,7 @@ class dcTranslaterModule
/** /**
* Parse zip filename to module, lang info * Parse zip filename to module, lang info
* *
* @param string $file The zip filename * @param string $file The zip filename
* @param boolean $throw Silently failed * @param boolean $throw Silently failed
* @return mixed Array of file info * @return mixed Array of file info
@ -403,19 +413,22 @@ class dcTranslaterModule
if ($is_file) { if ($is_file) {
$module = $f[1] == $this->prop['id'] ?$f[1] : false; $module = $f[1] == $this->prop['id'] ?$f[1] : false;
$lang = l10n::isCode($f[2]) ? $f[2] : false; $lang = l10n::isCode($f[2]) ? $f[2] : false;
$group = in_array($f[3], dctranslater::$allowed_l10n_groups) ? $f[3] : false; $group = in_array($f[3], dctranslater::$allowed_l10n_groups) ? $f[3] : false;
$ext = dctranslater::isLangphpFile($f[4]) || dctranslater::isPoFile($f[4]) ? $f[4] : false; $ext = dctranslater::isLangphpFile($f[4]) || dctranslater::isPoFile($f[4]) ? $f[4] : false;
} }
if (!$is_file || !$module || !$lang || !$group || !$ext) { if (!$is_file || !$module || !$lang || !$group || !$ext) {
if ($throw) { if ($throw) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Zip file %s is not in translater format'), $file __('Zip file %s is not in translater format'),
$file
)); ));
} }
return []; return [];
} }
return [ return [
'module' => $module, 'module' => $module,
'lang' => $lang, 'lang' => $lang,
@ -429,7 +442,7 @@ class dcTranslaterModule
//@{ //@{
/** /**
* List available langs of a module * List available langs of a module
* *
* @param boolean $return_path Return path or name * @param boolean $return_path Return path or name
* @return array The lang list * @return array The lang list
*/ */
@ -440,7 +453,7 @@ class dcTranslaterModule
$prefix = preg_match('/(locales(.*))$/', $this->prop['locales']) ? 'locales' : ''; $prefix = preg_match('/(locales(.*))$/', $this->prop['locales']) ? 'locales' : '';
$files = dcTranslater::scandir($this->prop['locales']); $files = dcTranslater::scandir($this->prop['locales']);
foreach($files as $file) { foreach ($files as $file) {
if (!preg_match('/.*?locales\/([^\/]*?)\/([^\/]*?)(.lang.php|.po)$/', $prefix . $file, $m)) { if (!preg_match('/.*?locales\/([^\/]*?)\/([^\/]*?)(.lang.php|.po)$/', $prefix . $file, $m)) {
continue; continue;
} }
@ -455,12 +468,13 @@ class dcTranslaterModule
$res[$m[1]] = l10n::getLanguageName($m[1]); // Lang name $res[$m[1]] = l10n::getLanguageName($m[1]); // Lang name
} }
} }
return $res; return $res;
} }
/** /**
* List of used langs of a module * List of used langs of a module
* *
* @return array The list of iso names and codes * @return array The list of iso names and codes
*/ */
public function getUsedLangs() public function getUsedLangs()
@ -470,7 +484,7 @@ class dcTranslaterModule
/** /**
* List of unsused langs of a module * List of unsused langs of a module
* *
* @return array The list of iso names and codes * @return array The list of iso names and codes
*/ */
public function getUnusedLangs() public function getUnusedLangs()
@ -480,7 +494,7 @@ class dcTranslaterModule
/** /**
* Add a lang to a module * Add a lang to a module
* *
* @param string $lang The lang id * @param string $lang The lang id
* @param string $from_lang The lang to copy from * @param string $from_lang The lang to copy from
* @return boolean True on success * @return boolean True on success
@ -489,14 +503,16 @@ class dcTranslaterModule
{ {
if (!l10n::isCode($lang)) { if (!l10n::isCode($lang)) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Unknow language %s'), $lang __('Unknow language %s'),
$lang
)); ));
} }
$langs = $this->getLangs(); $langs = $this->getLangs();
if (isset($langs[$lang])) { if (isset($langs[$lang])) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Language %s already exists'), $lang __('Language %s already exists'),
$lang
)); ));
} }
@ -504,21 +520,23 @@ class dcTranslaterModule
if (!empty($from_lang) && !isset($langs[$from_lang])) { if (!empty($from_lang) && !isset($langs[$from_lang])) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Failed to copy file from language %s'), $from_lang __('Failed to copy file from language %s'),
$from_lang
)); ));
} }
if (!empty($from_lang) && isset($langs[$from_lang])) { if (!empty($from_lang) && isset($langs[$from_lang])) {
$files = dcTranslater::scandir($this->prop['locales'] . '/' . $from_lang); $files = dcTranslater::scandir($this->prop['locales'] . '/' . $from_lang);
foreach($files as $file) { foreach ($files as $file) {
if (is_dir($this->prop['locales'] . '/' . $from_lang . '/' . $file) if (is_dir($this->prop['locales'] . '/' . $from_lang . '/' . $file)
|| !dcTranslater::isLangphpFile($file) || !dcTranslater::isLangphpFile($file)
&& !dcTranslater::isPoFile($file) && !dcTranslater::isPoFile($file)
) { ) {
continue; continue;
} }
files::putContent($this->prop['locales'] . '/' . $lang . '/' . $file, files::putContent(
$this->prop['locales'] . '/' . $lang . '/' . $file,
file_get_contents($this->prop['locales'] . '/' . $from_lang . '/' . $file) file_get_contents($this->prop['locales'] . '/' . $from_lang . '/' . $file)
); );
} }
@ -530,7 +548,7 @@ class dcTranslaterModule
/** /**
* Update an existing lang * Update an existing lang
* *
* @param string $lang The lang * @param string $lang The lang
* @param array $msgs The messages * @param array $msgs The messages
*/ */
@ -538,14 +556,16 @@ class dcTranslaterModule
{ {
if (!l10n::isCode($lang)) { if (!l10n::isCode($lang)) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Unknow language %s'), $lang __('Unknow language %s'),
$lang
)); ));
} }
$langs = $this->getLangs(); $langs = $this->getLangs();
if (!isset($langs[$lang])) { if (!isset($langs[$lang])) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Failed to find language %s'), $lang __('Failed to find language %s'),
$lang
)); ));
} }
@ -554,19 +574,19 @@ class dcTranslaterModule
} }
$rs = []; $rs = [];
foreach($msgs as $msg) { foreach ($msgs as $msg) {
if (empty($msg['msgstr'][0])) { if (empty($msg['msgstr'][0])) {
continue; continue;
} }
$rs[$msg['group']][] = $msg; $rs[$msg['group']][] = $msg;
} }
foreach(dcTranslater::$allowed_l10n_groups as $group) { foreach (dcTranslater::$allowed_l10n_groups as $group) {
if (isset($rs[$group])) { if (isset($rs[$group])) {
continue; continue;
} }
$po_file = $this->prop['locales'] . '/' . $lang . '/' . $group . '.po'; $po_file = $this->prop['locales'] . '/' . $lang . '/' . $group . '.po';
$langphp_file = $this->prop['locales'] . '/' . $lang . '/' . $group . '.lang.php'; $langphp_file = $this->prop['locales'] . '/' . $lang . '/' . $group . '.lang.php';
if (file_exists($po_file)) { if (file_exists($po_file)) {
@ -579,11 +599,12 @@ class dcTranslaterModule
if (empty($rs)) { if (empty($rs)) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('No string to write, language %s deleted'), $lang __('No string to write, language %s deleted'),
$lang
)); ));
} }
foreach($rs as $group => $msgs) { foreach ($rs as $group => $msgs) {
$this->setPoContent($lang, $group, $msgs); $this->setPoContent($lang, $group, $msgs);
$this->setLangphpContent($lang, $group, $msgs); $this->setLangphpContent($lang, $group, $msgs);
} }
@ -591,7 +612,7 @@ class dcTranslaterModule
/** /**
* Delete a lang * Delete a lang
* *
* @param string $lang The lang code * @param string $lang The lang code
* @param boolean $del_empty_dir Also remove empty locales dir * @param boolean $del_empty_dir Also remove empty locales dir
* @return boolean True on success * @return boolean True on success
@ -601,18 +622,20 @@ class dcTranslaterModule
# Path is right formed # Path is right formed
if (!l10n::isCode($lang)) { if (!l10n::isCode($lang)) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Unknow language %s'), $lang __('Unknow language %s'),
$lang
)); ));
} }
$files = $this->getLangs(true); $files = $this->getLangs(true);
if (!isset($files[$lang])) { if (!isset($files[$lang])) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Failed to find language %s'), $lang __('Failed to find language %s'),
$lang
)); ));
} }
foreach($files[$lang] as $file) { foreach ($files[$lang] as $file) {
unlink($this->prop['locales'] . '/' . $file); unlink($this->prop['locales'] . '/' . $file);
} }
@ -625,12 +648,13 @@ class dcTranslaterModule
if (empty($loc)) { if (empty($loc)) {
rmdir($this->prop['locales']); rmdir($this->prop['locales']);
} }
return true; return true;
} }
/** /**
* Construct and parse a .po file * Construct and parse a .po file
* *
* @param string $lang The lang code * @param string $lang The lang code
* @param string $group The lang group * @param string $group The lang group
* @param array $msgs The strings * @param array $msgs The strings
@ -641,14 +665,13 @@ class dcTranslaterModule
$content = ''; $content = '';
if ($this->translater->parse_comment) { if ($this->translater->parse_comment) {
$content .= $content .= '# Language: ' . $lang->name . "\n" .
'# Language: ' . $lang->name . "\n" . '# Module: ' . $this->id . ' - ' . $this->version . "\n" .
'# Module: ' . $this->id . " - " . $this->version . "\n" .
'# Date: ' . dt::str('%Y-%m-%d %H:%M:%S') . "\n"; '# Date: ' . dt::str('%Y-%m-%d %H:%M:%S') . "\n";
if ($this->translater->parse_user && $this->translater->parse_userinfo != '') { if ($this->translater->parse_user && $this->translater->parse_userinfo != '') {
$search = dctranslater::$allowed_user_informations; $search = dctranslater::$allowed_user_informations;
foreach($search AS $n) { foreach ($search as $n) {
$replace[] = $this->core->auth->getInfo('user_' . $n); $replace[] = $this->core->auth->getInfo('user_' . $n);
} }
$info = trim(str_replace($search, $replace, $this->translater->parse_userinfo)); $info = trim(str_replace($search, $replace, $this->translater->parse_userinfo));
@ -656,11 +679,9 @@ class dcTranslaterModule
$content .= '# Author: ' . html::escapeHTML($info) . "\n"; $content .= '# Author: ' . html::escapeHTML($info) . "\n";
} }
} }
$content .= $content .= '# Translated with translater ' . $this->core->plugins->moduleInfo('translater', 'version') . "\n\n";
'# Translated with translater ' . $this->core->plugins->moduleInfo('translater', 'version') . "\n\n";
} }
$content .= $content .= "msgid \"\"\n" .
"msgid \"\"\n" .
"msgstr \"\"\n" . "msgstr \"\"\n" .
'"Content-Type: text/plain; charset=UTF-8\n"' . "\n" . '"Content-Type: text/plain; charset=UTF-8\n"' . "\n" .
'"Project-Id-Version: ' . $this->id . ' ' . $this->version . '\n"' . "\n" . '"Project-Id-Version: ' . $this->id . ' ' . $this->version . '\n"' . "\n" .
@ -675,14 +696,13 @@ class dcTranslaterModule
$comments = []; $comments = [];
if ($this->translater->parse_comment) { if ($this->translater->parse_comment) {
$msgids = $lang->getMsgids(); $msgids = $lang->getMsgids();
foreach($msgids as $msg) { foreach ($msgids as $msg) {
$comments[$msg['msgid']] = (isset($comments[$msg['msgid']]) ? $comments[$msg['msgid']] = ($comments[$msg['msgid']] ?? '') .
$comments[$msg['msgid']] : '') . '#: ' . trim($msg['file'], '/') . ':' . $msg['line'] . "\n";
'#: '.trim($msg['file'],'/') . ':' . $msg['line'] . "\n";
} }
} }
foreach($msgs as $msg) { foreach ($msgs as $msg) {
if (empty($msg['msgstr'][0])) { if (empty($msg['msgstr'][0])) {
continue; continue;
} }
@ -694,7 +714,7 @@ class dcTranslaterModule
$content .= 'msgstr "' . dcTranslater::poString($msg['msgstr'][0], true) . '"' . "\n"; $content .= 'msgstr "' . dcTranslater::poString($msg['msgstr'][0], true) . '"' . "\n";
} else { } else {
$content .= 'msgid_plural "' . dcTranslater::poString($msg['msgid_plural'], true) . '"' . "\n"; $content .= 'msgid_plural "' . dcTranslater::poString($msg['msgid_plural'], true) . '"' . "\n";
foreach($msg['msgstr'] as $i => $plural) { foreach ($msg['msgstr'] as $i => $plural) {
$content .= 'msgstr[' . $i . '] "' . dcTranslater::poString(($msg['msgstr'][$i] ?: ''), true) . '"' . "\n"; $content .= 'msgstr[' . $i . '] "' . dcTranslater::poString(($msg['msgstr'][$i] ?: ''), true) . '"' . "\n";
} }
} }
@ -703,23 +723,25 @@ class dcTranslaterModule
$file = $this->locales . '/' . $lang->code . '/' . $group . '.po'; $file = $this->locales . '/' . $lang->code . '/' . $group . '.po';
$path = path::info($file); $path = path::info($file);
if (is_dir($path['dirname']) && !is_writable($path['dirname']) if (is_dir($path['dirname']) && !is_writable($path['dirname'])
|| file_exists($file) && !is_writable($file)) { || file_exists($file) && !is_writable($file)) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Failed to grant write acces on file %s'), $file __('Failed to grant write acces on file %s'),
$file
)); ));
} }
if (!($f = @files::putContent($file, $content))) { if (!($f = @files::putContent($file, $content))) {
throw new Exception(sprintf( throw new Exception(sprintf(
__('Failed to write file %s'), $file __('Failed to write file %s'),
$file
)); ));
} }
} }
/** /**
* Construct and write a .lang.php file * Construct and write a .lang.php file
* *
* @param string $lang The lang code * @param string $lang The lang code
* @param string $group The lang group * @param string $group The lang group
* @param array $msgs The strings * @param array $msgs The strings
@ -734,26 +756,24 @@ class dcTranslaterModule
$content = ''; $content = '';
if ($this->translater->parse_comment) { if ($this->translater->parse_comment) {
$content .= $content .= '// Language: ' . $lang->name . "\n" .
'// Language: ' . $lang->name . "\n" . '// Module: ' . $this->id . ' - ' . $this->verison . "\n" .
'// Module: ' . $this->id . " - " . $this->verison . "\n" .
'// Date: ' . dt::str('%Y-%m-%d %H:%M:%S') . "\n"; '// Date: ' . dt::str('%Y-%m-%d %H:%M:%S') . "\n";
if ($this->translater->parse_user && !empty($this->translater->parse_userinfo)) { if ($this->translater->parse_user && !empty($this->translater->parse_userinfo)) {
$search = dcTranslater::$allowed_user_informations; $search = dcTranslater::$allowed_user_informations;
foreach($search as $n) { foreach ($search as $n) {
$replace[] = $this->core->auth->getInfo('user_' . $n); $replace[] = $this->core->auth->getInfo('user_' . $n);
} }
$info = trim(str_replace($search, $replace,$this->translater->parse_userinfo)); $info = trim(str_replace($search, $replace, $this->translater->parse_userinfo));
if (!empty($info)) { if (!empty($info)) {
$content .= '// Author: ' . html::escapeHTML($info) . "\n"; $content .= '// Author: ' . html::escapeHTML($info) . "\n";
} }
} }
$content .= $content .= '// Translated with dcTranslater - ' . $this->core->plugins->moduleInfo('translater', 'version') . "\n\n";
'// Translated with dcTranslater - ' . $this->core->plugins->moduleInfo('translater', 'version') . "\n\n";
} }
l10n::generatePhpFileFromPo($this->locales . '/' . $lang->code . '/' . $group, $content); l10n::generatePhpFileFromPo($this->locales . '/' . $lang->code . '/' . $group, $content);
} }
//@} //@}
} }

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return; return;
} }
@ -26,105 +25,105 @@ class dcTranslater
/** @var array $default_settings Plugins default settings */ /** @var array $default_settings Plugins default settings */
private static $default_settings = [ private static $default_settings = [
'plugin_menu' => [ 'plugin_menu' => [
'id' => 'translater_plugin_menu', 'id' => 'translater_plugin_menu',
'value' => 0, 'value' => 0,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Put an link in plugins page' 'label' => 'Put an link in plugins page'
], ],
'theme_menu' => [ 'theme_menu' => [
'id' => 'translater_theme_menu', 'id' => 'translater_theme_menu',
'value' => 0, 'value' => 0,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Put a link in themes page' 'label' => 'Put a link in themes page'
], ],
'backup_auto' => [ 'backup_auto' => [
'id' => 'translater_backup_auto', 'id' => 'translater_backup_auto',
'value' => 1, 'value' => 1,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Make a backup of languages old files when there are modified' 'label' => 'Make a backup of languages old files when there are modified'
], ],
'backup_limit' => [ 'backup_limit' => [
'id' => 'translater_backup_limit', 'id' => 'translater_backup_limit',
'value' => 20, 'value' => 20,
'type' => 'string', 'type' => 'string',
'label' => 'Maximum backups per module' 'label' => 'Maximum backups per module'
], ],
'backup_folder' => [ 'backup_folder' => [
'id' => 'translater_backup_folder', 'id' => 'translater_backup_folder',
'value' => 'module', 'value' => 'module',
'type' => 'string', 'type' => 'string',
'label' => 'In which folder to store backups' 'label' => 'In which folder to store backups'
], ],
'start_page' => [ 'start_page' => [
'id' => 'translater_start_page', 'id' => 'translater_start_page',
'value' => '-', 'value' => '-',
'type' => 'string', 'type' => 'string',
'label' => 'Page to start on' 'label' => 'Page to start on'
], ],
'write_langphp' => [ 'write_langphp' => [
'id' => 'translater_write_langphp', 'id' => 'translater_write_langphp',
'value' => 0, 'value' => 0,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Write .lang.php languages files' 'label' => 'Write .lang.php languages files'
], ],
'scan_tpl' => [ 'scan_tpl' => [
'id' => 'translater_scan_tpl', 'id' => 'translater_scan_tpl',
'value' => 1, 'value' => 1,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Translate strings of templates files' 'label' => 'Translate strings of templates files'
], ],
'parse_nodc' => [ 'parse_nodc' => [
'id' => 'translater_parse_nodc', 'id' => 'translater_parse_nodc',
'value' => 1, 'value' => 1,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Translate only untranslated strings of Dotclear', 'label' => 'Translate only untranslated strings of Dotclear',
], ],
'hide_default' => [ 'hide_default' => [
'id' => 'translater_hide_default', 'id' => 'translater_hide_default',
'value' => 1, 'value' => 1,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Hide default modules of Dotclear', 'label' => 'Hide default modules of Dotclear',
], ],
'parse_comment' => [ 'parse_comment' => [
'id' => 'translater_parse_comment', 'id' => 'translater_parse_comment',
'value' => 0, 'value' => 0,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Write comments and strings informations in lang files' 'label' => 'Write comments and strings informations in lang files'
], ],
'parse_user' => [ 'parse_user' => [
'id' => 'translater_parse_user', 'id' => 'translater_parse_user',
'value' => 0, 'value' => 0,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Write inforamtions about author in lang files' 'label' => 'Write inforamtions about author in lang files'
], ],
'parse_userinfo' => [ 'parse_userinfo' => [
'id' => 'translater_parse_userinfo', 'id' => 'translater_parse_userinfo',
'value' => 'displayname, email', 'value' => 'displayname, email',
'type' => 'string', 'type' => 'string',
'label' => 'Type of informations about user to write' 'label' => 'Type of informations about user to write'
], ],
'import_overwrite' => [ 'import_overwrite' => [
'id' => 'translater_import_overwrite', 'id' => 'translater_import_overwrite',
'value' => 0, 'value' => 0,
'type' => 'boolean', 'type' => 'boolean',
'label' => 'Overwrite existing languages when import packages' 'label' => 'Overwrite existing languages when import packages'
], ],
'export_filename' => [ 'export_filename' => [
'id' => 'translater_export_filename', 'id' => 'translater_export_filename',
'value' => 'type-module-l10n-timestamp', 'value' => 'type-module-l10n-timestamp',
'type' => 'string', 'type' => 'string',
'label' => 'Name of files of exported package' 'label' => 'Name of files of exported package'
], ],
'proposal_tool' => [ 'proposal_tool' => [
'id' => 'translater_proposal_tool', 'id' => 'translater_proposal_tool',
'value' => 'google', 'value' => 'google',
'type' => 'string', 'type' => 'string',
'label' => 'Id of default tool for proposed translation' 'label' => 'Id of default tool for proposed translation'
], ],
'proposal_lang' => [ 'proposal_lang' => [
'id' => 'translater_proposal_lang', 'id' => 'translater_proposal_lang',
'value' => 'en', 'value' => 'en',
'type' => 'string', 'type' => 'string',
'label' => 'Default source language for proposed translation' 'label' => 'Default source language for proposed translation'
] ]
]; ];
@ -150,7 +149,7 @@ class dcTranslater
/** /**
* translater instance * translater instance
* *
* @param dcCore $core dcCore instance * @param dcCore $core dcCore instance
* @param boolean $core Also load modules * @param boolean $core Also load modules
*/ */
@ -171,7 +170,7 @@ class dcTranslater
__('locales folder of translater') => 'translater' __('locales folder of translater') => 'translater'
]; ];
self::$default_distrib_modules = [ self::$default_distrib_modules = [
'plugin' => explode(',', DC_DISTRIB_PLUGINS), 'plugin' => explode(',', DC_DISTRIB_PLUGINS),
'theme' => explode(',', DC_DISTRIB_THEMES) 'theme' => explode(',', DC_DISTRIB_THEMES)
]; ];
} }
@ -180,7 +179,7 @@ class dcTranslater
//@{ //@{
/** /**
* Get array of default settings * Get array of default settings
* *
* @return array All default settings * @return array All default settings
*/ */
public function getDefaultSettings(): array public function getDefaultSettings(): array
@ -190,13 +189,13 @@ class dcTranslater
/** /**
* Get a setting according to default settings list * Get a setting according to default settings list
* *
* @param string $id The settings short id * @param string $id The settings short id
* @return mixed The setting value if exists or null * @return mixed The setting value if exists or null
*/ */
public function getSetting(string $id) public function getSetting(string $id)
{ {
return array_key_exists($id, self::$default_settings) ? return array_key_exists($id, self::$default_settings) ?
$this->core->blog->settings->translater->get(self::$default_settings[$id]['id']) : ''; $this->core->blog->settings->translater->get(self::$default_settings[$id]['id']) : '';
} }
@ -210,7 +209,7 @@ class dcTranslater
/** /**
* Set a setting according to default settings list * Set a setting according to default settings list
* *
* @param string $id The setting short id * @param string $id The setting short id
* @param mixed $value The setting value * @param mixed $value The setting value
* @param mixed $overwrite Overwrite settings if exists * @param mixed $overwrite Overwrite settings if exists
@ -224,6 +223,7 @@ class dcTranslater
$s = self::$default_settings[$id]; $s = self::$default_settings[$id];
$this->core->blog->settings->translater->drop($s['id']); $this->core->blog->settings->translater->drop($s['id']);
$this->core->blog->settings->translater->put($s['id'], $value, $s['type'], $s['label'], $overwrite, true); $this->core->blog->settings->translater->put($s['id'], $value, $s['type'], $s['label'], $overwrite, true);
return true; return true;
} }
@ -238,7 +238,7 @@ class dcTranslater
/// @name modules methods /// @name modules methods
//@{ //@{
/** /**
* Load array of modules infos by type of modules * Load array of modules infos by type of modules
*/ */
private function loadModules() private function loadModules()
@ -252,34 +252,34 @@ class dcTranslater
'theme' => $themes->getModules(), 'theme' => $themes->getModules(),
'plugin' => $this->core->plugins->getModules() 'plugin' => $this->core->plugins->getModules()
]; ];
foreach($list as $type => $modules) { foreach ($list as $type => $modules) {
foreach($modules as $id => $info) { foreach ($modules as $id => $info) {
if (!$info['root_writable']) { if (!$info['root_writable']) {
// continue; // continue;
} }
$info['id'] = $id; $info['id'] = $id;
$info['type'] = $type; $info['type'] = $type;
$this->modules[$type][$id] = new dcTranslaterModule($this, $info); $this->modules[$type][$id] = new dcTranslaterModule($this, $info);
} }
} }
} }
/** /**
* Return array of modules infos by type of modules * Return array of modules infos by type of modules
* *
* @param string $type The modules type * @param string $type The modules type
* @return array The list of modules infos * @return array The list of modules infos
*/ */
public function getModules(string $type = ''): array public function getModules(string $type = ''): array
{ {
return in_array($type, ['plugin', 'theme']) ? return in_array($type, ['plugin', 'theme']) ?
$this->modules[$type] : $this->modules[$type] :
array_merge($this->modules['theme'], $this->modules['plugin']); array_merge($this->modules['theme'], $this->modules['plugin']);
} }
/** /**
* Return module class of a particular module for a given type of module * Return module class of a particular module for a given type of module
* *
* @param string $type The module type * @param string $type The module type
* @param string $id The module id * @param string $id The module id
* @return dcTranslaterModule The dcTranslaterModule instance * @return dcTranslaterModule The dcTranslaterModule instance
@ -290,14 +290,16 @@ class dcTranslater
throw new Exception( throw new Exception(
sprintf(__('Failed to find module %s'), $id) sprintf(__('Failed to find module %s'), $id)
); );
return false; return false;
} }
return $this->modules[$type][$id]; return $this->modules[$type][$id];
} }
/** /**
* Return module class of a particular module for a given type of module * Return module class of a particular module for a given type of module
* *
* @param string $module dcTranslaterModule instance * @param string $module dcTranslaterModule instance
* @param string $lang The lang iso code * @param string $lang The lang iso code
* @return dcTranslaterLang dcTranslaterLang instance or false * @return dcTranslaterLang dcTranslaterLang instance or false
@ -308,17 +310,19 @@ class dcTranslater
throw new Exception( throw new Exception(
sprintf(__('Failed find language %s'), $lang) sprintf(__('Failed find language %s'), $lang)
); );
return false; return false;
} }
return new dcTranslaterLang($module, $lang); return new dcTranslaterLang($module, $lang);
} }
//@} //@}
/// @name helper methods /// @name helper methods
//@{ //@{
/** /**
* Scan recursively a folder and return files and folders names * Scan recursively a folder and return files and folders names
* *
* @param string $path The path to scan * @param string $path The path to scan
* @param string $dir Internal recursion * @param string $dir Internal recursion
* @param array $res Internal recursion * @param array $res Internal recursion
@ -332,24 +336,25 @@ class dcTranslater
} }
$files = files::scandir($path); $files = files::scandir($path);
foreach($files AS $file) { foreach ($files as $file) {
if (in_array($file, ['.', '..'])) { if (in_array($file, ['.', '..'])) {
continue; continue;
} }
if (is_dir($path . '/' . $file)) { if (is_dir($path . '/' . $file)) {
$res[] = $file; $res[] = $file;
$res = self::scanDir($path . '/' . $file, $dir . '/' . $file, $res); $res = self::scanDir($path . '/' . $file, $dir . '/' . $file, $res);
} else { } else {
$res[] = empty($dir) ? $file : $dir . '/' . $file; $res[] = empty($dir) ? $file : $dir . '/' . $file;
} }
} }
return $res; return $res;
} }
/** /**
* Encode a string * Encode a string
* *
* @param string $str The string to encode * @param string $str The string to encode
* @return string The encoded string * @return string The encoded string
*/ */
@ -360,7 +365,7 @@ class dcTranslater
/** /**
* Clean a po string * Clean a po string
* *
* @param string $string The string to clean * @param string $string The string to clean
* @param boolean $reverse Un/escape string * @param boolean $reverse Un/escape string
* @return string The cleaned string * @return string The cleaned string
@ -368,19 +373,20 @@ class dcTranslater
public static function poString(string $string, bool $reverse = false): string public static function poString(string $string, bool $reverse = false): string
{ {
if ($reverse) { if ($reverse) {
$smap = array('"', "\n", "\t", "\r"); $smap = ['"', "\n", "\t", "\r"];
$rmap = array('\\"', '\\n"' . "\n" . '"', '\\t', '\\r'); $rmap = ['\\"', '\\n"' . "\n" . '"', '\\t', '\\r'];
return trim((string) str_replace($smap, $rmap, $string)); 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));
} }
$smap = ['/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\"/'];
$rmap = ['', "\n", "\r", "\t", '"'];
return trim((string) preg_replace($smap, $rmap, $string));
} }
/** /**
* Try if a file is a .po file * Try if a file is a .po file
* *
* @param string $file The path to test * @param string $file The path to test
* @return boolean Success * @return boolean Success
*/ */
@ -391,7 +397,7 @@ class dcTranslater
/** /**
* Try if a file is a .lang.php file * Try if a file is a .lang.php file
* *
* @param string $file The path to test * @param string $file The path to test
* @return boolean Success * @return boolean Success
*/ */
@ -402,7 +408,7 @@ class dcTranslater
/** /**
* Check limit number of backup for a module * Check limit number of backup for a module
* *
* @param string $root The backups root * @param string $root The backups root
* @param string $limit The backups limit * @param string $limit The backups limit
* @param boolean $throw Silently failed * @param boolean $throw Silently failed
@ -411,9 +417,9 @@ class dcTranslater
public static function isBackupLimit(string $root, int $limit = 10, bool $throw = false): bool public static function isBackupLimit(string $root, int $limit = 10, bool $throw = false): bool
{ {
$count = 0; $count = 0;
foreach(self::scandir($root) AS $file) { foreach (self::scandir($root) as $file) {
if (!is_dir($root . '/' . $file) if (!is_dir($root . '/' . $file)
&& preg_match('/^(l10n-'. $id . '(.*?).bck.zip)$/', $root) && preg_match('/^(l10n-' . $id . '(.*?).bck.zip)$/', $root)
) { ) {
$count++; $count++;
} }
@ -426,8 +432,10 @@ class dcTranslater
sprintf(__('Limit of %s backups for module %s exceed'), $this->backup_limit, $id) sprintf(__('Limit of %s backups for module %s exceed'), $this->backup_limit, $id)
); );
} }
return true; return true;
} }
return false; return false;
} }
@ -435,7 +443,7 @@ class dcTranslater
* Extract messages from a php contents * Extract messages from a php contents
* *
* support plurals * support plurals
* *
* @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
@ -445,12 +453,12 @@ class dcTranslater
$duplicate = $final_strings = $lines = []; $duplicate = $final_strings = $lines = [];
// split content by line to combine match/line on the end // split content by line to combine match/line on the end
$content = str_replace("\r\n", "\n", $content); $content = str_replace("\r\n", "\n", $content);
$o = 0; $o = 0;
$parts = explode("\n", $content); $parts = explode("\n", $content);
foreach($parts as $li => $part) { foreach ($parts as $li => $part) {
$m = explode($func . '(', $part); $m = explode($func . '(', $part);
for($i = 1; $i < count($m); $i++) { for ($i = 1; $i < count($m); $i++) {
$lines[$o] = $li+1; $lines[$o] = $li + 1;
$o++; $o++;
} }
} }
@ -460,14 +468,15 @@ class dcTranslater
array_shift($parts); array_shift($parts);
// walk through parts // walk through parts
$p = 0; $p = 0;
foreach($parts as $part) { foreach ($parts as $part) {
// should start with quote // should start with quote
if (!in_array(substr($part,0,1), ['"', "'"])) { if (!in_array(substr($part, 0, 1), ['"', "'"])) {
$p++; $p++;
continue; continue;
} }
// put back first parenthesis // put back first parenthesis
$part = '('.$part; $part = '(' . $part;
// find pairs of parenthesis // find pairs of parenthesis
preg_match_all("/\((?:[^\)\(]+|(?R))*+\)/s", $part, $subparts); preg_match_all("/\((?:[^\)\(]+|(?R))*+\)/s", $part, $subparts);
// find quoted strings (single or double) // find quoted strings (single or double)
@ -475,16 +484,17 @@ class dcTranslater
// strings exist // strings exist
if (!empty($strings[0])) { if (!empty($strings[0])) {
// remove quotes // remove quotes
$strings[0] = array_map(function($v){ return substr($v, 1, -1);}, $strings[0]); $strings[0] = array_map(function ($v) { return substr($v, 1, -1);}, $strings[0]);
// filter duplicate strings (only check first string for plurals form) // filter duplicate strings (only check first string for plurals form)
if (!in_array($strings[0][0], $duplicate)) { if (!in_array($strings[0][0], $duplicate)) {
// fill final array // fill final array
$final_strings[] = [$strings[0], $lines[$p]]; $final_strings[] = [$strings[0], $lines[$p]];
$duplicate[] = $strings[0][0]; $duplicate[] = $strings[0][0];
} }
} }
$p++; $p++;
} }
return $final_strings; return $final_strings;
} }
@ -500,12 +510,12 @@ class dcTranslater
$duplicate = $final_strings = $lines = []; $duplicate = $final_strings = $lines = [];
// split content by line to combine match/line on the end // split content by line to combine match/line on the end
$content = str_replace("\r\n", "\n", $content); $content = str_replace("\r\n", "\n", $content);
$o = 0; $o = 0;
$parts = explode("\n", $content); $parts = explode("\n", $content);
foreach($parts as $li => $part) { foreach ($parts as $li => $part) {
$m = explode('{{' . $func . ' ', $part); $m = explode('{{' . $func . ' ', $part);
for($i = 1; $i < count($m); $i++) { for ($i = 1; $i < count($m); $i++) {
$lines[$o] = $li+1; $lines[$o] = $li + 1;
$o++; $o++;
} }
} }
@ -515,19 +525,20 @@ class dcTranslater
} }
// walk through parts // walk through parts
$p = 0; $p = 0;
foreach($parts[1] as $part) { foreach ($parts[1] as $part) {
// strings exist // strings exist
if (!empty($part)) { if (!empty($part)) {
// filter duplicate strings // filter duplicate strings
if (!in_array($part, $duplicate)) { if (!in_array($part, $duplicate)) {
// fill final array // fill final array
$final_strings[] = [[$part], $lines[$p]]; $final_strings[] = [[$part], $lines[$p]];
$duplicate[] = $part; $duplicate[] = $part;
} }
} }
$p++; $p++;
} }
return $final_strings; return $final_strings;
} }
//@} //@}
} }

View File

@ -1,12 +1,12 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
@ -18,7 +18,7 @@ class translaterProposals
{ {
public $core; public $core;
private $stack = array(); private $stack = [];
public function __construct($core) public function __construct($core)
{ {
@ -58,4 +58,4 @@ class translaterProposals
{ {
return array_key_exists($id, $this->stack); return array_key_exists($id, $this->stack);
} }
} }

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return; return;
} }
@ -25,13 +24,13 @@ class translaterRest
{ {
public static function getProposal($core, $get) public static function getProposal($core, $get)
{ {
$from = !empty($get['langFrom']) ? trim($get['langFrom']) : ''; $from = !empty($get['langFrom']) ? trim($get['langFrom']) : '';
$to = !empty($get['langTo']) ? trim($get['langTo']) : ''; $to = !empty($get['langTo']) ? trim($get['langTo']) : '';
$tool = !empty($get['langTool']) ? trim($get['langTool']) : ''; $tool = !empty($get['langTool']) ? trim($get['langTool']) : '';
$str_in = !empty($get['langStr']) ? trim($get['langStr']) : ''; $str_in = !empty($get['langStr']) ? trim($get['langStr']) : '';
$str_in = text::toUTF8($str_in); $str_in = text::toUTF8($str_in);
$str_in = trim($str_in); $str_in = trim($str_in);
$str_out = ''; $str_out = '';
$rsp = new xmlTag(); $rsp = new xmlTag();
@ -51,19 +50,20 @@ class translaterRest
throw new Exception(__('Translation tool is not configured')); throw new Exception(__('Translation tool is not configured'));
} }
$str_out = (string) $translater->proposal->getTool($tool)->translate($str_in ,$from, $to); $str_out = (string) $translater->proposal->getTool($tool)->translate($str_in, $from, $to);
} }
$x = new xmlTag('proposal'); $x = new xmlTag('proposal');
$x->lang_from = $from; $x->lang_from = $from;
$x->lang_to = $to; $x->lang_to = $to;
$x->tool = $tool; $x->tool = $tool;
$x->str_from = $str_in; $x->str_from = $str_in;
$x->str_to = text::toUTF8(html::decodeEntities($str_out)); $x->str_to = text::toUTF8(html::decodeEntities($str_out));
$rsp->insertNode($x); $rsp->insertNode($x);
} catch (Exception $e) { } catch (Exception $e) {
$core->error->add($e->getMessage()); $core->error->add($e->getMessage());
} }
return $rsp; return $rsp;
} }
} }

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return; return;
} }
@ -22,9 +21,9 @@ if (!defined('DC_CONTEXT_ADMIN')) {
*/ */
class googleProposalTool extends translaterProposalTool class googleProposalTool extends translaterProposalTool
{ {
private $api = 'https://www.googleapis.com/language/translate/v2'; private $api = 'https://www.googleapis.com/language/translate/v2';
private $agent = 'dcTranslater - http://jcd.lv/?q=translater'; private $agent = 'dcTranslater - http://jcd.lv/?q=translater';
private $key = null; //ex: AsSDqsGsfdSDSQFQsfedj9bnzY390aIg-1d private $key = null; //ex: AsSDqsGsfdSDSQFQsfedj9bnzY390aIg-1d
protected function setup() protected function setup()
{ {
@ -41,7 +40,7 @@ class googleProposalTool extends translaterProposalTool
'<p><label class="classic" for="translater_google_proposal_key">' . '<p><label class="classic" for="translater_google_proposal_key">' .
__('API Console Single Access Key') . '<br />' . __('API Console Single Access Key') . '<br />' .
form::field('translater_google_proposal_key', 65, 255, $this->key) . form::field('translater_google_proposal_key', 65, 255, $this->key) .
'</label></p>'. '</label></p>' .
'<p>' . __('You must have on Google API console:') . '</p>' . '<p>' . __('You must have on Google API console:') . '</p>' .
'<ul>' . '<ul>' .
'<li><a href="https://code.google.com/apis/console/#access">' . __('A single access API key') . '</a></li>' . '<li><a href="https://code.google.com/apis/console/#access">' . __('A single access API key') . '</a></li>' .
@ -51,7 +50,7 @@ class googleProposalTool extends translaterProposalTool
public function save() public function save()
{ {
$key = empty($_POST['translater_google_proposal_key']) ? $key = empty($_POST['translater_google_proposal_key']) ?
'' : $_POST['translater_google_proposal_key']; '' : $_POST['translater_google_proposal_key'];
$this->core->blog->settings->translater->put('translater_google_proposal_key', $key, 'string', '', true, true); $this->core->blog->settings->translater->put('translater_google_proposal_key', $key, 'string', '', true, true);
@ -60,19 +59,19 @@ class googleProposalTool extends translaterProposalTool
public function translate($str, $from, $to) public function translate($str, $from, $to)
{ {
try { try {
$data = array( $data = [
'key' => $this->key, 'key' => $this->key,
'q' => $str, 'q' => $str,
'source' => $from, 'source' => $from,
'target' => $to 'target' => $to
); ];
$path = ''; $path = '';
$client = netHttp::initClient($this->api, $path); $client = netHttp::initClient($this->api, $path);
$client->setUserAgent($this->agent); $client->setUserAgent($this->agent);
$client->useGzip(false); $client->useGzip(false);
$client->setPersistReferers(false); $client->setPersistReferers(false);
$client->get($path,$data); $client->get($path, $data);
$rs = $client->getContent(); $rs = $client->getContent();
@ -89,8 +88,9 @@ class googleProposalTool extends translaterProposalTool
} }
return $dec->data->translations[0]->translatedText; return $dec->data->translations[0]->translatedText;
} catch (Exception $e) {
} }
catch (Exception $e) {}
return ''; return '';
} }
} }

View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return; return;
} }
@ -39,27 +38,27 @@ class microsoftProposalTool extends translaterProposalTool
public function form() public function form()
{ {
return return
'<p><label class="classic" for="translater_microsoft_proposal_client">' . '<p><label class="classic" for="translater_microsoft_proposal_client">' .
__('Application client ID') . '<br />' . __('Application client ID') . '<br />' .
form::field('translater_microsoft_proposal_client', 65, 255, $this->client) . form::field('translater_microsoft_proposal_client', 65, 255, $this->client) .
'</label></p>' . '</label></p>' .
'<p><label class="classic" for="translater_microsoft_proposal_secret">' . '<p><label class="classic" for="translater_microsoft_proposal_secret">' .
__('Application client Secret') . '<br />' . __('Application client Secret') . '<br />' .
form::field('translater_microsoft_proposal_secret', 65, 255, $this->secret) . form::field('translater_microsoft_proposal_secret', 65, 255, $this->secret) .
'</label></p>' . '</label></p>' .
'<p>' . __('You must have:') . '</p>' . '<p>' . __('You must have:') . '</p>' .
'<ul>' . '<ul>' .
'<li><a href="https://datamarket.azure.com/account">' . __('A Microsoft Windows Azure account') . '</a></li>' . '<li><a href="https://datamarket.azure.com/account">' . __('A Microsoft Windows Azure account') . '</a></li>' .
'<li><a href="https://datamarket.azure.com/dataset/bing/microsofttranslator">' . __('A valid subscription to Microsoft Translator') . '</a></li>' . '<li><a href="https://datamarket.azure.com/dataset/bing/microsofttranslator">' . __('A valid subscription to Microsoft Translator') . '</a></li>' .
'<li><a href="https://datamarket.azure.com/developer/applications/">' . __('And register an application') . '</a></li>' . '<li><a href="https://datamarket.azure.com/developer/applications/">' . __('And register an application') . '</a></li>' .
'</ul>'; '</ul>';
} }
public function save() public function save()
{ {
$client = empty($_POST['translater_microsoft_proposal_client']) ? $client = empty($_POST['translater_microsoft_proposal_client']) ?
'' : $_POST['translater_microsoft_proposal_client']; '' : $_POST['translater_microsoft_proposal_client'];
$secret = empty($_POST['translater_microsoft_proposal_secret']) ? $secret = empty($_POST['translater_microsoft_proposal_secret']) ?
'' : $_POST['translater_microsoft_proposal_secret']; '' : $_POST['translater_microsoft_proposal_secret'];
$this->core->blog->settings->translater->put('translater_microsoft_proposal_client', $client, 'string', '', true, true); $this->core->blog->settings->translater->put('translater_microsoft_proposal_client', $client, 'string', '', true, true);
@ -70,7 +69,8 @@ class microsoftProposalTool extends translaterProposalTool
{ {
try { try {
return $this->doYourFuckingJob($this->client, $this->secret, $str, $from, $to); return $this->doYourFuckingJob($this->client, $this->secret, $str, $from, $to);
} catch (Exception $e) {} } catch (Exception $e) {
}
return ''; return '';
} }
@ -83,20 +83,20 @@ class microsoftProposalTool extends translaterProposalTool
{ {
try { try {
//Client ID of the application. //Client ID of the application.
$clientID = $client; $clientID = $client;
//Client Secret key of the application. //Client Secret key of the application.
$clientSecret = $secret; $clientSecret = $secret;
//OAuth Url. //OAuth Url.
$authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; $authUrl = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/';
//Application Scope Url //Application Scope Url
$scopeUrl = "http://api.microsofttranslator.com"; $scopeUrl = 'http://api.microsofttranslator.com';
//Application grant type //Application grant type
$grantType = "client_credentials"; $grantType = 'client_credentials';
//Get the Access token. //Get the Access token.
$accessToken = $this->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); $accessToken = $this->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl);
//Create the authorization Header string. //Create the authorization Header string.
$authHeader = "Authorization: Bearer " . $accessToken; $authHeader = 'Authorization: Bearer ' . $accessToken;
//Set the params.// //Set the params.//
$fromLanguage = $from; $fromLanguage = $from;
@ -105,7 +105,7 @@ class microsoftProposalTool extends translaterProposalTool
$contentType = 'text/plain'; $contentType = 'text/plain';
$category = 'general'; $category = 'general';
$params = "text=" . urlencode($inputStr) . "&to=" . $toLanguage . "&from=" . $fromLanguage; $params = 'text=' . urlencode($inputStr) . '&to=' . $toLanguage . '&from=' . $fromLanguage;
$translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?$params"; $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?$params";
//Get the curlResponse. //Get the curlResponse.
@ -113,8 +113,8 @@ class microsoftProposalTool extends translaterProposalTool
//Interprets a string of XML into an object. //Interprets a string of XML into an object.
$xmlObj = simplexml_load_string($curlResponse); $xmlObj = simplexml_load_string($curlResponse);
foreach((array)$xmlObj[0] as $val){ foreach ((array) $xmlObj[0] as $val) {
$translatedStr = $val; $translatedStr = $val;
} }
return (string) $translatedStr; return (string) $translatedStr;
@ -148,40 +148,42 @@ class microsoftProposalTool extends translaterProposalTool
//Initialize the Curl Session. //Initialize the Curl Session.
$ch = curl_init(); $ch = curl_init();
//Create the request Array. //Create the request Array.
$paramArr = array ( $paramArr = [
'grant_type' => $grantType, 'grant_type' => $grantType,
'scope' => $scopeUrl, 'scope' => $scopeUrl,
'client_id' => $clientID, 'client_id' => $clientID,
'client_secret' => $clientSecret 'client_secret' => $clientSecret
); ];
//Create an Http Query.// //Create an Http Query.//
$paramArr = http_build_query($paramArr); $paramArr = http_build_query($paramArr);
//Set the Curl URL. //Set the Curl URL.
curl_setopt($ch, CURLOPT_URL, $authUrl); curl_setopt($ch, CURLOPT_URL, $authUrl);
//Set HTTP POST Request. //Set HTTP POST Request.
curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POST, true);
//Set data to POST in HTTP "POST" Operation. //Set data to POST in HTTP "POST" Operation.
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArr); curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArr);
//CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec(). //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec().
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate. //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Execute the cURL session. //Execute the cURL session.
$strResponse = curl_exec($ch); $strResponse = curl_exec($ch);
//Get the Error Code returned by Curl. //Get the Error Code returned by Curl.
$curlErrno = curl_errno($ch); $curlErrno = curl_errno($ch);
if($curlErrno) { if ($curlErrno) {
$curlError = curl_error($ch); $curlError = curl_error($ch);
curl_close($ch); curl_close($ch);
throw new Exception($curlError); throw new Exception($curlError);
} }
//Close the Curl Session. //Close the Curl Session.
curl_close($ch); curl_close($ch);
//Decode the returned JSON string. //Decode the returned JSON string.
$objResponse = json_decode($strResponse); $objResponse = json_decode($strResponse);
if (@$objResponse->error){ if (@$objResponse->error) {
throw new Exception($objResponse->error_description); throw new Exception($objResponse->error_description);
} }
return $objResponse->access_token; return $objResponse->access_token;
} catch (Exception $e) { } catch (Exception $e) {
throw $e; throw $e;
@ -198,17 +200,18 @@ class microsoftProposalTool extends translaterProposalTool
* @return string. * @return string.
* *
*/ */
private function curlRequest($url, $authHeader) { private function curlRequest($url, $authHeader)
{
//Initialize the Curl Session. //Initialize the Curl Session.
$ch = curl_init(); $ch = curl_init();
//Set the Curl url. //Set the Curl url.
curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
//Set the HTTP HEADER Fields. //Set the HTTP HEADER Fields.
curl_setopt ($ch, CURLOPT_HTTPHEADER, array($authHeader,"Content-Type: text/xml")); curl_setopt($ch, CURLOPT_HTTPHEADER, [$authHeader,'Content-Type: text/xml']);
//CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec(). //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec().
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate. //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate.
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Execute the cURL session. //Execute the cURL session.
$curlResponse = curl_exec($ch); $curlResponse = curl_exec($ch);
//Get the Error Code returned by Curl. //Get the Error Code returned by Curl.
@ -216,10 +219,12 @@ class microsoftProposalTool extends translaterProposalTool
if ($curlErrno) { if ($curlErrno) {
$curlError = curl_error($ch); $curlError = curl_error($ch);
curl_close($ch); curl_close($ch);
throw new Exception($curlError); throw new Exception($curlError);
} }
//Close a cURL session. //Close a cURL session.
curl_close($ch); curl_close($ch);
return $curlResponse; return $curlResponse;
} }
} }

View File

@ -1,12 +1,12 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
@ -21,12 +21,12 @@ abstract class translaterProposalTool
public $core; public $core;
private $active = false; private $active = false;
private $name = 'unknow'; private $name = 'unknow';
private $desc = 'no description'; private $desc = 'no description';
/** /**
Constructor Constructor
*/ */
final public function __construct($core) final public function __construct($core)
{ {
$this->core = $core; $this->core = $core;
@ -37,7 +37,7 @@ abstract class translaterProposalTool
Set name of this tool Set name of this tool
@param string Tool's name @param string Tool's name
*/ */
final protected function setName($name) final protected function setName($name)
{ {
$this->name = (string) $name; $this->name = (string) $name;
@ -47,7 +47,7 @@ abstract class translaterProposalTool
Get name of this tool Get name of this tool
@return string Tool's name @return string Tool's name
*/ */
final public function getName() final public function getName()
{ {
return $this->name; return $this->name;
@ -57,7 +57,7 @@ abstract class translaterProposalTool
Set description of this tool Set description of this tool
@param string Tool's description @param string Tool's description
*/ */
final protected function setDesc($desc) final protected function setDesc($desc)
{ {
$this->desc = (string) $desc; $this->desc = (string) $desc;
@ -67,7 +67,7 @@ abstract class translaterProposalTool
Get description of this tool Get description of this tool
@return string Tool's description @return string Tool's description
*/ */
final public function getDesc() final public function getDesc()
{ {
return $this->desc; return $this->desc;
@ -77,7 +77,7 @@ abstract class translaterProposalTool
Set tool as (un)active Set tool as (un)active
@param boolean $active True to set it as active @param boolean $active True to set it as active
*/ */
final protected function setActive($active) final protected function setActive($active)
{ {
$this->active = (boolean) $active; $this->active = (boolean) $active;
@ -87,7 +87,7 @@ abstract class translaterProposalTool
Check if this tool is active Check if this tool is active
@return boolean True if it is active @return boolean True if it is active
*/ */
final public function isActive() final public function isActive()
{ {
return $this->active; return $this->active;
@ -95,19 +95,19 @@ abstract class translaterProposalTool
/** /**
Set tool's info - using setName(),setDesc(),setActive() Set tool's info - using setName(),setDesc(),setActive()
*/ */
abstract protected function setup(); abstract protected function setup();
/** /**
Get configuration interface Get configuration interface
@return Form field @return Form field
*/ */
abstract public function form(); abstract public function form();
/** /**
Save configuration Save configuration
*/ */
abstract public function save(); abstract public function save();
/** /**
@ -117,6 +117,6 @@ abstract class translaterProposalTool
@param string $from Source language code @param string $from Source language code
@param string to Destination language code @param string to Destination language code
@return Translated string @return Translated string
*/ */
abstract public function translate($str,$from,$to); abstract public function translate($str, $from, $to);
} }

278
index.php
View File

@ -1,16 +1,15 @@
<?php <?php
/** /**
* @brief translater, a plugin for Dotclear 2 * @brief translater, a plugin for Dotclear 2
* *
* @package Dotclear * @package Dotclear
* @subpackage Plugin * @subpackage Plugin
* *
* @author Jean-Christian Denis & contributors * @author Jean-Christian Denis & contributors
* *
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { if (!defined('DC_CONTEXT_ADMIN')) {
return; return;
} }
@ -19,10 +18,10 @@ dcPage::checkSuper();
$translater = new dcTranslater($core); $translater = new dcTranslater($core);
$type = $_REQUEST['type'] ?? $translater->start_page ?: ''; $type = $_REQUEST['type'] ?? $translater->start_page ?: '';
$module = $_REQUEST['module'] ?? ''; $module = $_REQUEST['module'] ?? '';
$lang = $_REQUEST['lang'] ?? ''; $lang = $_REQUEST['lang'] ?? '';
$action = $_POST['action'] ?? ''; $action = $_POST['action'] ?? '';
if (!in_array($type, ['plugin', 'theme'])) { if (!in_array($type, ['plugin', 'theme'])) {
$type = ''; $type = '';
@ -31,7 +30,7 @@ if (!in_array($type, ['plugin', 'theme'])) {
if (!empty($type) && !empty($module)) { if (!empty($type) && !empty($module)) {
try { try {
$module = $translater->getModule($type, $module); $module = $translater->getModule($type, $module);
} catch(Exception $e) { } catch (Exception $e) {
$core->error->add($e->getMessage()); $core->error->add($e->getMessage());
$module = ''; $module = '';
} }
@ -40,7 +39,7 @@ if (!empty($type) && !empty($module)) {
if (!empty($module) && !empty($lang)) { if (!empty($module) && !empty($lang)) {
try { try {
$lang = $translater->getLang($module, $lang); $lang = $translater->getLang($module, $lang);
} catch(Exception $e) { } catch (Exception $e) {
$core->error->add($e->getMessage()); $core->error->add($e->getMessage());
$lang = ''; $lang = '';
} }
@ -53,10 +52,10 @@ if (empty($type)) {
$breadcrumb[$type == 'plugin' ? __('Plugins') : __('Themes')] = ''; $breadcrumb[$type == 'plugin' ? __('Plugins') : __('Themes')] = '';
} elseif (empty($lang)) { } elseif (empty($lang)) {
$breadcrumb[$type == 'plugin' ? __('Plugins') : __('Themes')] = $core->adminurl->get('translater', ['type' => $type]); $breadcrumb[$type == 'plugin' ? __('Plugins') : __('Themes')] = $core->adminurl->get('translater', ['type' => $type]);
$breadcrumb[html::escapeHTML($module->name)] = ''; $breadcrumb[html::escapeHTML($module->name)] = '';
} elseif (!empty($lang)) { } elseif (!empty($lang)) {
$breadcrumb[$type == 'plugin' ? __('Plugins') : __('Themes')] = $core->adminurl->get('translater', ['type' => $type]); $breadcrumb[$type == 'plugin' ? __('Plugins') : __('Themes')] = $core->adminurl->get('translater', ['type' => $type]);
$breadcrumb[html::escapeHTML($module->name)] = $core->adminurl->get('translater', ['type' => $type, 'module' => $module->id]); $breadcrumb[html::escapeHTML($module->name)] = $core->adminurl->get('translater', ['type' => $type, 'module' => $module->id]);
$breadcrumb[html::escapeHTML(sprintf(__('%s language edition'), $lang->name))] = ''; $breadcrumb[html::escapeHTML(sprintf(__('%s language edition'), $lang->name))] = '';
} }
@ -66,7 +65,7 @@ try {
throw new Exception(__('Nothing to backup')); throw new Exception(__('Nothing to backup'));
} }
$module_codes = $module->getUsedlangs(); $module_codes = $module->getUsedlangs();
foreach($module_codes as $code_id) { foreach ($module_codes as $code_id) {
if (in_array($code_id, $_POST['codes'])) { if (in_array($code_id, $_POST['codes'])) {
$module->createBackup($code_id); $module->createBackup($code_id);
} }
@ -77,10 +76,10 @@ try {
if ($action == 'module_restore_backup') { if ($action == 'module_restore_backup') {
if (empty($module) || empty($_POST['files'])) { if (empty($module) || empty($_POST['files'])) {
throw New Exception(__('Nothing to restore')); throw new Exception(__('Nothing to restore'));
} }
$module_backups = $module->getBackups(true); $module_backups = $module->getBackups(true);
foreach($module_backups as $backup_file) { foreach ($module_backups as $backup_file) {
if (in_array($backup_file, $_POST['files'])) { if (in_array($backup_file, $_POST['files'])) {
$module->restoreBackup($backup_file); $module->restoreBackup($backup_file);
} }
@ -91,10 +90,10 @@ try {
if ($action == 'module_delete_backup') { if ($action == 'module_delete_backup') {
if (empty($module) || empty($_POST['files'])) { if (empty($module) || empty($_POST['files'])) {
throw New Exception(__('Nothing to delete')); throw new Exception(__('Nothing to delete'));
} }
$module_backups = $module->getBackups(true); $module_backups = $module->getBackups(true);
foreach($module_backups as $backup_file) { foreach ($module_backups as $backup_file) {
if (in_array($backup_file, $_POST['files'])) { if (in_array($backup_file, $_POST['files'])) {
$module->deleteBackup($backup_file); $module->deleteBackup($backup_file);
} }
@ -138,7 +137,7 @@ try {
throw new Exception(__('Nothing to delete')); throw new Exception(__('Nothing to delete'));
} }
$module_codes = $module->getUsedlangs(); $module_codes = $module->getUsedlangs();
foreach($module_codes as $code_id) { foreach ($module_codes as $code_id) {
if (in_array($code_id, $_POST['codes'])) { if (in_array($code_id, $_POST['codes'])) {
$module->delLang($code_id); $module->delLang($code_id);
} }
@ -152,7 +151,7 @@ try {
throw new Exception(__('Nothing to update')); throw new Exception(__('Nothing to update'));
} }
if (!empty($_POST['update_group'])) { if (!empty($_POST['update_group'])) {
foreach($_POST['entries'] as $i => $entry) { foreach ($_POST['entries'] as $i => $entry) {
if (isset($entry['check']) && isset($_POST['multigroup'])) { if (isset($entry['check']) && isset($_POST['multigroup'])) {
$_POST['entries'][$i]['group'] = $_POST['multigroup']; $_POST['entries'][$i]['group'] = $_POST['multigroup'];
} }
@ -162,16 +161,14 @@ try {
dcPage::addSuccessNotice(__('Language successfully updated')); dcPage::addSuccessNotice(__('Language successfully updated'));
$core->adminurl->redirect('translater', ['type' => $type, 'module' => $module->id, 'lang' => $_POST['code']]); $core->adminurl->redirect('translater', ['type' => $type, 'module' => $module->id, 'lang' => $_POST['code']]);
} }
} catch (Exception $e) { } catch (Exception $e) {
$core->error->add($e->getMessage()); $core->error->add($e->getMessage());
} }
echo echo
'<html><head><title>' . __('Translater') . '</title>' . '<html><head><title>' . __('Translater') . '</title>' .
dcPage::jsPageTabs() . dcPage::jsPageTabs() .
dcPage::cssLoad(dcPage::getPF('translater/css/translater.css')) . dcPage::cssLoad(dcPage::getPF('translater/css/translater.css')) .
dcpage::jsJson('translater', [ dcpage::jsJson('translater', [
'title_add_detail' => __('Use this text'), 'title_add_detail' => __('Use this text'),
@ -192,7 +189,7 @@ if (empty($module) && $type != '') {
// modules list // modules list
echo '<form id="theme-form" method="post" action="' . $core->adminurl->get('translater', ['type' => 'plugin']) . '">'; echo '<form id="theme-form" method="post" action="' . $core->adminurl->get('translater', ['type' => 'plugin']) . '">';
$res = ''; $res = '';
$modules = $translater->getModules($type); $modules = $translater->getModules($type);
ksort($modules); ksort($modules);
foreach ($modules as $module) { foreach ($modules as $module) {
@ -217,13 +214,13 @@ if (empty($module) && $type != '') {
if ($module->root_writable) { if ($module->root_writable) {
$codes[$code_id] = sprintf( $codes[$code_id] = sprintf(
'<a class="wait maximal nowrap" title="%s" href="%s">%s (%s)</a>', '<a class="wait maximal nowrap" title="%s" href="%s">%s (%s)</a>',
html::escapeHTML(sprintf(__('Edit language %s of module %s'), html::escapeHTML($code_name), __($module->name))), html::escapeHTML(sprintf(__('Edit language %s of module %s'), html::escapeHTML($code_name), __($module->name))),
$core->adminurl->get('translater', ['type' => $module->type, 'module' => $module->id, 'lang' => $code_id]), $core->adminurl->get('translater', ['type' => $module->type, 'module' => $module->id, 'lang' => $code_id]),
html::escapeHTML($code_name), html::escapeHTML($code_name),
$code_id $code_id
); );
} else { } else {
$codes[$code_id] = html::escapeHTML($code_name) . '(' .$code_id . ')'; $codes[$code_id] = html::escapeHTML($code_name) . '(' . $code_id . ')';
} }
} }
$res .= sprintf( $res .= sprintf(
@ -237,7 +234,7 @@ if (empty($module) && $type != '') {
echo ' echo '
<div class="table-outer"> <div class="table-outer">
<table class="clear"> <table class="clear">
<caption>' . sprintf(__('Modules list of type "%s"'), $type) .'</caption> <caption>' . sprintf(__('Modules list of type "%s"'), $type) . '</caption>
<tr> <tr>
<th class="nowrap">' . __('Id') . '</th> <th class="nowrap">' . __('Id') . '</th>
<th class="nowrap">' . __('Languages') . '</th> <th class="nowrap">' . __('Languages') . '</th>
@ -246,20 +243,18 @@ if (empty($module) && $type != '') {
</tr>' . </tr>' .
$res . $res .
'</table></div>'; '</table></div>';
} else { } else {
echo '<tr><td colspan="6">' . __('There is no editable modules') . '</td></tr>'; echo '<tr><td colspan="6">' . __('There is no editable modules') . '</td></tr>';
} }
echo '</form>'; echo '</form>';
dcPage::helpBlock('translater.type'); dcPage::helpBlock('translater.type');
} elseif (!empty($module) && empty($lang)) { } elseif (!empty($module) && empty($lang)) {
$codes = $module->getUsedLangs(); $codes = $module->getUsedLangs();
$backups = $module->getBackups(); $backups = $module->getBackups();
$unused_codes = $module->getUnusedLangs(); $unused_codes = $module->getUnusedLangs();
// module summary // module summary
echo '<h3>' . sprintf(__('Module %s %s by %s'), $module->name, $module->version, $module->author) . '</h3> echo '<h3>' . sprintf(__('Module %s %s by %s'), $module->name, $module->version, $module->author) . '</h3>
<ul class="nice col"> <ul class="nice col">
<li><strong>' . __('Root') . '</strong> ' . $module->root . '</li> <li><strong>' . __('Root') . '</strong> ' . $module->root . '</li>
@ -270,38 +265,38 @@ if (empty($module) && $type != '') {
// existing languages // existing languages
if (count($codes)) { if (count($codes)) {
echo echo
'<div class="clear fieldset"><h3>' . __('Translations') . '</h3>' . '<div class="clear fieldset"><h3>' . __('Translations') . '</h3>' .
'<form id="module-translations-form" method="post" action="' . $core->adminurl->get('translater') . '">' . '<form id="module-translations-form" method="post" action="' . $core->adminurl->get('translater') . '">' .
'<table class="clear maximal">' . '<table class="clear maximal">' .
'<caption>' . __('Existing languages translations') .'</caption>' . '<caption>' . __('Existing languages translations') . '</caption>' .
'<tr>' . '<tr>' .
'<th class="nowrap" colspan="2">' . __('Language') . '</th>' . '<th class="nowrap" colspan="2">' . __('Language') . '</th>' .
'<th class="nowrap">' . __('Code') . '</th>' . '<th class="nowrap">' . __('Code') . '</th>' .
'<th class="nowrap">' . __('Backups') . '</th>' . '<th class="nowrap">' . __('Backups') . '</th>' .
'<th class="nowrap">' . __('Last backup') . '</th>' . '<th class="nowrap">' . __('Last backup') . '</th>' .
'</tr>'; '</tr>';
foreach($codes AS $code_name => $code_id) { foreach ($codes as $code_name => $code_id) {
echo echo
'<tr class="line">' . '<tr class="line">' .
'<td class="minimal">' . form::checkbox(['codes[]', 'existing_code_' . $code_id], $code_id, '', '', '', false) . '</td>' . '<td class="minimal">' . form::checkbox(['codes[]', 'existing_code_' . $code_id], $code_id, '', '', '', false) . '</td>' .
'<td class="nowrap">' . '<td class="nowrap">' .
'<a href="' . '<a href="' .
$core->adminurl->get('translater', ['type' => $module->type, 'module' => $module->id, 'lang' => $code_id]) $core->adminurl->get('translater', ['type' => $module->type, 'module' => $module->id, 'lang' => $code_id])
. '" title="' . sprintf(__('Edit %s language'), html::escapeHTML($code_name)) . '">' . $code_name . '</a>' . . '" title="' . sprintf(__('Edit %s language'), html::escapeHTML($code_name)) . '">' . $code_name . '</a>' .
'</td>' . '</td>' .
'<td class="nowrap maximal"> ' . $code_id . '</td>'; '<td class="nowrap maximal"> ' . $code_id . '</td>';
if (isset($backups[$code_id])) { if (isset($backups[$code_id])) {
foreach($backups[$code_id] AS $file => $info) { foreach ($backups[$code_id] as $file => $info) {
$time[$code_id] = isset($time[$code_id]) && $time[$code_id] > $info['time'] ? $time[$code_id] = isset($time[$code_id]) && $time[$code_id] > $info['time'] ?
$time[$code_id] : $info['time']; $time[$code_id] : $info['time'];
} }
echo echo
'<td class="nowrap">' . count($backups[$code_id]) . '</td>' . '<td class="nowrap">' . count($backups[$code_id]) . '</td>' .
'<td class="nowrap"> ' . '<td class="nowrap"> ' .
dt::str('%Y-%m-%d %H:%M', $time[$code_id], $core->blog->settings->system->blog_timezone) . dt::str('%Y-%m-%d %H:%M', $time[$code_id], $core->blog->settings->system->blog_timezone) .
'</td>'; '</td>';
} else { } else {
echo '<td class="nowrap">' . __('no backups') . '</td><td class="maximal nowrap">-</td>'; echo '<td class="nowrap">' . __('no backups') . '</td><td class="maximal nowrap">-</td>';
@ -312,14 +307,14 @@ if (empty($module) && $type != '') {
<div class="two-cols"> <div class="two-cols">
<p class="col checkboxes-helpers"></p> <p class="col checkboxes-helpers"></p>
<p class="col right">' . __('Selected languages action:') . ' ' . <p class="col right">' . __('Selected languages action:') . ' ' .
form::combo('action', [ form::combo('action', [
__('Backup languages') => 'module_create_backups', __('Backup languages') => 'module_create_backups',
__('Delete languages') => 'module_delete_codes', __('Delete languages') => 'module_delete_codes',
__('Export languages') => 'module_export_pack' __('Export languages') => 'module_export_pack'
]) . ' ]) . '
<input id="do-action" type="submit" value="' . __('ok') . '" /></p>' . <input id="do-action" type="submit" value="' . __('ok') . '" /></p>' .
$core->formNonce() . $core->formNonce() .
$core->adminurl->getHiddenFormFields( $core->adminurl->getHiddenFormFields(
'translater', 'translater',
['type' => $module->type, 'module' => $module->id] ['type' => $module->type, 'module' => $module->id]
@ -331,40 +326,41 @@ if (empty($module) && $type != '') {
if (!empty($codes) || !empty($backups)) { if (!empty($codes) || !empty($backups)) {
// delete / retore backups // delete / retore backups
if (!empty($backups)) { if (!empty($backups)) {
echo '<div class="fieldset"><h3>' . __('Backups') . '</h3>' . echo '<div class="fieldset"><h3>' . __('Backups') . '</h3>' .
'<form id="module-backups-form" method="post" action="' . $core->adminurl->get('translater') . '">' . '<form id="module-backups-form" method="post" action="' . $core->adminurl->get('translater') . '">' .
'<table class="clear">' . '<table class="clear">' .
'<caption>' . __('Existing languages backups') . '</caption>' . '<caption>' . __('Existing languages backups') . '</caption>' .
'<tr>' . '<tr>' .
'<th class="nowrap" colspan="2">' . __('Language') . '</th>' . '<th class="nowrap" colspan="2">' . __('Language') . '</th>' .
'<th class="nowrap">' . __('Code') . '</th>' . '<th class="nowrap">' . __('Code') . '</th>' .
'<th class="nowrap">' . __('Date') . '</th>' . '<th class="nowrap">' . __('Date') . '</th>' .
'<th class="nowrap">' . __('File') . '</th>' . '<th class="nowrap">' . __('File') . '</th>' .
'<th class="nowrap">' . __('Size') . '</th>' . '<th class="nowrap">' . __('Size') . '</th>' .
'</tr>'; '</tr>';
$table_line = $table_line = '<tr class="line">' .
'<tr class="line">' .
'<td class="minimal">%s</td>' . '<td class="minimal">%s</td>' .
'<td class="nowrap"><label for="%s">%s</label></td>' . '<td class="nowrap"><label for="%s">%s</label></td>' .
'<td class="nowrap maximal">%s</td>' . '<td class="nowrap maximal">%s</td>' .
'<td class="nowrap count">%s</td>' . '<td class="nowrap count">%s</td>' .
'<td class="nowrap">%s</td>' . '<td class="nowrap">%s</td>' .
'<td class="nowrap count">%s</td>' . '<td class="nowrap count">%s</td>' .
'</tr>'; '</tr>';
$i=0; $i = 0;
foreach($backups as $backup_codes) { foreach ($backups as $backup_codes) {
foreach($backup_codes as $backup_file => $backup_code) { foreach ($backup_codes as $backup_file => $backup_code) {
$i++; $i++;
$form_id = 'form_file_' . $backup_code['code'] . $backup_code['time']; $form_id = 'form_file_' . $backup_code['code'] . $backup_code['time'];
echo sprintf($table_line, echo sprintf(
$table_line,
form::checkbox(['files[]', $form_id], $backup_file, '', '', '', false), form::checkbox(['files[]', $form_id], $backup_file, '', '', '', false),
$form_id, $backup_code['name'], $form_id,
$backup_code['name'],
$backup_code['code'], $backup_code['code'],
dt::str( dt::str(
$core->blog->settings->system->date_format . ' ' . $core->blog->settings->system->time_format, $core->blog->settings->system->date_format . ' ' . $core->blog->settings->system->time_format,
$backup_code['time'], $backup_code['time'],
$core->blog->settings->system->blog_timezone $core->blog->settings->system->blog_timezone
), ),
$backup_code['path']['basename'], $backup_code['path']['basename'],
@ -377,13 +373,13 @@ if (empty($module) && $type != '') {
<div class="two-cols"> <div class="two-cols">
<p class="col checkboxes-helpers"></p> <p class="col checkboxes-helpers"></p>
<p class="col right">' . __('Selected backups action:') . ' ' . <p class="col right">' . __('Selected backups action:') . ' ' .
form::combo('action', [ form::combo('action', [
__('Restore backups') => 'module_restore_backup', __('Restore backups') => 'module_restore_backup',
__('Delete backups') => 'module_delete_backup' __('Delete backups') => 'module_delete_backup'
]) . ' ]) . '
<input id="do-action" type="submit" value="' . __('ok') . '" /></p>' . <input id="do-action" type="submit" value="' . __('ok') . '" /></p>' .
$core->formNonce() . $core->formNonce() .
$core->adminurl->getHiddenFormFields( $core->adminurl->getHiddenFormFields(
'translater', 'translater',
['type' => $module->type, 'module' => $module->id] ['type' => $module->type, 'module' => $module->id]
@ -398,18 +394,18 @@ if (empty($module) && $type != '') {
if (!empty($unused_codes)) { if (!empty($unused_codes)) {
echo '<div class="col fieldset"><h3>' . __('Add language') . '</h3> echo '<div class="col fieldset"><h3>' . __('Add language') . '</h3>
<form id="muodule-code-create-form" method="post" action="' . $core->adminurl->get('translater') . '"> <form id="muodule-code-create-form" method="post" action="' . $core->adminurl->get('translater') . '">
<p class="field"><label for="code">' . __('Select language:') . '</label>' . <p class="field"><label for="code">' . __('Select language:') . '</label>' .
form::combo(['code'], array_merge(['-' => '-'], $unused_codes), $core->auth->getInfo('user_lang')) . '</p>'; form::combo(['code'], array_merge(['-' => '-'], $unused_codes), $core->auth->getInfo('user_lang')) . '</p>';
if (empty($codes)) { if (empty($codes)) {
echo '<p>' . form::hidden(['from'], '') . '</p>'; echo '<p>' . form::hidden(['from'], '') . '</p>';
} else { } else {
echo echo
'<p class="field"><label for="from">' . __('Copy from language:') . '</label>' . '<p class="field"><label for="from">' . __('Copy from language:') . '</label>' .
form::combo(['from'], array_merge(['-' => ''], $codes)) . ' (' . __('optionnal') . ')</p>'; form::combo(['from'], array_merge(['-' => ''], $codes)) . ' (' . __('optionnal') . ')</p>';
} }
echo ' echo '
<p><input type="submit" name="save" value="' . __('Create') . '" />' . <p><input type="submit" name="save" value="' . __('Create') . '" />' .
$core->formNonce() . $core->formNonce() .
$core->adminurl->getHiddenFormFields( $core->adminurl->getHiddenFormFields(
'translater', 'translater',
['type' => $module->type, 'module' => $module->id, 'action' => 'module_add_code'] ['type' => $module->type, 'module' => $module->id, 'action' => 'module_add_code']
@ -423,8 +419,8 @@ if (empty($module) && $type != '') {
<p><label for="packfile">' . __('Select languages package to import:') . '<label> ' . <p><label for="packfile">' . __('Select languages package to import:') . '<label> ' .
'<input id="packfile" type="file" name="packfile" /></p> '<input id="packfile" type="file" name="packfile" /></p>
<p> <p>
<input type="submit" name="save" value="' . __('Import') . '" />' . <input type="submit" name="save" value="' . __('Import') . '" />' .
$core->formNonce() . $core->formNonce() .
$core->adminurl->getHiddenFormFields( $core->adminurl->getHiddenFormFields(
'translater', 'translater',
['type' => $module->type, 'module' => $module->id, 'action' => 'module_import_pack'] ['type' => $module->type, 'module' => $module->id, 'action' => 'module_import_pack']
@ -434,25 +430,23 @@ if (empty($module) && $type != '') {
echo '</div>'; echo '</div>';
dcPage::helpBlock('translater.module'); dcPage::helpBlock('translater.module');
} elseif (!empty($lang)) { } elseif (!empty($lang)) {
$lines = $lang->getMessages(); $lines = $lang->getMessages();
echo echo
'<div id="lang-form">' . '<div id="lang-form">' .
'<form id="lang-edit-form" method="post" action="' . $core->adminurl->get('translater') . '">' . '<form id="lang-edit-form" method="post" action="' . $core->adminurl->get('translater') . '">' .
'<table class="table-outer">' . '<table class="table-outer">' .
'<caption>' . sprintf(__('List of %s localized strings'), count($lines)) . '</caption>' . '<caption>' . sprintf(__('List of %s localized strings'), count($lines)) . '</caption>' .
'<tr>' . '<tr>' .
'<th colspan="2">' . __('Group') . '</th>' . '<th colspan="2">' . __('Group') . '</th>' .
'<th>' . __('String') . '</th>' . '<th>' . __('String') . '</th>' .
'<th>' . __('Translation') . '</th>' . '<th>' . __('Translation') . '</th>' .
'<th>' . __('Existing') . '</th>' . '<th>' . __('Existing') . '</th>' .
'<th>' . __('File') . '</th>' . '<th>' . __('File') . '</th>' .
'</tr>'; '</tr>';
$table_line = $table_line = '<tr class="line%s">' .
'<tr class="line%s">' .
'<td class="nowrap minimal">%s</td>' . '<td class="nowrap minimal">%s</td>' .
'<td class="nowrap minimal">%s</td>' . '<td class="nowrap minimal">%s</td>' .
'<td>%s</td>' . '<td>%s</td>' .
@ -464,20 +458,20 @@ if (empty($module) && $type != '') {
$table_li = '<i>%s</i><br />'; $table_li = '<i>%s</i><br />';
$i = 1; $i = 1;
foreach ($lines AS $msgid => $rs) { foreach ($lines as $msgid => $rs) {
$in_dc = ($rs['in_dc'] && $translater->parse_nodc); $in_dc = ($rs['in_dc'] && $translater->parse_nodc);
$allowed_l10n_groups = array_combine($translater::$allowed_l10n_groups, $translater::$allowed_l10n_groups); $allowed_l10n_groups = array_combine($translater::$allowed_l10n_groups, $translater::$allowed_l10n_groups);
$t_msgstr = $t_files = $strin = []; $t_msgstr = $t_files = $strin = [];
foreach($rs['o_msgstrs'] as $o_msgstr) { foreach ($rs['o_msgstrs'] as $o_msgstr) {
if (!isset($strin[$o_msgstr['msgstr'][0]])) { if (!isset($strin[$o_msgstr['msgstr'][0]])) {
$strin[$o_msgstr['msgstr'][0]] = []; $strin[$o_msgstr['msgstr'][0]] = [];
} }
$strin[$o_msgstr['msgstr'][0]][] = ['module' => $o_msgstr['module'], 'file' => $o_msgstr['file']]; $strin[$o_msgstr['msgstr'][0]][] = ['module' => $o_msgstr['module'], 'file' => $o_msgstr['file']];
} }
foreach($strin as $k => $v) { foreach ($strin as $k => $v) {
$res = []; $res = [];
foreach($v as $str) { foreach ($v as $str) {
$res[] = sprintf($table_li, html::escapeHTML($str['module'] . ':' . $str['file'])); $res[] = sprintf($table_li, html::escapeHTML($str['module'] . ':' . $str['file']));
} }
$t_msgstr[] = sprintf($table_ul, html::escapeHTML($k), implode('', $res)); $t_msgstr[] = sprintf($table_ul, html::escapeHTML($k), implode('', $res));
@ -488,18 +482,21 @@ if (empty($module) && $type != '') {
$t_files[] = $rs['files'][0][0] . ':' . $rs['files'][0][1]; $t_files[] = $rs['files'][0][0] . ':' . $rs['files'][0][1];
} else { } else {
$res = []; $res = [];
foreach($rs['files'] as $location) { foreach ($rs['files'] as $location) {
$res[] = sprintf($table_li, implode(' : ', $location)); $res[] = sprintf($table_li, implode(' : ', $location));
} }
$t_files[] = sprintf($table_ul, sprintf(__('%s occurrences'), count($rs['files'])), implode('', $res));; $t_files[] = sprintf($table_ul, sprintf(__('%s occurrences'), count($rs['files'])), implode('', $res));
;
} }
} }
echo sprintf($table_line, $in_dc ? ' offline' : ' translaterline', echo sprintf(
$table_line,
$in_dc ? ' offline' : ' translaterline',
form::checkbox(['entries[' . $i . '][check]'], 1), form::checkbox(['entries[' . $i . '][check]'], 1),
form::combo(['entries[' . $i . '][group]'], $allowed_l10n_groups, $rs['group'], '', '', $in_dc), form::combo(['entries[' . $i . '][group]'], $allowed_l10n_groups, $rs['group'], '', '', $in_dc),
html::escapeHTML($msgid), html::escapeHTML($msgid),
form::hidden(['entries[' . $i . '][msgid]'], html::escapeHTML($msgid)) . form::hidden(['entries[' . $i . '][msgid]'], html::escapeHTML($msgid)) .
form::field(['entries[' . $i . '][msgstr][0]'], 48, 255, html::escapeHTML($rs['msgstr'][0]), '', '', $in_dc), form::field(['entries[' . $i . '][msgstr][0]'], 48, 255, html::escapeHTML($rs['msgstr'][0]), '', '', $in_dc),
implode('', $t_msgstr), implode('', $t_msgstr),
implode('', $t_files) implode('', $t_files)
@ -507,29 +504,31 @@ if (empty($module) && $type != '') {
if (!empty($rs['plural'])) { if (!empty($rs['plural'])) {
$t_msgstr = $strin = []; $t_msgstr = $strin = [];
foreach($lang->plural as $j => $plural) { foreach ($lang->plural as $j => $plural) {
foreach($rs['o_msgstrs'] as $o_msgstr) { foreach ($rs['o_msgstrs'] as $o_msgstr) {
if (isset($o_msgstr['msgstr'][$j+1])) { if (isset($o_msgstr['msgstr'][$j + 1])) {
if (!isset($strin[$o_msgstr['msgstr'][$j+1]])) { if (!isset($strin[$o_msgstr['msgstr'][$j + 1]])) {
$strin[$o_msgstr['msgstr'][$j+1]] = []; $strin[$o_msgstr['msgstr'][$j + 1]] = [];
} }
$strin[$o_msgstr['msgstr'][$j+1]][] = ['module' => $o_msgstr['module'], 'file' => $o_msgstr['file']]; $strin[$o_msgstr['msgstr'][$j + 1]][] = ['module' => $o_msgstr['module'], 'file' => $o_msgstr['file']];
} }
} }
foreach($strin as $k => $v) { foreach ($strin as $k => $v) {
$res = []; $res = [];
foreach($v as $str) { foreach ($v as $str) {
$res[] = sprintf($table_li, html::escapeHTML($str['module'] . ':' . $str['file'])); $res[] = sprintf($table_li, html::escapeHTML($str['module'] . ':' . $str['file']));
} }
$t_msgstr[] = sprintf($table_ul, html::escapeHTML($k), implode('', $res)); $t_msgstr[] = sprintf($table_ul, html::escapeHTML($k), implode('', $res));
} }
echo sprintf($table_line, $in_dc ? ' offline' : ' translaterline', echo sprintf(
$table_line,
$in_dc ? ' offline' : ' translaterline',
'+', '+',
sprintf(__('Plural "%s"'), $plural), sprintf(__('Plural "%s"'), $plural),
sprintf(__('Plural form of "%s"'), $rs['plural']), sprintf(__('Plural form of "%s"'), $rs['plural']),
form::hidden(['entries[' . $i . '][msgid_plural]'], html::escapeHTML($rs['plural'])) . form::hidden(['entries[' . $i . '][msgid_plural]'], html::escapeHTML($rs['plural'])) .
form::field(['entries[' . $i . '][msgstr][' . $j+1 . ']'], 48, 255, html::escapeHTML($rs['msgstr'][$j+1] ?? ''), '', '', $in_dc), form::field(['entries[' . $i . '][msgstr][' . $j + 1 . ']'], 48, 255, html::escapeHTML($rs['msgstr'][$j + 1] ?? ''), '', '', $in_dc),
implode('', $t_msgstr), implode('', $t_msgstr),
'' ''
); );
@ -537,7 +536,9 @@ if (empty($module) && $type != '') {
} }
$i++; $i++;
} }
echo sprintf($table_line, ' offline', echo sprintf(
$table_line,
' offline',
form::checkbox(['entries[' . $i . '][check]'], 1), form::checkbox(['entries[' . $i . '][check]'], 1),
form::combo(['entries[' . $i . '][group]'], $allowed_l10n_groups, 'main'), form::combo(['entries[' . $i . '][group]'], $allowed_l10n_groups, 'main'),
form::field(['entries[' . $i . '][msgid]'], 48, 255, ''), form::field(['entries[' . $i . '][msgid]'], 48, 255, ''),
@ -545,50 +546,51 @@ if (empty($module) && $type != '') {
'', '',
'' ''
); );
echo echo
'</table>' . '</table>' .
'<div class="two-cols">' . '<div class="two-cols">' .
'<div class="col left">' . '<div class="col left">' .
'<p class="checkboxes-helpers"></p>' . '<p class="checkboxes-helpers"></p>' .
'<p><label for="update_group">' . '<p><label for="update_group">' .
form::checkbox('update_group', 1) . form::checkbox('update_group', 1) .
__('Change the group of the selected translations to:') . ' ' . __('Change the group of the selected translations to:') . ' ' .
form::combo('multigroup', $allowed_l10n_groups) . '</label></p>' . form::combo('multigroup', $allowed_l10n_groups) . '</label></p>' .
'</div>' . '</div>' .
'<p class="col right">' . '<p class="col right">' .
'<input id="do-action" type="submit" value="' . __('Save') . ' (s)" accesskey="s" /></p>' . '<input id="do-action" type="submit" value="' . __('Save') . ' (s)" accesskey="s" /></p>' .
$core->formNonce() . $core->formNonce() .
form::hidden(['code'], $lang->code) . form::hidden(['code'], $lang->code) .
$core->adminurl->getHiddenFormFields( $core->adminurl->getHiddenFormFields(
'translater', 'translater',
['type' => $module->type, 'module' => $module->id, 'lang' => $lang->code, 'action' => 'module_update_code'] ['type' => $module->type, 'module' => $module->id, 'lang' => $lang->code, 'action' => 'module_update_code']
) . ) .
'</p></div>' . '</p></div>' .
'</form>' . '</form>' .
'<p>&nbsp;</p>' . '<p>&nbsp;</p>' .
'</div>'; '</div>';
dcPage::helpBlock('translater.lang'); dcPage::helpBlock('translater.lang');
} else { } else {
$line = '<li><a href="%s"%s>%s</a></li>'; $line = '<li><a href="%s"%s>%s</a></li>';
echo '<h4><i>' . __('Translate your Dotclear plugins and themes') . '</i></h4>' . echo '<h4><i>' . __('Translate your Dotclear plugins and themes') . '</i></h4>' .
sprintf( sprintf(
'<h3><ul class="nice">%s</ul></h3>', '<h3><ul class="nice">%s</ul></h3>',
sprintf( sprintf(
$line, $line,
$core->adminurl->get('translater', ['type' => 'plugin']), $core->adminurl->get('translater', ['type' => 'plugin']),
$type == 'plugin' ? ' class="active"' : '', $type == 'plugin' ? ' class="active"' : '',
__('Translate plugins')) . __('Translate plugins')
) .
sprintf( sprintf(
$line, $line,
$core->adminurl->get('translater', ['type' => 'theme']), $core->adminurl->get('translater', ['type' => 'theme']),
$type == 'theme' ? ' class="active"' : '', $type == 'theme' ? ' class="active"' : '',
__('Translate themes')) __('Translate themes')
)
); );
dcPage::helpBlock('translater.index'); dcPage::helpBlock('translater.index');
} }
echo '</body></html>'; echo '</body></html>';

View File

@ -1,20 +1,19 @@
<?php <?php
# -- BEGIN LICENSE BLOCK ---------------------------------- /**
# * @brief translater, a plugin for Dotclear 2
# This file is part of translater, a plugin for Dotclear 2. *
# * @package Dotclear
# Copyright (c) 2009-2021 Jean-Christian Denis and contributors * @subpackage Plugin
# *
# Licensed under the GPL version 2.0 license. * @author Jean-Christian Denis & contributors
# A copy of this license is available in LICENSE file or at *
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * @copyright Jean-Christian Denis
# * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
# -- END LICENSE BLOCK ------------------------------------ */
if (!defined('DC_RC_PATH')) { if (!defined('DC_RC_PATH')) {
return; return;
} }
foreach(['index', 'type', 'module', 'lang', 'config'] as $v) { foreach (['index', 'type', 'module', 'lang', 'config'] as $v) {
$__resources['help']['translater.' . $v] = dirname(__FILE__) . '/help/translater.' . $v . '.html'; $__resources['help']['translater.' . $v] = dirname(__FILE__) . '/help/translater.' . $v . '.html';
} }