Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-lingui-transform-js

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-lingui-transform-js

Transform lingui-i18n methods to ICU message format

  • 1.0.0-7
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

react-plugin-lingui-transform-js

This plugin transforms messages written using lingui-i18n methods to static ICU message format.

The transformation speeds up translation at runtime while using helper methods allows to type-check messages.

Installation

npm install --save-dev babel-plugin-lingui-transform-js
# or
yarn add --dev babel-plugin-lingui-transform-js

Usage

.babelrc

{
  "plugins": ["lingui-transform-js"]
}

Via CLI

babel --plugins lingui-transform-js script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["lingui-transform-js"]
})

Details

Plugin performs following transformations:

Static message

i18n.t`Hello World`

// becomes
i18n.t({ id: "Hello World" })

Message with variables

i18n.t`Hi, my name is ${name}`

// becomes 
i18n.t({ id: "Hi, my name is {name}", params: { name }})

Plurals, select, ordinal and other formats

Format is basically a function which receives a value and format options. It's transformed to {variable, format, options} form. The most common formats are plural, select and ordinal:

i18n.plural({
  value: count,
  one: "# Book",
  other: "# Books"
})

// becomes
i18n.t({ 
  id: "{count, plural, one {# Book} other {# Books}}", 
  params: { count }
})

Number/date formats:

i18n.t`The answer is ${i18n.number(answer)}`

// becomes
i18n.t({ 
  id: "The answer is {answer, number}", 
  params: { answer }
})

Custom formats:

const currency = {
  style: 'currency',
  currency: 'EUR'
}

i18n.t`The price is ${i18n.number(amount, currency)}`

// becomes
i18n.t({ 
  id: "The price is {amount, number, currency}", 
  params: { answer },
  formats: { currency }
})

Combination of any above

i18n.select({
  value: gender,
  male: i18n.plural({
    value: numOfGuests,
    offset: 1,
    0: `${host} doesn't invite any guests`,
    1: `${host} invite ${guest} to his party`,
    other: `${host} invite ${guest} and # other to his party`
  }),
  female: i18n.plural({ ... })
})

// becomes
i18n.t({ 
  id: "{gender, select, male {{numOfGuests, plural, offset:1 =0 {...}}} female {...}}", 
  params: { gender, numOfGuests, host, guest }
})

Keywords

FAQs

Package last updated on 03 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc