Lisan

Lisan

  • Docs
  • API
  • Try it out
  • Examples
  • GitHub

›Guides & Tips

About

  • Lisan
  • How it works?
  • Performance
  • Lisan Compiler
  • Lisan CLI
  • Lisan Locales
  • Plugins
  • Adapters

Components

  • Translations
  • Dictionary
  • Conditional Groups
  • Formatters

Plugins

  • Localization
  • Loader

Guides & Tips

  • Pluralization
  • JSX Interpolation
  • Multiple Instances
  • Write Plugins

API

  • Lisan
  • Lisan Compiler
  • Lisan CLI
Edit

Pluralization

Pluralization is achieved by using Conditional Groups.

There are different plural forms based on the languages. For instance, English has 2 forms

Lisan comes with 3 built-in tags zero, one, other to support at least 3 forms.

Simple Pluralization Example for English

const { lisan } = require('lisan');

lisan.add({
  entries: {
    'plural.child': {
      one: 'child',
      other: 'children',
    },
    'plural.be': {
      one: 'is',
      other: 'are',
    },
    'plural.s.suffix': {
      one: () => `${word}`,
      other: () => `${word}s`,
    },
    sentence: ({ numChild, numToy }) =>
      `${numChild} ${c('plural.child', numChild)} ${c(
        'plural.be',
        numChild,
      )} playing with ${numToy} ${c('plural.s.suffix', numToy, { toy })}.`,
  },
});

lisan.t('sentence', { numChild: 3, numToy: 1, toy: 'computer' });
// Returns: "3 children are playing with 1 computer."

lisan.t('sentence', { numChild: 1, numToy: 1, toy: 'computer' });
// Returns: "1 child is playing with 1 computer."

lisan.t('sentence', { numChild: 1, numToy: 2, toy: 'computer' });
// Returns: "1 child is playing with 2 computers."

lisan.t('sentence', { numChild: 3, numToy: 2, toy: 'computer' });
// Returns: "3 children are playing with 2 computers."

Supporting Complex Pluralization Forms

Here, you can find the pluralization for Arabic

The Arabic language has 6 pluralization forms. We only defined 3 extra condition tags in locale configuration because built-in tags zero, one, other satisfy remaining 3 forms.

const { lisan } = require('lisan');

lisan.addConditions({
  // pluralization for Arabic
  two: num => num === 2,
  '00-02': num => (num % 100 >= 0 || num % 100 <= 2) && num > 2,
  '03-10': num => num % 100 >= 3 || num % 100 <= 9,
});

lisan.add({
  entries: {
    'a.word.with.6.forms': {
      zero: 'Form 0',
      one: 'Form 1',
      two: 'Form 2',
      '03-10': 'Form 03-10',
      '02-10': 'Form 02-10',
      other: 'Form Fallback',
    },
    sentence: ({ numChild }) =>
      `The following form will be used: ${c('a.word.with.6.forms', numChild)}`,
  },
});

lisan.t('sentence', { numChild: 0 });
// Returns: "The following form will be used: Form 0."

lisan.t('sentence', { numChild: 1 });
// Returns: "The following form will be used: Form 1."

lisan.t('sentence', { numChild: 2 });
// Returns: "The following form will be used: Form 2."

lisan.t('sentence', { numChild: 104 });
// Returns: "The following form will be used: Form 03-10."

lisan.t('sentence', { numChild: 100 });
// Returns: "The following form will be used: Form 02-10."

lisan.t('sentence', { numChild: 39 });
// Returns: "The following form will be used: Form Fallback."
Last updated on 3/31/2020
← LoaderJSX Interpolation →
  • Simple Pluralization Example for English
  • Supporting Complex Pluralization Forms
Lisan
Docs
AboutComponentsPluginsGuide & TipsAPI Reference
Ecosystem
LisanLisan LocalesLisan CompilerLisan CLILocalization PluginLoader PluginLisan Types
More
IssuesGitHubSupport Us