Warning: You are browsing the documentation for PrestaShop 8, which is outdated.

You might want to read an updated version of this page for the current version, PrestaShop 9. Read the updated version of this page

Learn how to update to the latest version.

The Locale component

The Locale component is in charge of formating numbers, prices and percentages.

It is accessible through the Context

It provides useful methods such as formatNumber() and formatPrice().

Initialization of Locale

The Locale is initialized with :

formatNumber() method

This method receives one parameter $number as int, float or string. It returns a formatted number, as a string, taking the NumberSpecification used by the locale, set during its initialization.

example of use

With a en-US locale,

$number = 1234.56;

var_dump(Context::getContext()->getCurrentLocale()->formatNumber($number));

Will dump:

string(8) "1,234.56"

formatPrice() method

This method receives two parameters, the first one $number as int, float or string, and the second one $currencyCode as string. It returns a formatted price, as a string, taking the matching PriceSpecification identified by the $currencyCode.

example of use

With a en-US locale, and a GBP currency code:

$price = 1234.56;

var_dump(Context::getContext()->getCurrentLocale()->formatPrice($price, 'GBP'));
// string(10) "£1,234.56"

With a en-US locale, and a EUR currency code,

$price = 1234.56;

var_dump(Context::getContext()->getCurrentLocale()->formatPrice($price, 'EUR'));
// string(10) "€1,234.56"