Shopify / quilt

A loosely related set of packages for JavaScript/TypeScript projects at Shopify
MIT License
1.7k stars 220 forks source link

[react-i18n] Handle explicit "0" and "1" pluralization keys #2315

Closed trishrempel closed 1 year ago

trishrempel commented 2 years ago

Overview

As described in the Common Locale Data Repository (CLDR), add support for explicit "0" and "1" pluralization keys.

Usage example: In English (which only defines language-specific rules for “one” and “other”) this can be used to have special behavior for 0:

Typically, this is achieved by having completely separate keys for the explicit "0" and "1" use cases:

{
  "zeroProducts": "No products",
  "products": {
    "one": "{count} product",
    "other": "{count} products",
  }
}

This would then have to be handled explicitly in code:

const label = products.length === 0 ? i18n.translate('zeroProducts') : i18n.t('products', { count: products.length });

Once the explicit handling of the "0" and "1" case is complete, the key definition becomes simpler:

{
  "products": {
    "0": "No products",
    "one": "{count} product",
    "other": "{count} products",
  }
}

The implementation in code also becomes simpler:

const label = i18n.t('products', { count: products.length });

Type

Motivation

What inspired this feature request? What problems were you facing, or what else makes you think this should be in quilt?

Provide simpler implementation of common pluralization use cases.

Checklist

trishrempel commented 1 year ago

Solved by https://github.com/Shopify/quilt/pull/2539