All Projects → ericls → react-put

ericls / react-put

Licence: other
A flexible formatter and i18n interface for React.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-put

Globalize
A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
Stars: ✭ 4,612 (+19952.17%)
Mutual labels:  i18n, formatter
Dayperiodformatter
A formatter for localized day periods (morning, afternoon, night, etc.)
Stars: ✭ 181 (+686.96%)
Mutual labels:  i18n, formatter
Translatedjs
Internationalization and localization for JavaScript and Node.js
Stars: ✭ 17 (-26.09%)
Mutual labels:  i18n, formatter
li18nt
🌎 Lint your i18n translation files. Detect conflicting properties, duplicates and make it more readable and easier to maintain by formatting it!
Stars: ✭ 29 (+26.09%)
Mutual labels:  i18n, formatter
Moment.php
Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Stars: ✭ 900 (+3813.04%)
Mutual labels:  i18n, formatter
vue-translated
Internationalization (i18n) and localization (l10n) library for Vue.js v2.
Stars: ✭ 19 (-17.39%)
Mutual labels:  i18n, formatter
ii18n
II18N - Go i18n library.
Stars: ✭ 20 (-13.04%)
Mutual labels:  i18n
yapf-online
google/yapf online demo
Stars: ✭ 23 (+0%)
Mutual labels:  formatter
formatters
A javascript library for formatting and manipulating.
Stars: ✭ 14 (-39.13%)
Mutual labels:  formatter
gettext-extractor
A flexible and powerful Gettext message extractor with support for JavaScript, TypeScript, JSX and HTML.
Stars: ✭ 82 (+256.52%)
Mutual labels:  i18n
lucene
Node.js lib to transform: lucene query → syntax tree → lucene query
Stars: ✭ 61 (+165.22%)
Mutual labels:  formatter
Fmt.jl
Python-style format strings for Julia
Stars: ✭ 31 (+34.78%)
Mutual labels:  formatter
acts as localized
Localization accessor mechanism for AR models
Stars: ✭ 12 (-47.83%)
Mutual labels:  i18n
archived-bot
A Discord music bot serving music in over 3 million discord servers
Stars: ✭ 496 (+2056.52%)
Mutual labels:  i18n
v-page
A simple pagination bar, including length Menu, i18n support, based on Vue2.x
Stars: ✭ 85 (+269.57%)
Mutual labels:  i18n
wlc
Weblate command line client
Stars: ✭ 22 (-4.35%)
Mutual labels:  i18n
intl-format
A wrapper library for PHP to format and internationalize values in messages like sprintf
Stars: ✭ 12 (-47.83%)
Mutual labels:  formatter
tr4n5l4te
Use Google Translate without an API key.
Stars: ✭ 32 (+39.13%)
Mutual labels:  i18n
lancer
Turn your python code into a hideous mess. Ever heard of Black? This is the opposite.
Stars: ✭ 179 (+678.26%)
Mutual labels:  formatter
canonix
Experiment in Nix formatting
Stars: ✭ 18 (-21.74%)
Mutual labels:  formatter

react-put

Build Status codecov

A package that displays things in react components. Suitable for formatting and i18n.

Interactive Demo

This package works by injecting a function (by default called put) into the props of a a connected react component. The injected function takes a key and optional context and returns something else (usually a string).

Install

npm i --save react-put

Examples:

The basic usage:

// App.js
import connectPut from "react-put"

class App extends Component {
  render() {
    return (
      <div>
        <p>{this.props.put('hello')}, {this.props.put('welcome', 'username')}</p>
        <p>{this.props.put('haveApple', 'username', 3)}</p>
        <p>{this.props.put('testKey')}</p>
      </div>
    );
  }
}
const options = {
  dictionary: {
    hello: '你好',
    welcome: name => `欢迎${name}`,
    haveApple: (name, amount) => `${name} has ${amount} ${amount === 1 ? 'apple' : 'apples'}`,
  },
  mapPropToDictionary: props => props, // You can do something wild with this option
};
export default connectPut(options)(App);

// test.js
import App from './App';

...
  render() {
    return <App testKey='someValue' />
  }
...

// renders:
<div>
  <p>你好, 欢迎username</p>
  <p>username has 3 apples</p>
  <p>someValue</p>
</div>

Here's an example of the usage with redux managed props:

class App extends Component {
  constructor(props) {
    super(props);
    this.changeLanguage = () => {
      this.props.dispatch({ type: 'SET_DICT', dictionary: {...} }); // Assume SET_DICT is received by dictionary reducer
    };
  }
  render() {
    return (
      <div>
        <p>{this.props.put('hello')}, {this.props.put('welcome', 'username')}</p>
        <p>{this.props.put('haveApple', 'username', 3)}</p>
        <p>{this.props.put('testKey')}</p>
        <button onClick={this.changeLanguage}>Change Language</button>
      </div>
    );
  }
}
const options = {
  mapPropToDictionary: props => Object.assign({}, props.dictionary),
};
const mapStateToProps = state => Object.assign({}, { dictionary: state.dictionary });
ConnectedApp = connectPut(options)(App);
ConnectedApp = connect(mapStateToProps)(ConnectedApp);

Guide:

This package exposes a single function connectPut and is the default export of the package.

connectPut():

type Options = {
  dictionary?: Object,
  mapPropToDictionary?: (props: Object) => Object,
  putFunctionName?: string,
  notFound?: (key: string) => any
}
connectPut(options: Options)(Component) => Component

Options:

There are 4 optional keys in the options.

key description
dictionary An object directly used by the injected function
mapPropToDictionary A function that takes props of a component and returns an object that updates dictionary
notFound A function that takes key, if (!(key in dictionary)), and returns something to display. (Defaults to key => `$$${key}`)
putFunctionName A string that specifies the injected prop name. (Defaults to put)

put():

The connected component will have a new props, which by default is called put.

put(key, ...context) => any

This function looks up the key in dictionary and returns something to return accordingly.

If the value of the key is a string, a string is returned. If the value is a function, the function is called with ...context and returns.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].