Ticket #476 (closed defect: fixed)
Problem when adding a new text (translate) with MessageSource_XLIFF
| Reported by: | letux | Owned by: | wei |
|---|---|---|---|
| Priority: | normal | Milestone: | 3.0.6 |
| Component: | Prado Framework v3 | Version: | 3.0 |
| Severity: | minor | Keywords: | |
| Cc: |
Description
The function save in the class MessageSource?_XLIFF should be modified. Indeed, the id in the XML file is not already right. Sometimes, we have more than one time the same id. [code] <trans-unit id="1"> <source>Add</source> <target/> </trans-unit>
<trans-unit id="3"> <source>Edit</source> <target/> </trans-unit>
<trans-unit id="3"> <source>New text</source> <target/> </trans-unit>/code
This couls appear when we delete one text in the XML file. The reason is the new id is compute with the number of text contain in th xml file and not withe the last id.
current function
[code]public function save($catalogue='messages') {
$messages = $this->untranslated; if(count($messages) <= 0) return false;
$variants = $this->getVariants($catalogue);
if($variants)
list($variant, $filename) = $variants;
else
list($variant, $filename) = $this->createMessageTemplate($catalogue);
if(is_writable($filename) == false)
throw new TIOException("Unable to save to file {$filename}, file must be writable.");
//create a new dom, import the existing xml $dom = DOMDocument::load($filename);
//find the body element $xpath = new DomXPath($dom);
$body = $xpath->query('//body')->item(0);
$count = $xpath->query('//trans-unit')->length;
//for each message add it to the XML file using DOM
foreach($messages as $message) {
$unit = $dom->createElement('trans-unit'); $unit->setAttribute('id',++$count);
$source = $dom->createElement('source', $message); $target = $dom->createElement('target',);
$unit->appendChild($dom->createTextNode("\n")); $unit->appendChild($source); $unit->appendChild($dom->createTextNode("\n")); $unit->appendChild($target); $unit->appendChild($dom->createTextNode("\n"));
$body->appendChild($dom->createTextNode("\n")); $body->appendChild($unit); $body->appendChild($dom->createTextNode("\n"));
}
$fileNode = $xpath->query('//file')->item(0); $fileNode->setAttribute('date', @date('Y-m-d\TH:i:s\Z'));
//save it and clear the cache for this variant $dom->save($filename);
if(!empty($this->cache))
$this->cache->clean($variant, $this->culture);
return true;
and could be something like that
[code]public function save($catalogue='messages') {
$messages = $this->untranslated; if(count($messages) <= 0) return false;
$variants = $this->getVariants($catalogue);
if($variants)
list($variant, $filename) = $variants;
else
list($variant, $filename) = $this->createMessageTemplate($catalogue);
if(is_writable($filename) == false)
throw new TIOException("Unable to save to file {$filename}, file must be writable.");
$xml = simplexml_load_file($filename); $translationUnit = $xml->xpath('//trans-unit'); $lastId = 0;
foreach($translationUnit as $unit)
$lastId = $unitid?;
$lastId += 0;
//create a new dom, import the existing xml $dom = DOMDocument::load($filename);
//find the body element $xpath = new DomXPath($dom);
$body = $xpath->query('//body')->item(0);
//for each message add it to the XML file using DOM
foreach($messages as $message) {
$unit = $dom->createElement('trans-unit'); $unit->setAttribute('id',++$lastId);
$source = $dom->createElement('source', $message); $target = $dom->createElement('target',);
$unit->appendChild($dom->createTextNode("\n")); $unit->appendChild($source); $unit->appendChild($dom->createTextNode("\n")); $unit->appendChild($target); $unit->appendChild($dom->createTextNode("\n"));
$body->appendChild($dom->createTextNode("\n")); $body->appendChild($unit); $body->appendChild($dom->createTextNode("\n"));
}
$fileNode = $xpath->query('//file')->item(0); $fileNode->setAttribute('date', @date('Y-m-d\TH:i:s\Z'));
//save it and clear the cache for this variant $dom->save($filename);
if(!empty($this->cache))
$this->cache->clean($variant, $this->culture);
return true;
