use l10n class for php. and po becomes a requirment
This commit is contained in:
parent
51dc37ad16
commit
5934d267f1
@ -41,9 +41,6 @@ if (!empty($_POST['save'])) {
|
||||
|
||||
echo '
|
||||
<div class="fieldset"><h4>' . __('Translation') . '</h4>
|
||||
<p><label for="write_po">' .
|
||||
form::checkbox('write_po', '1' ,$translater->write_po) .
|
||||
__('Write .po files') . '</label></p>
|
||||
<p><label for="write_langphp">' .
|
||||
form::checkbox('write_langphp', '1', $translater->write_langphp) .
|
||||
__('Write .lang.php files') . '</label></p>
|
||||
|
@ -150,6 +150,8 @@ class dcTranslaterLang
|
||||
];
|
||||
}
|
||||
|
||||
//TODO: tpl file extract
|
||||
|
||||
unset($contents);
|
||||
}
|
||||
return $res;
|
||||
@ -162,7 +164,7 @@ class dcTranslaterLang
|
||||
*/
|
||||
public function getMsgStrs(): array
|
||||
{
|
||||
$res = $scanned = [];
|
||||
$res = $exists = $scanned = [];
|
||||
|
||||
$langs = $this->module->getLangs(true);
|
||||
if (!isset($langs[$this->code])) {
|
||||
@ -183,7 +185,7 @@ class dcTranslaterLang
|
||||
}
|
||||
$entries = $po[1];
|
||||
foreach($entries as $entry) {
|
||||
$res[] = array(
|
||||
$res[] = [
|
||||
'msgid' => $entry['msgid'],
|
||||
'msgid_plural' => $entry['msgid_plural'] ?? '',
|
||||
'msgstr' => is_array($entry['msgstr']) ? $entry['msgstr'] : [$entry['msgstr']],
|
||||
@ -192,28 +194,10 @@ class dcTranslaterLang
|
||||
'path' => $path,
|
||||
'file' => basename($file),
|
||||
'group'=> str_replace('.po', '', basename($file))
|
||||
);
|
||||
];
|
||||
$exists[] = $entry['msgid'];
|
||||
}
|
||||
/*
|
||||
# .lang.php files
|
||||
} elseif (self::isLangphpFile($file)) {
|
||||
$php = self::getLangphpFile($path);
|
||||
foreach($php AS $id => $str) {
|
||||
# Don't overwrite .po
|
||||
if (isset($is_po[$requested_lang][$id])) {
|
||||
continue;
|
||||
}
|
||||
$res[] = array(
|
||||
'msgid' => self::encodeMsg($id),
|
||||
'msgstr' => self::encodeMsg($str),
|
||||
'lang' => $requested_lang,
|
||||
'type' => 'php',
|
||||
'path' => $path,
|
||||
'file' => basename($file),
|
||||
'group'=> str_replace('.lang.php', '', basename($file))
|
||||
);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
|
@ -630,40 +630,6 @@ class dcTranslaterModule
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a lang file
|
||||
*
|
||||
* @param string $file The full file path
|
||||
* @param string $content The file content
|
||||
* @param boolean $throw Silently failed
|
||||
* @return boolean True on success
|
||||
*/
|
||||
private function writeLang(string $file, string $content, bool $throw = false)
|
||||
{
|
||||
$path = path::info($file);
|
||||
if (is_dir($path['dirname']) && !is_writable($path['dirname'])
|
||||
|| file_exists($file) && !is_writable($file)) {
|
||||
throw new Exception(sprintf(
|
||||
__('Cannot grant write acces on lang file %s'), $file
|
||||
));
|
||||
}
|
||||
|
||||
# -- BEHAVIOR -- dcTranslaterBeforeWriteLangFile
|
||||
$this->core->callBehavior('dcTranslaterBeforeWriteLangFile', $file, $content, $throw);
|
||||
|
||||
$f = @files::putContent($file,$content);
|
||||
if (!$f && $throw) {
|
||||
throw new Exception(sprintf(
|
||||
__('Cannot write lang file %s'), $file
|
||||
));
|
||||
}
|
||||
|
||||
# -- BEHAVIOR -- dcTranslaterAfterWriteLangFile
|
||||
$this->core->callBehavior('dcTranslaterAfterWriteLangFile', $f, $file, $content, $throw);
|
||||
|
||||
return $f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct and parse a .po file
|
||||
*
|
||||
@ -673,10 +639,6 @@ class dcTranslaterModule
|
||||
*/
|
||||
private function setPoContent(string $lang, string $group, array $msgs)
|
||||
{
|
||||
if (!$this->translater->write_po) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$lang = new dcTranslaterLang($this, $lang);
|
||||
|
||||
$content = '';
|
||||
@ -742,7 +704,20 @@ class dcTranslaterModule
|
||||
$content .= "\n";
|
||||
}
|
||||
|
||||
self::writeLang($this->locales . '/' . $lang->code . '/' . $group . '.po', $content, true);
|
||||
$file = $this->locales . '/' . $lang->code . '/' . $group . '.po';
|
||||
$path = path::info($file);
|
||||
if (is_dir($path['dirname']) && !is_writable($path['dirname'])
|
||||
|| file_exists($file) && !is_writable($file)) {
|
||||
throw new Exception(sprintf(
|
||||
__('Cannot grant write acces on lang file %s'), $file
|
||||
));
|
||||
}
|
||||
|
||||
if (!($f = @files::putContent($file, $content))) {
|
||||
throw new Exception(sprintf(
|
||||
__('Cannot write lang file %s'), $file
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -760,7 +735,7 @@ class dcTranslaterModule
|
||||
|
||||
$lang = new dcTranslaterLang($this, $lang);
|
||||
|
||||
$content = "<?php\n";
|
||||
$content = '';
|
||||
if ($this->translater->parse_comment) {
|
||||
$content .=
|
||||
'// Language: ' . $lang->name . " \n" .
|
||||
@ -780,42 +755,8 @@ class dcTranslaterModule
|
||||
$content .=
|
||||
'// Translated with dcTranslater - ' . $this->core->plugins->moduleInfo('translater', 'version') . " \n\n";
|
||||
}
|
||||
if ($this->translater->parse_comment) {
|
||||
$msgids = $lang->getMsgids();
|
||||
foreach($msgids as $msg) {
|
||||
if (isset($msgs[$msg['msgid']])) {
|
||||
$comments[$msg['msgid']] = (isset($comments[$msg['msgid']]) ?
|
||||
$comments[$msg['msgid']] : '') .
|
||||
'#'.trim($msg['file'],'/') . ':' . $msg['line'] . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach($msgs as $msg) {
|
||||
if (empty($msg['msgstr'][0])) {
|
||||
continue;
|
||||
}
|
||||
if ($this->translater->parse_comment && isset($comments[$msg['msgid']])) {
|
||||
$content .= $comments[$msg['msgid']];
|
||||
}
|
||||
if (empty($msg['msgid_plural'])) {
|
||||
$content .=
|
||||
'$GLOBALS[\'__l10n\'][\'' . addcslashes($msg['msgid'], "'") . '\'] = ' .
|
||||
'\'' . dcTranslater::langphpString($msg['msgstr'][0], true) . "';\n";
|
||||
} else {
|
||||
foreach($msg['msgstr'] as $i => $plural) {
|
||||
$content .=
|
||||
'$GLOBALS[\'__l10n\'][\'' . addcslashes($msg['msgid'], "'") . '\'][' . $i . '] = ' .
|
||||
'\'' . dcTranslater::langphpString(($msg['msgstr'][$i] ?: ''), true) . "';\n";
|
||||
}
|
||||
}
|
||||
if ($this->translater->parse_comment) {
|
||||
$content .= "\n";
|
||||
}
|
||||
}
|
||||
$content .= "";
|
||||
|
||||
self::writeLang($this->locales . '/' . $lang->code . '/' . $group . '.lang.php', $content, true);
|
||||
l10n::generatePhpFileFromPo($this->locales . '/' . $lang->code . '/' . $group, $content);
|
||||
}
|
||||
//@}
|
||||
}
|
@ -61,12 +61,6 @@ class dcTranslater
|
||||
'type' => 'string',
|
||||
'label' => 'Page to start on'
|
||||
],
|
||||
'write_po' => [
|
||||
'id' => 'translater_write_po',
|
||||
'value' => 1,
|
||||
'type' => 'boolean',
|
||||
'label' => 'Write .po languages files'
|
||||
],
|
||||
'write_langphp' => [
|
||||
'id' => 'translater_write_langphp',
|
||||
'value' => 0,
|
||||
@ -483,25 +477,5 @@ class dcTranslater
|
||||
return trim((string) preg_replace($smap, $rmap, $string));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean a lang.php string
|
||||
*
|
||||
* @param string $string The string to clean
|
||||
* @param boolean $reverse Un/escape string
|
||||
* @return string The cleaned string
|
||||
*/
|
||||
public static function langphpString(string $string, bool $reverse = false): string
|
||||
{
|
||||
if ($reverse) {
|
||||
$smap = array('\'', "\n", "\t", "\r");
|
||||
$rmap = array('\\\'', '\\n"' . "\n" . '"', '\\t', '\\r');
|
||||
return trim((string) str_replace($smap, $rmap, $string));
|
||||
} else {
|
||||
$smap = array('/\\\\n/', '/\\\\r/', '/\\\\t/', "/\\\'/");
|
||||
$rmap = array("\n", "\r", "\t", "'");
|
||||
return trim((string) preg_replace($smap, $rmap, $string));
|
||||
}
|
||||
}
|
||||
//@}
|
||||
}
|
Loading…
Reference in New Issue
Block a user