synacor / preact-i18n

Simple localization for Preact.
BSD 3-Clause "New" or "Revised" License
206 stars 18 forks source link

Having multiple plural expressions in one string #28

Closed chinchang closed 5 years ago

chinchang commented 6 years ago

Let's say I have this string: You have 2 oranges and 5 bananas. What do I provide in the plural prop when I have multiple numeric expressions to pluralize? Examples show passing only 1 value to plural prop.

pl12133 commented 5 years ago

@chinchang Good question, you can use the <Text> component with nested <Text> components as fields to accomplish this:

+ dictionary.json
{
  "fruits": {
    "youHave": "You have {{oranges}} and {{bananas}}",
    "oranges": {
      "none": "no oranges",
      "singular": "{{count}} orange",
      "plural": "{{count}} oranges"
    },
    "bananas": {
      "none": "no bananas",
      "singular": "{{count}} banana",
      "plural": "{{count}} bananas"
    },
  }
};
function HowManyFruits({ orangeCount, bananaCount }) {
  return (
    <Text
      id="fruits.youHave"
      fields={{
        oranges: <Text id="fruits.oranges" fields={{ count: orangeCount }} plural={orangeCount} />,
        bananas: <Text id="fruits.bananas" fields={{ count: bananaCount }} plural={bananaCount} />
      }}
    />
  );
}

You can play around with this example here: https://jsfiddle.net/kup94zmy/1/

I think that answers the question so I'll close this out, but let me know if this is still an issue.

donkeyDau commented 4 years ago

This is not working for me. I have:

{
  editEntity: "Edit '{{entityType}}'",
  entities: {
    CONS: 'Load'
   }
}
<Text id="editEntity" fields={{
    entityType: <Text id="entities.CONS" />,
}}
/>

But it results in: Edit '[object Object]'

pl12133 commented 4 years ago

@donkeyDau Please make sure you are using at least version 1.4.0 for this feature to work.

donkeyDau commented 4 years ago

Can't remember which version I was using when I wrote the comment. Switched to 2.1.2-preactx and it works like a charm now. Thank you @pl12133 Also for the hooks. It'll relief some pain :)

donkeyDau commented 4 years ago

Have to revert my answer: Still not working with 2.1.2-preactx in my case ... Output is stil Edit [object Object]

pl12133 commented 4 years ago

Looks like that is caused by the master branch being ahead of the preactX branch, once master is merged into preactX this should be resolved.

donkeyDau commented 4 years ago

Alright. I'm looking forward to this merge :)