fluent-php icon indicating copy to clipboard operation
fluent-php copied to clipboard

DATETIME and NUMBER functions

Open tobyzerner opened this issue 4 years ago • 14 comments

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.NumberFormat and Intl.DateTimeFormat, and thus the full API for the built-in functions cannot be supported. Specifically:
    • In NUMBER, the currencyDisplay option is not supported.
    • In DATETIME, no options are supported except for timeZone. However, the experimental dateStyle and timeStyle options 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>

tobyzerner avatar Aug 22 '21 22:08 tobyzerner

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 :)

jrmajor avatar Aug 23 '21 18:08 jrmajor

@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.

jrmajor avatar Sep 20 '21 22:09 jrmajor

Awesome! Very ambitious - thank you for putting in the work.

tobyzerner avatar Sep 25 '21 03:09 tobyzerner

Hi @jrmajor, thanks for your work so far! Can I bother you for a little progress update?

tobyzerner avatar Jan 25 '22 09:01 tobyzerner

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 :).

jrmajor avatar Jan 26 '22 01:01 jrmajor

Hey @jrmajor - do you have any thoughts about support for partially-formatted variables?

tobyzerner avatar Feb 22 '22 22:02 tobyzerner

@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]));

jrmajor avatar Feb 24 '22 11:02 jrmajor

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.

jrmajor avatar May 11 '23 19:05 jrmajor

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.

tobyzerner avatar May 27 '23 04:05 tobyzerner

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.

jrmajor avatar Aug 19 '23 11:08 jrmajor

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.

tobyzerner avatar Aug 19 '23 11:08 tobyzerner

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 avatar Aug 22 '23 17:08 ramsey

@ramsey A self-hosted app, meaning that having a hard dependency on a non-default PHP extension probably isn't a good idea, unfortunately 😔

tobyzerner avatar Aug 22 '23 23:08 tobyzerner

Understood. There might be some interest in making it part of the core, so we'll see. 😄

ramsey avatar Aug 23 '23 23:08 ramsey