robisim74 / qwik-speak

Translate your Qwik apps into any language
https://robisim74.gitbook.io/qwik-speak/
MIT License
133 stars 15 forks source link

Support for Zero case in plural #120

Closed juanpmarin closed 8 months ago

juanpmarin commented 8 months ago

Hi!

I have one use case that would love to solve with qwik-speak.

For a list item I'm showing the days since it was created, I want to display it like this:

0 days: "Today"` 1 day: "A day ago"

1 days: "34 days ago"

Is it possible to achieve this with qwik-speak?

robisim74 commented 8 months ago

Maybe you are looking for relative times? https://github.com/robisim74/qwik-speak/blob/main/docs/translate.md#userelativetime

E.g.:

const rt = useRelativeTime();

      <p>{rt(-0, 'day', {numeric: 'auto'})}</p> {/* today */}
      <p>{rt(-1, 'day', {numeric: 'auto'})}</p> {/* yesterday */}
      <p>{rt(-2, 'day', {numeric: 'auto'})}</p> {/* 2 days ago */}

The Plural function also uses Intl, so if you want to use the plural you have to handle the zero value yourself:

      {!count.value && t('zero')}
      {count.value > 0 && p(count.value, 'devs')}

That's because only some locales support zero cardinal. More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules

juanpmarin commented 8 months ago

@robisim74 I think that solves it, thanks!