npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@locmod/intl

v1.0.5

Published

This is lightweight copycat of `react-intl` package with minimal functionality

Downloads

293

Readme

Intl

This is lightweight copycat of react-inlt package with minimal functionality.

Note: For now we have only EN locale and there is no requirements to extract translations to json. All translations injected to js bundle.

Installation

Add /// <reference types="@locmod/intl" /> into your next-env.d.ts

import React from 'react'
import { IntlProvider, Message } from 'intl'

const messages = {
  title: {
    en: 'Hello World',
  },
}

const App = () => (
  <IntlProvider locale="en">
    <Message value={messages.title} />
  </IntlProvider>
)

Message Declaration

All intl messages should be written separately from components in messages.ts.

const messages = {
  title: {
    en: 'Hello World',
  },
  content: {
    en: 'Hello, <b>{username}</b>',
  },
}

<Message />

import { Message } from 'intl'
import messages from './messages'

const App = () => {
  const username = 'John Doe'

  const contentMessage = useMemo(() => ({
    ...messages.content, 
    values: { username },
  }), [])

  return (
    <div>
      <Message value={messages.title} />
      <Message value={contentMessage} />
    </div>
  )
}

Component Props

value - string | IntlMessage html - boolean, "false" by default

<Message /> is ended component and like tags can receive any additional attribute.

<Message value={message} html />

intl.formatMessage

We shouldn't wrap intl.formatMessage with useMemo because its value based on locale. Intl makes optimizations inside itself.

import { useIntl } from 'intl'
import messages from './messages'

const App = () => {
  const intl = useIntl()

  const title = intl.formatMessage(messages.title)
  const content = intl.formatMessage(messages.content)

  return (
    <div>
      {title}
      <span dangerouslySetInnerHTML={{ __html: content }} />
    </div>
  )
}

Warning: Please use <Message /> when it's possible, and avoid of using intl.formatMessage when it's not necessary.

Message Syntax

Simple Arguments

'Hello, <b>{username}</b>'

formatMessage(message, { username: 'John Doe' })  // Hello, <b>John Doe</b>

Formatted Arguments

Sometimes you'd like to use conditions inside text based on passed value. The most case is plurals. There are couple of formulas:

'{count} {count, plural, one {product} other {products}}'

formatMessage(message, { count: 0 })  // 0 products
formatMessage(message, { count: 1 })  // 1 product
'{count, plural, one {# product} other {# products}}'

formatMessage(message, { count: 0 })  // 0 products
formatMessage(message, { count: 1 })  // 1 product
'Get free {gender, select, female {perfume} male {cologne}}'

formatMessage(message, { gender: 'female' })  // Get free perfume
formatMessage(message, { gender: 'male' })    // Get free cologne
formatMessage(message, { gender: null })      // Get free
'{gender, select, female {perfume} male {cologne} other {fragrance}}'

formatMessage(message, { gender: 'female' })  // perfume
formatMessage(message, { gender: 'male' })    // cologne
formatMessage(message, { gender: null })      // fragrance

React Component Arguments

A message can contain react components. To render them as components but not strings <Message /> component should be used

import Icon from 'components/Icon/Icon'
import Link from 'components/Link/Link'

const message = { 
  en: 'Read our <Icon /> <Link to="{docsLink}">docs</Link>',
  values: { docsLink: '/docs' },
  components: { Icon, Link },
}

return (
  <Message value={message} />
)