I made a small Kotlin Multiplatform library that a...
# feed
j
I made a small Kotlin Multiplatform library that allows formatting relative dates and filesizes. It’s called HumanReadable and it’s available on GitHub: https://github.com/jacobras/Human-Readable. It has translations for 11 languages in the current release (it uses Libres internally for multiplatform strings). It works with KotlinX DateTime.
🎉 14
👍🏾 1
👍🏽 1
👍🏻 2
👍🏼 1
K 7
👍 15
f
Nice! Very useful 😊
j
Thanks! Yeah, I needed it myself so there you go 😅
f
How does it determine the ‘correct’ output? For example, 544 hours is shown as ‘23 days’ in your example, but ‘3 weeks’ might also be ~ correct ish
j
I will add that to the readme! It currently eagerly switches: if it fits in days then it goes with days, as soon as a week is passed it goes into week and so forth.
f
So basically, it looks for the largest ‘container’ it completely fills? min, hour, day, week, month, etc
j
Correct. I'd like to make that configurable, though.
👍 1
I'm also thinking if it would be useful to have a configurable "overlap point". For example:
8.days
is a week ago but for
11.days
I already switch to "2 weeks" because of rounding. In minutes that doesn't happen:
59.minutes
results in "59 minutes", not yet "1 hour". That's inconsistent. I saw a similar library in another language switch on 75%: so
44.minutes
= "44 minutes" but then
45.minutes
becomes "1 hour".
f
Yeah that seems more human readable, but less precise. A ‘precise-ness’ parameter could help 🙂
j
This .NET one actually calls it
precision
🤓 https://github.com/Humanizr/Humanizer#humanize-datetime
👍 1
f
Ah yes of course! 😅 (English is not my first language)
d
Is there a way to add translation to other languages?
j
Thanks for the feedback @Frank Bouwens! 🎉 I added docs about the precision https://github.com/jacobras/Human-Readable?tab=readme-ov-file#datetime-precision
kodee happy 1
K 1
🎉 1
🎉 1
k
Hi! Could you share with me how the resources work when I depend on the library?
j
> Is there a way to add translation to other languages? @dave08 sure, you can let me know which one you're missing or follow the instructions I just added: https://github.com/jacobras/Human-Readable?#adding-a-language 🙂
👍🏼 1
k
How were localized strings bundled to the final app?
I suspect it shouldn't work
I'm checking.... very interesting
j
@Konstantin Tskhovrebov nice to see you, I attended your talk recently at the Kotlin NL meetup 🙂 I use the small Libres library for multiplatform strings. It grabs the XML files with KMP and turns them into code. I like it a lot. Here's an example of what it generates:
k
Oh! I see! Cool 🙂 so, instead the resources there is generated code! 🚀
j
Exactly, that's why I like it. No issues with reading XML files on runtime, just code that works on several platforms. Before I found it, I had a very similar class in the library, which I wrote manually. Lots of hassle, a lot easier to do it this way. Plus it has better locale detection (uses the platform on Android & iOS I think, and on web you can set it yourself)
k
@Pamela Hill ☝️ Libres works. it is not real resources as we know and all is saved in a final binary but anyway
j
Shout-out to its creator @Skeptick , who's also in this Slack it seems 🙂
k
I think there's a small error in the readme:
Copy code
HumanReadable.fileSize(20_411_000_000, decimals = 2) // "20.44 GB"
Shouldn't that be "19.01 GB"? Also, I think it would be nice to have an option for binary or decimal base (if set to decimal, then use "GiB" for gibibytes and "GB" for decimal gigabytes; if set to binary, use "GB" for gibibytes and no support for decimal gigabytes)
j
Woops, error in the readme indeed. Fixed, thanks! Will think about GiB vs GB, not sure what users are expecting/needing most.
m
@Klitos Kyriacou it's exactly the opposite of what you say. GB has a base of 1000, whereas GiB has a base of 1024. Windows IIRC uses a base of 1024 but use for that GB. MacOS for a long time used GiB, however recently switched to GB, although the base remained 1024. On Linux you often see GiB with a base of 1024. (own experience + wiki: https://en.wikipedia.org/wiki/Gigabyte) However, I agree that it would be useful to be able to influence this via the parameter
d
Or maybe even better -- to allow the user to override from where to take the translations... not everybody is working in KMM, so maybe an abstraction to provide the translations and making LIbres an optional module for those that want that out-of-the box, could be pretty nice!
k
@Miroslav Sobotka I didn't mean the exact opposite. I meant it like this:
Option = binary -> "GB" means gibibyte
Option = decimal -> "GB" means gigabyte and "GiB" means gibibyte
So, the option controls whether to use base 1024 (binary) or 1000 (decimal) for the meaning of "GB".
Though I do like Dave's idea of allowing the user to control where to take the translations; then we could provide our own translation and choose whether to call it "GB" or "GiB".
k
it may be extended though
f
There’s a PR open for that issue, by @Jacob Ras himself 😉
👍 1
j
I'm aware as Frank shows 🙂 I also opened this one to have a more sensible fallback: https://github.com/Skeptick/libres/pull/54 and in HumanReadable I have extensions for all the languages it supports: https://github.com/jacobras/Human-Readable/blob/main/src/jsMain/kotlin/nl/jacobras/humanreadable/extendLibresPlurals.kt