ZF-4085: wrong xml encoding detection mechanism in Zend_Translate_Adapter_Xliff throwing Zend_Translate_Exception: XML error: No error at line 0

Description

If you use the following header for xliff files: <?xml version='1.0' encoding='utf-8'?> instead of <?xml version="1.0" encoding="utf-8"?> (i.e. single quotes instead of double quotes for "enconding" attribute value) you end up with the following exception: Fatal error: Uncaught exception 'Zend_Translate_Exception' with message 'XML error: No error at line 0' in /home/remy/my_php/ZendFramework-1.5.3/library/Zend/Translate/Adapter/Xliff.php:97 Stack trace:

0 ZendFramework-1.5.3/library/Zend/Translate/Adapter.php(406): Zend_Translate_Adapter_Xliff->_loadTranslationData('../application/...', 'en', Array)

My xliff file has been generated by translate-toolkit (part of pootle project), using po2xliff python script. At first I thought that all attribute values had to be wrapped into double quotes (cf. http://bugs.locamotion.org/show_bug.cgi?id=492) but single quotes appear to be legal too (cf. http://www.w3.org/TR/REC-xml/#NT-EncodingDecl).

Error comes from Zend_Translate_Adapter_Xliff->_findEncoding() method which tries to extract xml encoding from xliff file, but expect encoding value to be wrapped using double-quotes: line 195: $encoding = substr($encoding, 0, strpos($encoding, '"'));

One solution could be "$encoding = substr($encoding, 0, min(strpos($encoding, '"'), strpos($encoding, "'")));" since no single quote is to be found in any valid encoding value.

Regards, Remy

Comments

Oups! my suggested solution does not work. (one of the two min() arguments may be false and then return 0). The following one works (it detects opening char (" or ') and look for corresponding closing one): [old] $encoding = substr($file, strpos($file, "encoding") + 10); $encoding = substr($encoding, 0, strpos($encoding, '"')); [new] $encoding = substr($file, strpos($file, "encoding") + 9); $encoding = substr($encoding, 1, strpos($encoding, $encoding[0]));

Regards, Remy

Fixed with r11088.

PS: Given solution code does not work.

thx. I saw it too, I attached a working solution as comment #1 Cheers

Both of your solutions do not work :-) Look at trunk.. your encoding is ALWAYS empty as you must add an offset of 1 to the strpos in the second line. Otherwise it will be substr() from 1 to 0 which is empty :-)

You're 100% right, shame on me :P

I forgot to add "1" as offset on my second strpos.

$encoding = substr($file, strpos($file, "encoding") + 9); $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1));

Thanks for your vigileance :) Remy

Updating for the 1.6.0 release.