diff --git a/_config.php b/_config.php index 377b42c..239bd0f 100644 --- a/_config.php +++ b/_config.php @@ -41,9 +41,6 @@ if (!empty($_POST['save'])) { echo '

' . __('Translation') . '

-

diff --git a/inc/class.dc.translater.lang.php b/inc/class.dc.translater.lang.php index 5ef5790..701ef13 100644 --- a/inc/class.dc.translater.lang.php +++ b/inc/class.dc.translater.lang.php @@ -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; diff --git a/inc/class.dc.translater.module.php b/inc/class.dc.translater.module.php index e933a0c..8e7faf3 100644 --- a/inc/class.dc.translater.module.php +++ b/inc/class.dc.translater.module.php @@ -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 = "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); } //@} } \ No newline at end of file diff --git a/inc/class.dc.translater.php b/inc/class.dc.translater.php index 010bb59..5e9b776 100644 --- a/inc/class.dc.translater.php +++ b/inc/class.dc.translater.php @@ -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)); - } - } //@} } \ No newline at end of file