Coder Social home page Coder Social logo

yeojz / redux-intl-connect Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 1.21 MB

Localization / i18n for redux - connect agnostic bindings with support for ICU Message Syntax

Home Page: https://yeojz.github.io/redux-intl-connect

License: Other

JavaScript 89.61% HTML 2.10% CSS 8.29%
redux react i18n messageformat internationalization localization

redux-intl-connect's Introduction

redux-intl-connect

Build Status Coverage Status npm package PRs Welcome

About

redux-intl-connect is a redux connect agnostic binding for internationalizing your application, with support for ICU MessageFormat.

This library does not depend on polyfills and/or the ECMAScript Internationalization API. It provides a single method: formatMessage with it's API inspired by the FormatJS counterpart.

Motivation

FormatJS and it's corresponding bindings for React, Ember, Angular for Redux are great. However, 2 use cases in some of my projects led to this:

  1. Location with older browsers meant the need for polyfills due to the absence of ECMAScript Internationalization API. However, these places are also highly likely to have slower internet speeds. As such a relatively large dependency download which is not ideal.
  2. Only functionality provided by formatMessage is required.

Links

Features

MessageFormat Syntax

For example:

// Messages in the reducer:
{
    someKey: 'You {NUM_ADDS, plural, offset:1' +
        '=0{did not add this}' +
        '=1{added this}' +
        'one{and one other person added this}' +
        'other{and # others added this}' +
      '}.',

    otherKey: '{GENDER, select, male{He} female{She} other{They}} liked this.'
}

// In your files:
formatMessage({id: 'someKey'}, {NUM_ADDS: 2}); // "You and one other person added this."

formatMessage({id: 'otherKey'}, {GENDER: 'male'}); // "He liked this."

Optional ECMA Intl Support

