user11982170
user11982170

Reputation:

Pluralization in vue i18n

Hi i am trying to pluralize based on https://kazupon.github.io/vue-i18n/guide/pluralization.html

imageCount== 1
          ? $t("message.imageMessage", 1, { imageCount})
          : $t("message.imageMessage", imageCount, {
              imageCount
            })



imageMessage: '{imageCount} image downloaded | {imageCount} images downloaded'

Problem : currently it is displaying bo the messages which should not happen,is there anything wrong in the way which i have implemented?

enter image description here

Codesandbox: https://codesandbox.io/s/lingering-haze-z9jzt?file=/src/components/HelloWorld.vue

Upvotes: 6

Views: 7441

Answers (2)

pegiadise
pegiadise

Reputation: 689

With vue@3 and vue-i18n@9 and Composition API, with locale key being:

{
  message: {
    imageMessage: "no images | 1 image downloaded | {n} images downloaded"
  }
}

It's enough to do:

$t('message.imageMessage', { n: images.length })
// OR 
$t('message.imageMessage', images.length)

Upvotes: 5

Phil
Phil

Reputation: 164901

From the documentation...

Your template will need to use $tc() instead of $t().


You can also improve / shorten your code somewhat by using {n} or {count} in your translation strings...

en: {
  message: {
    imageMessage: "{n} image downloaded | {n} images downloaded"
  }
}

and in your templates

$tc("message.imageMessage", imageCount)

Upvotes: 9

Related Questions