DATETIME and NUMBER functions
Hey @jrmajor, thanks again for your amazing work on this library.
Almost 2 years ago I began my own fluent-php implementation, which was basically a 1:1 port of the JS implementation. It was untested and I never published it or used it in production, so I'm glad you started work on this one which appears to have a much improved architecture and more intent to maintain than I ever had.
However, my implementation did have some additional functionality – specifically, I had implementations of the NUMBER and DATETIME builtin functions. I wanted to share the source code with you now in hope that it can be of use to you.
https://github.com/tobyzerner/fluent-php
Note that the function implementations have a number of caveats, documented in the README:
- PHP includes the NumberFormatter and IntlDateFormatter classes, but these lack some of the functionality of
Intl.NumberFormatandIntl.DateTimeFormat, and thus the full API for the built-in functions cannot be supported. Specifically:
- In
NUMBER, thecurrencyDisplayoption is not supported.- In
DATETIME, no options are supported except fortimeZone. However, the experimentaldateStyleandtimeStyleoptions are supported.
But still, I think they're better than nothing. Fluent isn't nearly as useful without them.
On the side, it also had an implementation of DOM Overlays from fluent-dom (also in the form of a 1:1 port), which may also be useful:
Overlay::translateHtml(
'<p><img data-l10n-name="world" src="world.png"></p>',
[
'value' => 'Hello, <img data-l10n-name="world" alt="world">!',
'attributes' => ['title' => 'Hello']
]
);
// → <p title="Hello">Hello, <img data-l10n-name="world" alt="world" src="world.png">!</p>
Thank you for sharing your implementation! I'll try to find some time to work on built-in functions soon. I will also think about DOM overlay, but first I want to tag v1 :)
@tobyzerner I want NUMBER and DATETIME implementations to be complete, so I need to recreate Intl.NumberFormat and Intl.DateTimeFormat in PHP. I'm still working on it, but it will probably take some time.
Awesome! Very ambitious - thank you for putting in the work.
Hi @jrmajor, thanks for your work so far! Can I bother you for a little progress update?
Sure! I've just released https://github.com/jrmajor/fluent-php/releases/tag/v0.4.0 and https://github.com/jrmajor/laravel-fluent/releases/tag/v0.1.4 with NUMBER() support. DATETIME() seems even more complex — I'll see what I can do about it :).
Hey @jrmajor - do you have any thoughts about support for partially-formatted variables?
@tobyzerner They're already supported (added missing test in 482a0c8280a7def44f40349608a5bb6ae6c04a2a):
$bundle = (new FluentBundle('en-US'))->addFtl('test = { NUMBER($arg, minimumFractionDigits: 1) }');
// This will return '12345.0', respecting both minimumFractionDigits and useGrouping options.
$bundle->message('test', arg: (new FluentNumber(12345))->setOptions(['useGrouping' => false]));
After a few failed attempts, I decided to give up on the DATETIME() support until some PHP library with JS-compatible date formatter API comes up. For now, I would recommend providing a simplified replacement function based on IntlDateFormatter.
@tobyzerner, I'm sorry.
No worries @jrmajor! Seems like an extremely difficult thing to build.
FYI I have just launched my new project using this lib. Thanks for making it possible to use Fluent in PHP. For dates, mostly I am using github/relative-time-element anyway, otherwise I am using Carbon's isoFormat and passing the result into Fluent as a text argument. It's not quite as powerful for translators but works well enough for now.
Hey @tobyzerner, recently @ramsey started working on https://github.com/php-ecma-intl/ext, which would allow me to implement DATETIME(). I'm unsure whether it will work for your particular use case, as you may not want to introduce such dependency to a self-hosted product. Anyway, I'll see what can be done as soon as he tags a release.
Thanks for letting me know! As you say, this probably won't be suitable for my use-case, but it's good to know that this is being worked on.
Hi! 👋🏻
I'm developing ecma_intl to be a 1:1 port of ECMA-402 to PHP.
@jrmajor, the first tagged release will be a 0.1.0 version and will only include the Locale class and supporting features. Later releases will follow that include datetime and number formatting, etc.
@tobyzerner, what's your use-case?
@ramsey A self-hosted app, meaning that having a hard dependency on a non-default PHP extension probably isn't a good idea, unfortunately 😔
Understood. There might be some interest in making it part of the core, so we'll see. 😄