i18n
i18n is the abbreviation for internationalization. Magento
adds i18n support out of the box, thus adapting to various languages and regions without application changes. Within app/functions.php
, there is a __()
translation function, which is defined as follows:
function __() { $argc = func_get_args(); $text = array_shift($argc); if (!empty($argc) && is_array($argc[0])) { $argc = $argc[0]; } return new \Magento\Framework\Phrase($text, $argc); }
This translation
function accepts a variable number of arguments and passes them to a constructor of the \Magento\Framework\Phrase
class and returns its instance. The Phrase
class has the __toString
method, which then returns the translated string.
Here are a few examples of how we can use the __()
function:
__('Translate me')
__('Var1 %1, Var2 %2, Var %3', time(), date('Y'), 32)
__('Copyright %1 <a href="%2">Magento</a>', date('Y'), 'http://magento.com')
Strings passed through the translation
function...