Pluralization
Estimated reading time: 3 minutes
Typed-locale provides built-in support for pluralization, allowing you to handle different forms of a phrase based on a count. This is particularly useful for languages that have different rules for singular and plural forms.
Using the plural
Function
To define a pluralized phrase, use the plural
function in your translation object:
The plural
function accepts an object with the following properties:
0
(optional): Used when the count is zero.1
(optional): Used when the count is one.other
(required): Used for all other cases.
Using Pluralized Translations
When using a pluralized translation, you need to provide a count
parameter:
Type Safety
The pluralization system in typed-locale is fully type-safe. TypeScript will ensure that you provide the required count
parameter and will prevent you from using non-existent keys.
Pluralization with Other Variables
You can combine pluralization with other variables in your translations:
Handling Complex Pluralization Rules
While the basic 0
, 1
, and other
cases cover many languages, some languages have more complex pluralization rules. For these cases, you might need to use the other
case and handle the specific rules in your application logic.
For example:
This approach allows you to handle complex pluralization rules while still benefiting from typed-locale's type safety and structure.