Hello Internals,
I would like to propose an addition to the intl extension,
IntlDateTimePatternGenerator.
ICU exposes the DateTimePatternGenerator class that allows generating a
formatting pattern from a given "skeleton" for a given locale. (see
https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/classicu_1_1DateTimePatternGenerator.html)
This allows more flexibility than the current $format argument of
IntlDateFormatter::format(), which takes either a few pre-defined constants or
a hard-coded format.
This functionality also has been requested in the past. (see
https://bugs.php.net/bug.php?id=70377)
For example, if an application requires a format that will always use the long
version of a year, but the short version of a month (eg. "MM/dd/YYYY" for
"en_US", or "dd.MM.YYYY" for "de_DE"), one is out of luck.
IntlDateFormatter::SHORT will use the short year for "de_DE" ("dd.MM.YY"), and
IntlDateFormatter::MEDIUM will use the long version of a month for "en_US"
("MMM dd, YYYY").
With IntlDateTimePatternGenerator::getBestPattern(), a skeleton can be provided
to generate the appropriate pattern for a given locale.
In the use-case given above, the skeleton "YYYYMMdd" will generate the desired
patterns for each locale, which can then be passed to
IntlDateFormatter::format().
Proposed Signature:
```
class IntlDateTimePatternGenerator
{
public function __construct(?string $locale = null) {}
public static function create(?string $locale = null):
?IntlDateTimePatternGenerator {}
public function getBestPattern(string $skeleton): string|false {}
}
```
Example use-case:
```
$date = new \DateTime();
$skeleton = "YYYYMMdd";
$dtpg = new \IntlDateTimePatternGenerator(); // uses default locale if not
specified
$format = $dtpg->getBestPattern($skeleton);
echo \IntlDateFormatter::formatObject($date, $format); // outputs "28.03.2020"
for "de_DE" locale
```
The proposed implementation can be found here:
https://github.com/php/php-src/pull/6771
I assume that this will require an RFC, in which case I request the karma to
create one. My wiki account is deltragon.
Regards,
Mel Dafert
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php