BlueM
BlueM

Reputation: 3861

Sf2 “money” field type: decimal separator and locale

I have a Symfony application that I develop on OS X and deploy to Debian Jessie, both running PHP 5.6.12. The Symfony (standard edition) version is the same on both systems: 2.7.3. The translator service is not enabled, the default_locale is set to de (German) on both systems, and there are no configuration differences in respect to I18n or locale handling.

In this application, there is a form that uses the “money” field type, with the currency set to EUR. On OS X, the field value is displayed with comma as decimal separator (as it is common in German) and with the “€” symbol after the field. On Debian Jessie, the decimal separator is a dot, and the “€” symbol is displayed left to the field. This behavior is the same for clients sending different “Accept-Language” request headers.

My questions are:

Upvotes: 2

Views: 1792

Answers (1)

Jakob Alexander Eichler
Jakob Alexander Eichler

Reputation: 3056

You have to make sure that the php-intl extension is installed.

Because the Symfony Intl Component, which is a replacement layer for a missing php-intl extension only supports the locale: 'en', which you can see in their php files.

public function __construct($locale = 'en', $style = null, $pattern = null)
{
    if ('en' !== $locale && null !== $locale) {
        throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the locale "en" is supported');
    }
    ....
}

And also because an exception is thrown, if you try to call the following code.

\Locale::setDefault('de_DE');

The Symfony\Component\Intl\Locale\Locale::setDefault() is not implemented. Please install the "intl" extension for full localization capabilities.

500 Internal Server Error - MethodNotImplementedException

As the answer in the above linked Stackoverflow question states, you have to install php-intl. For me on Ubuntu somehow, that PHP-5 and PHP-7 is available was a problem. PHP5-intl was installed but not PHP7-intl. You should make sure by calling phpinfo(); that the extension is really loaded.

My answer assumes, that you have a locale configured in symfony, as I have done in the following lines:

framework:
    translator:      { fallbacks: ["%locale%", 'en'] }
    default_locale: de

The twig derivate in the symfony configuration also supports the following (for our issue unneccessary) entries:

date:
    format: d.m.Y, H:i:s
    interval_format: '%%d Tage'   
    timezone: Europe/Paris
number_format:
    decimals: 2
    decimal_point: ','
    thousands_separator: '.'

Upvotes: 3

Related Questions