symfony/symfony — pull request #3842 by eriksencosta (2012-04-09 02:55:51+00:00)
Title: [RFC][WIP] filters and helpers for localized formatting

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: documentation, implementation of number formatting

[![Build Status](https://secure.travis-ci.org/eriksencosta/symfony.png?branch=locale-dev)](http://travis-ci.org/eriksencosta/symfony)

symfony 1 had some [useful helpers](http://www.symfony-project.org/gentle-introduction/1_4/en/13-I18n-and-L10n#chapter_13_sub_outputting_data_in_the_user_s_culture) for localization. This RFC proposes the porting of some of those helpers to Symfony2. It's a simple feature that I missed and that I think is simple yes helpful. The [bcc/extra-tools-bundle](https://github.com/michelsalib/BCCExtraToolsBundle) have a Twig filter for date formatting and another for country display (but none for the PHP engine).

This initial implementation includes the formatters as Twig filters and PHP helpers:

- `formatCurrency`
- `formatDate`
- `formatTime`
- `formatDateTime`

This is a WIP and I want some feedback to make the API cleaner and to improve the naming (if this RFC get approved). I implemented the filters and helpers with every parameter hardcoded in the methods' signatures. I made this choice to maintain consistent with the others filters and helpers implementations. This made the date/time a little bit hard to use but this is easy to refactor:

// this (none to: dateStyle, timeStyle, calendar and timeZone - in this order)
date|formatDateTime(none, none, none, none, 'yyyy.MM.dd HH:mm:ss a', 'fr_FR')

// could become:
date|formatDateTime({ 'pattern' : 'yyyy.MM.dd HH:mm:ss a' }, 'fr_FR')

// or:
date|formatDateTime('fr_FR', { 'pattern' : 'yyyy.MM.dd HH:mm:ss a' })

Another thing I am not sure is about the service configuration. The `twig.extension.locale` depends on a service defined in FrameworkBundle (like `twig.extension.trans`) but the TwigBundle does not declare FrameworkBundle as a dependency. Also, naming deserves some love here too.

Examples:
--

Controller:

```php
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class LocaleController extends Controller
{
/**
* @Route("/locale/{format}", defaults={ "format"="twig" })
*/
public function indexAction($format)
{
return $this->render(
'VendorSomeBundle:Locale:index.html.'.$format,
array('date' => new \DateTime('2012-07-10'), 'amount' => 125.50)
);
}
}
```

Twig example:

```html

Currencies

Default

Using app global currency: {{ amount|formatCurrency() }}

BRL currency: {{ amount|formatCurrency('BRL') }}

Locale

Using app global currency: {{ amount|formatCurrency(none, 'pt_BR') }}

BRL currency: {{ amount|formatCurrency('BRL', 'pt_BR') }}

Dates

Default

date: {{ date|formatDate() }}

time: {{ date|formatTime() }}

datetime: {{ date|formatDateTime() }}

Date/time style

date: {{ date|formatDate('full') }}

time: {{ date|formatTime('full') }}

datetime: {{ date|formatDateTime('full', 'full') }}

Timezone

date: {{ date|formatDate(none, 'Europe/London') }}

time: {{ date|formatTime(none, 'Europe/London') }}

datetime: {{ date|formatDateTime(none, none, 'Europe/London') }}

Calendar

date: {{ date|formatDate(none, none, 'traditional') }}

time: {{ date|formatTime(none, none, 'traditional') }}

datetime: {{ date|formatDateTime(none, none, none, 'traditional') }}

Pattern

date: {{ date|formatDate(none, none, none, 'HH:mm:ss a') }}

time: {{ date|formatTime(none, none, none, 'yyyy.MM.dd') }}

datetime: {{ date|formatDateTime(none, none, none, none, 'yyyy.MM.dd HH:mm:ss a') }}

```

PHP example:
--

```php

Currencies

Default

config: formatCurrency($amount) ?>

BRL: formatCurrency($amount, 'BRL') ?>

Locale

config: formatCurrency($amount, null, 'pt_BR') ?>

BRL: formatCurrency($amount, 'BRL', 'pt_BR') ?>

Dates

Default

date: formatDate($date) ?>

time: formatTime($date) ?>

datetime: formatDateTime($date) ?>

Full style

date: formatDate($date, 'full') ?>

time: formatTime($date, 'full') ?>

datetime: formatDateTime($date, 'full', 'full') ?>

Timezone

date: formatDate($date, null, 'Europe/London') ?>

time: formatTime($date, null, 'Europe/London') ?>

datetime: formatDateTime($date, null, null, 'Europe/London') ?>

Calendar

date: formatDate($date, null, null, 'traditional') ?>

time: formatTime($date, null, null, 'traditional') ?>

datetime: formatDateTime($date, null, null, null, 'traditional') ?>

Pattern

date: formatDate($date, null, null, null, 'HH:mm:ss a') ?>

time: formatTime($date, null, null, null, 'yyyy.MM.dd') ?>

datetime: formatDateTime($date, null, null, null, null, 'yyyy.MM.dd HH:mm:ss a') ?>

```