Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATETIME and NUMBER functions #2

Open
tobyzerner opened this issue Aug 22, 2021 · 14 comments · Fixed by #3
Open

DATETIME and NUMBER functions #2

tobyzerner opened this issue Aug 22, 2021 · 14 comments · Fixed by #3
Labels
enhancement New feature or request

Comments

@tobyzerner
Copy link

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>
@jrmajor
Copy link
Owner

jrmajor commented Aug 23, 2021

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 jrmajor added the enhancement New feature or request label Aug 23, 2021
@jrmajor
Copy link
Owner

jrmajor commented Sep 20, 2021

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

@tobyzerner
Copy link
Author

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

@tobyzerner
Copy link
Author

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

@jrmajor
Copy link
Owner

jrmajor commented Jan 26, 2022

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

@tobyzerner
Copy link
Author

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

@jrmajor
Copy link
Owner

jrmajor commented Feb 24, 2022

@tobyzerner They're already supported (added missing test in 482a0c8):

$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
Copy link
Owner

jrmajor commented May 11, 2023

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 jrmajor closed this as completed May 11, 2023
@tobyzerner
Copy link
Author

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.

@jrmajor
Copy link
Owner

jrmajor commented Aug 19, 2023

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 jrmajor reopened this Aug 19, 2023
@tobyzerner
Copy link
Author

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.

@ramsey
Copy link

ramsey commented Aug 22, 2023

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?

@tobyzerner
Copy link
Author

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

@ramsey
Copy link

ramsey commented Aug 23, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants