Skip to content

Latest commit

 

History

History

macro

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

@bluecateng/l10n.macro

L10n macros.

Installation

npm i -D @bluecateng/l10n.macro babel-plugin-macros

Usage

Babel Configuration

To enable the macros plugin it should be added to the Babel configuration.

plugins: [
  "macros"
]

Macro Usage

t

Tags a template for localization. Other l10n macros can be shallow nested in the template.

import {t} from '@bluecateng/l10n.macro';

console.log(t`Hello world`);

const salute = (name) => t`Hello ${name}`;

select

Selects a string among the specified choices based on the value of key. The key can be either a string, a number, or a boolean. The list of choices must include "other" which is selected if none of the others match.

import {select} from '@bluecateng/l10n.macro';

const statusText = (active) => select(active, {true: 'Active', other: 'Inactive'});

plural

Selects a string among the specified choices based on the cardinal form of key. The key must be a number. The list of choices must include "other" which is selected if none of the others match. The choice keys can be either numbers, or plural keywords for the target language. An offset can be specified as the first entry in the choices.

import {plural} from '@bluecateng/l10n.macro';

const peopleCount = (count) =>
	plural(count, {offset: 1, 0: 'nobody', 1: 'you', one: 'you and one other', other: 'you and # other'});

selectOrdinal

Selects a string among the specified choices based on the ordinal form of key. The arguments for this macro must follow the same rules as the plural macro.

import {selectOrdinal} from '@bluecateng/l10n.macro';

const place = (number) => selectOrdinal(number, {one: '#st', two: '#nd', few: '#rd', other: '#th'});

number

Formats the specified number using the current locale rules. The first argument must be a number. The second argument is optional, if specified must be either 'decimal' or 'percent', the default value is 'decimal'.

import {t, number} from '@bluecateng/l10n.macro';

const interest = (value) => t`The interest is ${number(value, 'percent')}`;