While it is not the goal of this project, as stated above (in Motivation #1), the messageformat package which was introduced as the dependent library in v2, has optional support for browser ECMAScript Intl or via it's polyfills.

As such, you can optionally turn on Intl API support by dispatching or setting ecmaSupport value in the reducer to true. You'll need the corresponding polyfill if you want cross browser version support.

For more information about the extended support, check out the messageformat documentation

store.dispatch(updateIntl({ecmaSupport: true}));

Installation

Install the library:

npm install redux-intl-connect redux --save

Install a corresponding redux connect library. Examples:

npm install react-redux
npm install preact-redux
npm install ng-redux

Initialization

Using react-redux as an example:

// intlConnect.js

import {connect} from 'react-redux';
import {connectIntl} from 'redux-intl-connect';

export default connectIntl(connect);
// intlInject.js

import {connect} from 'react-redux';
import {injectIntl} from 'redux-intl-connect';

export default injectIntl(connect);

In your components

// Example Component

const Component = (props) => {
	return <div>{props.intl.formatMessage({id: 'translation_id'})}</div>
}
// Using intlConnect defined above

import connect from './intlConnect';

export default connect(mapStateToProps, mapDispatchToProps)(Component);
// Using intlInject defined above
import connect from 'react-redux';
import intlInject from './intlInject';

export default connect(mapStateToProps, mapDispatchToProps)(intlInject(Component));

Available Methods

Provide locale and messages onload

You should provide a default locale and messages when the store is initially loaded.

const initialState = {
  intl: {
    locale: 'it',
    messages: {
      'greeting': 'Ciao!',
    },
  },
  // ...other initialState
};

const store = createStore(reducer, initialState);

Switching locale and messages on demand

You could switch locale on user's request by dispatching updateIntl action.

import {updateIntl} from 'redux-intl-connect';

store.dispatch(updateIntl({
  locale,
  messages,
}));

In a "real-world" scenario, an action will be dispatched to fetch translations from a server before updateIntl is being called. A possible example with redux-thunk would be:

import {updateIntl} from 'redux-intl-connect';

const getAndUpdateIntl = (locale) => (dispatch) => {

  fetch('url-to-messages')
    .then(function(response) {
       return response.text()
     })
    .then((body) => {
        dispatch(updateIntl({
          locale,
          messages: body
        }))
    });
}

License

redux-intl-connect is BSD licensed

See also

Acknowledgement

Highly influenced by the following libraries:

ICU Message Syntax parsing is done via messageformat package.

redux-intl-connect's People

Contributors

greenkeeper[bot] avatar yeojz avatar

Stargazers

 avatar  avatar

Watchers

 avatar

redux-intl-connect's Issues

An in-range update of redux is breaking the build 🚨

Version 3.7.0 of redux just got published.

Branch Build failing 🚨
Dependency redux
Current Version 3.6.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As redux is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪

Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes v3.7.0

Another long break!

Oh, hey! I didn't see you sitting there. You look bored. How about a Redux release to spice things up?

Not a huge set of changes to report here. The biggest change, and the reason for the minor bump, is the UMD build is now done via Rollup. One big advantage is more readable code in the bundle. Rollup does "scope hoisting", which is a fancy term for putting every module at the top level of the file. Other than a surrounding IIFE, all of the code in Redux all lives together. You can compare the two here:

Rollup UMD build
vs
Webpack UMD build

There is also a cost savings of 30,811 vs 26,880 bytes, and 6,999 vs 5,995 bytes minified. Redux is already a small library, and this helps shave some extra bytes for our UMD users.

One thing to note is that Webpack has introduced it's own scope hoisting feature in 3.0 beta. So, this isn't intended as an indictment of Webpack. You should continue to use it in your own apps. The adage of "Webpack is for apps, Rollup is for libraries" definitely holds true. It still has a superior developer experience with hot module reloading and webpack-dev-server. But use whatever makes sense for your project, not just whatever we use. 😄

We're also looking at applying this to the NPM bundle. The main motivation is again more readable code in your bundles. Instead of transpilation oddities from Babel, you will end up with a single clean file, which should be easier to read through and debug. It's currently scheduled for the big, mythical 4.0 release and you can follow along in #2358

Changes

Commits

The new version differs by 309 commits.

  • 2d229f0 3.7.0
  • f3bba96 Removing the browser field for now
  • f4d9e55 Upgrade all example deps, including the universal example to Webpack 2
  • 2bc8f84 Upgrade some other deps
  • 070b838 Upgrades and remove check-es3-syntax since Rollup breaks it always
  • 1094724 Fix a typo in the Immutable.js docs (#2453)
  • bf3a557 Fix redux-mock-store libdef for todos-flow (#2430)
  • bf2b9b5 give redux-subscriber some attention in StoreSetup.md (#2433)
  • 8b3e0cb syncing docs with recent PR (2431) (#2432)
  • e238a19 'todo' const removal from reducers/todos.js (#2431)
  • 0babfab Merge pull request #2429 from alexbaumgertner/bugfix/doc-link
  • ae563c3 Fix link to ImmutableData article
  • 383215a Update redux.js (#2428)
  • c9ad6a4 Add 100% test coverage for todos-flow example (#2413)
  • ea16d76 Fix eslint warnings, update react-scripts (#2420)

There are 250 commits in total.

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of cross-env is breaking the build 🚨

Version 3.2.0 of cross-env just got published.

Branch Build failing 🚨
Dependency cross-env
Current Version 3.1.4
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As cross-env is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Release Notes v3.2.0

<a name"3.2.0">

3.2.0 (2017-03-04)

Features

  • revamp: revamp the entire lib (backward compatible) (#63) (dad00c46)
Commits

The new version differs by 4 commits .

  • dad00c4 feat(revamp): revamp the entire lib (backward compatible) (#63)
  • e33a85c docs(README): Add doc for cross-var. (#58)
  • 5e590ec docs(README): added how to use cross-env to run npm sub-scripts (#53)
  • afdb2de docs(README): mention Bash on Windows (#49)

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.