5

I keep seeing the following report in my system.log file:

2015-10-20T12:34:07+00:00 DEBUG (7): Array
(
    [type] => 2
    [message] => vsprintf(): Too few arguments
    [file] => app\code\core\Mage\Core\Model\Translate.php
    [line] => 416
)

I suspect it comes from the extension I wrote but I'm not sure. what's the best way to debug this? how do I know where it comes from? looking at the code at Translate.php doesn't help much.

5

As the other answer already stated, this is probably caused by too few arguments in a call to a translator method.

You have more %d or %s placeholders in the translations string then arguments passed additional to the string.

You should create a copy of app/code/core/Mage/Core/Model/Translate.php in app/code/local/Mage/Core/Model/Translate.php and add a temporary debug call there in the translate() method just above the vsprintf($translated, $args) call.

You could add something like this:

if (substr_count($translated, '%') != count($args)) {
    Mage::log(
        __METHOD__ . '() possible argument count mismatch! '
        . 'Backtrace: ' . mageDebugBacktrace(true, false, true)
    );
}

Logs will be written to var/log/system.log when logging is enabled (and the log file is still the default system.log).

It's a simple check, so it would also appear when you have a translate string containing an escaped percentage character, like "You get 5%% discount!"

You should not edit the core file directly, but if you do then please do not forget to revert this core file to it's original state afterwards! Or else Ben Marks will be mad!

  • @Ness did you find where it was coming from? – 7ochem Oct 22 '15 at 14:18
  • nope, but it was enough to find out it comes from another extension and not mine, so I don't really care :) your answer was very helpful, thanks! – Ronen Ness Oct 22 '15 at 16:03
  • You should NOT edit app/code/core/Mage/Core/Model/Translate.php instead create a copy of Translate.php in app/code/local/Mage/Core/Model/Translate.php and edit it there. – Black Jun 17 '18 at 13:18
  • @Black it's temporary. Most of the times in Magento 1 projects I would have the core code also in VCS. So it will be really easy to see core changes and revert them with a simple git checkout -- app/code/core/. Of course it's best to develop a habit of copying to app/code/local/. You'd see those files in your 'untracked' list when doing git status and you could just delete them again. You cannot easily see what has changed with git diff though, because the app/code/local/ copy will just be a complete new file to Git. – 7ochem Jun 18 '18 at 8:20
2

Hard to say without seeing the offending code, but I think you may have a translation call with insufficient number of parameters, something like:

echo $this->__('There are %d things in %s places', $arg);

Check your code for that.

  • yeah I know, but my point is I have no idea what the source of this error and this is what I'm asking - how do I find the source or any relevant information about it? thanks – Ronen Ness Oct 20 '15 at 13:08
  • 1
    Search your custom module for an unescaped %. That's my best guess. – benmarks Oct 20 '15 at 13:18
1

This is a bug in Magento Core. This bug can be fixed by escaping percent signs in app/code/core/Mage/Core/etc/jstranslator.xml (search for '%' and replace with '%%').

Then it's possible to remove the @ from @vsprintf in line 416 of app/code/core/Mage/Core/Translate.php to read: $result = vsprintf($translated, $args);

The @ sign there only hides the error in most places unless you use a set_error_handler function. Properly escaping the % in the xml will fix the error itself.

To test, you can add the validate-css-length class to a form field and type in an improper css unit of measurement and see that there is only one %.

I don't know how to suggest this as a patch, but it's been like this since at least 2015. You shouldn't edit the core files directly, but you can copy them from core to local and make the changes there.

1

Alrighty, here is the best answer based on the issue I encountered in Magento 1.9.3.8 after installing it fresh. Replace the vsprint line in the app\code\core\Mage\Core\Model\Translate.php:416 with:

try {
    $result = vsprintf($translated, $args);
} catch (Exception $e){
    echo $translated;
    exit;
}

This will print the issue string on the screen. For me it was this one: Please input a valid CSS-length. For example 100px or 77pt or 20em or .5ex or 50%.
Then you can search the code for the string and escape it if it's your code. If not, then you found a bug in someone else's code. Submit a bug report to them so they can fix it. In this case, this is in Mage_Core, so it's a Magento bug that was overlooked when releasing.

  • You should not edit the core file directly, create a copy in app/code/local/Mage/Core/Model/Translate.php and do whatever you like there! Otherwise you will loose upgrade compatibility. And also you are writing Exception $e but you never use $e. I tried your solution and nothing appears at all. – Black Jun 17 '18 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.