Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a date interval to text (internationalized)

Let's say I have a DateInterval, like "P10DT1H53S", and I want to format it to a human-readable string, like :

10 days, 1 hour and 53 seconds

Now, this is fairly easy if you only plan to do it for English. But I am looking for a clean and internationalized way to do that, using gettext.

Unfortunately, gettext only handles one plural at a time, not multiple (years, days, hours etc.), so I cannot use ngettext to achieve what I want. And plurals differ with languages greatly, so it's not as simple as iterating and adding "s" where needed. Also, I must build it as a whole, because I'm not sure every language would separate the days, hours, … using commas (with one "and" instead of the last comma).

like image 733
Artefact2 Avatar asked Dec 14 '25 14:12

Artefact2


1 Answers

The best repository for up-to-date internationalization data is the CLDR project, hosted by the Unicode Consortium. Locale-specific pluralization rules are part of the data set.

The repository data is available in a bunch of XML files. There are a few tools for manipulating the data, including code to generate POSIX-compatible locale files.

I'm not sure how much work you'd need to do to use the data in PHP (not my speciality), but it would probably be worth it in the long run.

like image 188
devstuff Avatar answered Dec 16 '25 07:12

devstuff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!