Categories


Archives


Recent Posts


Categories


Magento 2 Translation Function: Not sprintf compatible.

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Quick heads up if you’re using Magento 2’s __ function to translate strings in your extensions. Magento 1’s translation functions allowed you to use sprintf style replacement tokens.

Mage::helper('module')->__('Hello %s', $string);
$this->__('Hello %s', $string);    

Magento two opts to use a globally accesible __ function.

$translate_string = __('Hello World');    

This new function is not sprintf compatible. It does support replacement tokens, but those tokens are numerical based instead of type based.

$translate_string = __('Hello %1, how are %2', $string, $foo);    

Substitute your own treaties on the appropriateness of sprintf existing in a language that allows you to concatenate any two things together and get a string out the other side automatically.

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 12th January 2016