Skip to content
Snippets Groups Projects
Commit 0f3214d3 authored by Jared Hancock's avatar Jared Hancock
Browse files

i18n: Try different concept for content translation

Store a complex text with title and body combined with ASCII 0x04 byte
(field seprator).
parent 00d4b944
No related branches found
No related tags found
No related merge requests found
...@@ -138,14 +138,12 @@ class ContentAjaxAPI extends AjaxController { ...@@ -138,14 +138,12 @@ class ContentAjaxAPI extends AjaxController {
$content = Page::lookup($id, $lang); $content = Page::lookup($id, $lang);
$langs = $cfg->getSecondaryLanguages(); $langs = $cfg->getSecondaryLanguages();
$tags = array( $tag = $content->getTranslateTag('title:body');
$content->getTranslateTag('body') => 'body', $translations = CustomDataTranslation::allTranslations($tag, 'article');
$content->getTranslateTag('title') => 'title',
);
$translations = CustomDataTranslation::allTranslations(
array_keys($tags), 'article');
foreach ($translations as $t) { foreach ($translations as $t) {
$info[$tags[$t->object_hash]][$t->lang] = $t->text; list($title, $body) = explode("\x04", $t->text);
$info['title'][$t->lang] = $title;
$info['body'][$t->lang] = $body;
} }
include STAFFINC_DIR . 'templates/content-manage.tmpl.php'; include STAFFINC_DIR . 'templates/content-manage.tmpl.php';
......
...@@ -82,21 +82,20 @@ class Page { ...@@ -82,21 +82,20 @@ class Page {
function _fetchTranslation($lang=false) { function _fetchTranslation($lang=false) {
if (!isset($this->_local) || $lang) { if (!isset($this->_local) || $lang) {
$tags = array( $tag = $this->getTranslateTag('title:body');
$this->getTranslateTag('body'), $this->_local = CustomDataTranslation::allTranslations($tag, 'article', $lang);
$this->getTranslateTag('article'),
);
if (!$lang)
$lang = Internationalization::getCurrentLanguage();
$this->_local = CustomDataTranslation::allTranslations($tags, 'article', $lang);
} }
return $this->_local; return $this->_local;
} }
function _getLocal($what, $lang=false) { function _getLocal($what, $lang=false) {
$tag = $this->getTranslateTag($what); if (!$lang)
foreach ($this->_fetchTranslation($lang) as $t) $lang = Internationalization::getCurrentLanguage();
if ($tag == $t->object_hash) foreach ($this->_fetchTranslation($lang) as $t) {
return $t->text; if ($lang == $t->lang) {
list($title, $body) = explode("\x04", $t->text, 2);
return $what == 'body' ? $body : $title;
}
}
return $this->ht[$what]; return $this->ht[$what];
} }
...@@ -324,45 +323,44 @@ class Page { ...@@ -324,45 +323,44 @@ class Page {
function saveTranslations($vars, &$errors) { function saveTranslations($vars, &$errors) {
global $thisstaff; global $thisstaff;
$tags = array( $tag = $this->getTranslateTag('title:body');
'body' => $this->getTranslateTag('body'), $translations = CustomDataTranslation::allTranslations($tag, 'article');
'title' => $this->getTranslateTag('title'),
$this->getTranslateTag('body') => 'body',
$this->getTranslateTag('title') => 'title',
);
$translations = CustomDataTranslation::allTranslations(array_values($tags), 'article');
foreach ($translations as $t) { foreach ($translations as $t) {
$content = @$vars['trans'][$t->lang][$tags[$t->object_hash]]; $title = @$vars['trans'][$t->lang]['title'];
if (!$content) $body = @$vars['trans'][$t->lang]['body'];
if (!$title && !$body)
continue; continue;
$content = Format::sanitize($content);
// Content is not new and shouldn't be added below // Content is not new and shouldn't be added below
unset($vars['trans'][$t->lang][$tags[$t->object_hash]]); unset($vars['trans'][$t->lang]['title']);
unset($vars['trans'][$t->lang]['body']);
$content = $title . "\x04" . Format::sanitize($body);
if ($content == $t->text) if ($content == $t->text)
continue; continue;
$t->text = $content; $t->text = $content;
$t->agent_id = $thisstaff->getId(); $t->agent_id = $thisstaff->getId();
$t->updated = SqlFunction::NOW();
if (!$t->save()) if (!$t->save())
return false; return false;
} }
// New translations (?) // New translations (?)
foreach ($vars['trans'] as $lang=>$parts) { foreach ($vars['trans'] as $lang=>$parts) {
foreach ($parts as $tag=>$content) { $title = @$parts['title'];
$content = Format::sanitize($content); $body = @$parts['body'];
if (!$content || !isset($tags[$tag])) $content = $title . "\x04" . Format::sanitize($body);
continue; if ($content == "\x04")
$t = CustomDataTranslation::create(array( continue;
'type' => 'article', $t = CustomDataTranslation::create(array(
'object_hash' => $tags[$tag], 'type' => 'article',
'lang' => $lang, 'object_hash' => $tag,
'text' => $content, 'lang' => $lang,
'revision' => 1, 'text' => $content,
'agent_id' => $thisstaff->getId(), 'revision' => 1,
'updated' => SqlFunction::NOW(), 'agent_id' => $thisstaff->getId(),
)); 'updated' => SqlFunction::NOW(),
if (!$t->save()) ));
return false; if (!$t->save())
} return false;
} }
return true; return true;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment