All Projects → Tiqa → redux-polyglot

Tiqa / redux-polyglot

Licence: MIT license
Polyglot.js bindings for Redux

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to redux-polyglot

Polyglot
🔤 Multilingual and i18n support tool for Jekyll Blogs
Stars: ✭ 242 (+310.17%)
Mutual labels:  i18n, polyglot
react-polyglot-hooks
Hooks for using Polyglot.js with React.
Stars: ✭ 26 (-55.93%)
Mutual labels:  i18n, polyglot
MQCMS-admin
MQCMS后台管理系统
Stars: ✭ 24 (-59.32%)
Mutual labels:  i18n
msgtools
Tools for Developing Diagnostic Messages
Stars: ✭ 18 (-69.49%)
Mutual labels:  i18n
awesome-i18n
🌍 A curated list of i18n resources for all kind of languages and frameworks
Stars: ✭ 205 (+247.46%)
Mutual labels:  i18n
asgi-babel
Adds internationalization (i18n) support to ASGI applications (Asyncio/Trio)
Stars: ✭ 21 (-64.41%)
Mutual labels:  i18n
cakephp-i18n
A CakePHP plugin with I18n related tools.
Stars: ✭ 40 (-32.2%)
Mutual labels:  i18n
ad localize
ADLocalize is a simple way to manage your localization files. Supported wording sources : CSVs and Google Sheets. Localization file generation available for iOS, Android, JSON (i18next), YAML and Java properties
Stars: ✭ 22 (-62.71%)
Mutual labels:  i18n
qiokian
🙊 live2d anime waifu vuejs component.
Stars: ✭ 34 (-42.37%)
Mutual labels:  i18n
doorkeeper-i18n
Translation files for Doorkeeper OAuth 2 provider
Stars: ✭ 30 (-49.15%)
Mutual labels:  i18n
strf
Yet another C++ text formatting library.
Stars: ✭ 57 (-3.39%)
Mutual labels:  i18n
input.rs
libinput bindings for rust
Stars: ✭ 58 (-1.69%)
Mutual labels:  bindings
universal-react-starter-kit
Universal React Starter Kit is an universal web application framework using koa, react, redux and webpack.
Stars: ✭ 13 (-77.97%)
Mutual labels:  i18n
Foxy
🦊 A very, very cute, multipurpose, multilingual bot for Discord! Making your server cuter -w-
Stars: ✭ 58 (-1.69%)
Mutual labels:  i18n
vue-i18n
A small package for implementing translations in Vue.js
Stars: ✭ 40 (-32.2%)
Mutual labels:  i18n
angular-i18n-localization
An angular application with i18n and localization implemented.
Stars: ✭ 22 (-62.71%)
Mutual labels:  i18n
kosmikoa.nvim
A dark color scheme for Neovim with support for LSP, Treesitter. This mirror is deprecated. Use the repo at https://sr.ht/~novakane/kosmikoa.nvim/
Stars: ✭ 23 (-61.02%)
Mutual labels:  polyglot
example-app
Example app showcasing fulls1z3's Angular libraries
Stars: ✭ 27 (-54.24%)
Mutual labels:  i18n
poeditor-cli
POEditor CLI
Stars: ✭ 29 (-50.85%)
Mutual labels:  i18n
bpmn-js-i18n
Internationalization resources for bpmn-js
Stars: ✭ 49 (-16.95%)
Mutual labels:  i18n

redux-polyglot

Codeship Status for Tiqa/redux-polyglot

Toolset (actions, reducer, middleware, enhancer, selectors) to help use Polyglot with Redux.

Installation

    npm install --save redux-polyglot

Setup

First of all, you need the polyglot reducer in your rootReducer :

import { createStore, combineReducers } from 'redux';
import { polyglotReducer } from 'redux-polyglot';

const rootReducer = combineReducers({
    ...yourReducers,
    polyglot: polyglotReducer,
});
const store = createStore(rootReducer, {});

Usage

Set the language

without middleware

You can use redux-polyglot without his middleware, for this you need the setLanguage() action creator :

  • setLanguage :: (String, Object) -> Action

Example:

import { setLanguage } from 'redux-polyglot';

store.dispatch(setLanguage('en', { yolo: 'yolo' }));

second parameter should be polyglot phrases (see polyglot documentation)

note: if language phrases already exists, this will overwrite the corresponding object state.

with middleware

The createPolyglotMiddleware() function allow you to automatically update language and phrases by listening to specific action(s).

The middleware catches specific action(s), and find the locale in the payload, and then [asynchronously] load the polyglot phrases (with Promise).

It takes 3 parameters and return a middleware :

  • 1 - actionToCatch :: String | Array<String>
    • the type(s) of the action to catch
  • 2 - getLocale :: Object -> String
    • a function that take the catched action as parameter and return new language.
  • 3 - getPhrases :: String -> Promise Object
    • a function that take the language (as provided by getLocale) and return a Promise of Object ( Object should be polyglot phrases )

the middleware will catch actionToCatch; note: when a matching action is dispatched, it will return this promise called so you can await on it (pretty useful on SSR)

import { createStore, combineReducers, applyMiddleware } from 'redux';

const polyglotMiddleware = createPolyglotMiddleware(
    'ACTION_TO_CATCH',
    action => action.payload.locale,
    locale => new Promise(resolve => {
        // perform async here
        resolve({
            hello: 'bonjour',
        });
    }),
)

const store = createStore(rootReducer, {}, applyMiddleware(polyglotMiddleware));

you can catch more than one action passing an array of action types:

const polyglotMiddleware = createPolyglotMiddleware(
    ['FIRST_ACTION_TO_CATCH', 'SECOND_ACTION_TO_CATCH'],
    getLocale,
    getPhrases,
)

note: if language has not changed, nothing happens.

Translation

with getP() selector

You can use the getP(state) selector.

It returns an object with 4 functions inside :

  • t: String -> String : translation (the original polyglot t function)
  • tc: String -> String : translation capitalized
  • tt: String -> String : translation titleized
  • tu: String -> String : translation upper-cased
  • tm: (String -> String) -> String -> String : translation using a custom morphism

(see polyglot documentation)

there is an optional parameter to getP(). this is allow you to automatically 'aim' a scope in your phrases object using polyglotScope property.

for example :

store.dispatch(setLanguage('en', {
    some: { nested: { data: { hello: 'hello' } } }
}));
const p = getP(store.getState(), { polyglotScope: 'some.nested.data' });
console.log(p.tc('hello')) // => will return 'Hello'

Getting current locale

getLocale(state) selector returns current language.

If you use React

You can use connect() from react-redux, and the getP() selector, to get the p prop in your component.

Proptype:

p: PropTypes.shape({
    t: PropTypes.func.isRequired,
    tc: PropTypes.func.isRequired,
    tu: PropTypes.func.isRequired,
    tm: PropTypes.func.isRequired,
}),
translate() enhancer

props.p can be also be provided easily to a component with the translate enhancer :

import translate from 'redux-polyglot/translate';
const DummyComponentWithPProps = translate(DummyComponent);

you can select a polyglotScope with translate('scope', Component)

// all this lines return an enhanced Dummy component
translate(Dummy);
translate('catalog', Dummy); // with polyglotScope
translate()(Dummy); // curried
translate('catalog')(Dummy); // curried with polyglotScope.
translate({ polyglotScope : 'some.nested.data', ownPhrases: 'some.nested.data.hello': 'Hi !', ... })(Dummy); // curried with object configuration.
get locale in a component

You can use the getLocale() selector inside a mapStateToProps from react-redux.

Proptype: locale: PropTypes.string,

Overwrite phrases

In some case, you should be able to replace some default phrases by others phrases.

For doing this, you have to define an object which contains your overwrited phrases. This object is composed of : { 'some.nested.data': 'phrase', ... } where key is the target path you want to replace and value ... the new value.

with getP() selector

Simply add ownPhrases property and set the new configuration like above to overwrite :

store.dispatch(setLanguage('en', {
    some: { nested: { data: { hello: 'hello' } } }
}));
const p = getP(store.getState(), {
    polyglotScope: 'some.nested.data',
    ownPhrases: { 'some.nested.data.hello': 'Hi !' }
});
console.log(p.tc('hello')) // => will return 'Hi !'
with translate() enhancer

Instead passing only string as parameter : translate('catalog', Dummy), pass a plain object which contains polyglotScope and ownPhrases properties :

translate({
    polyglotScope : 'some.nested.data',
    ownPhrases: { 'some.nested.data.catalog': 'Cars' }
}, Dummy);
console.log(p.tc('catalog')) // => will return 'Cars'

Use polyglot options

if you want to use onMissingKey, allowMissing or warn polyglot options, you can use the createGetP factory selector to create a custom getP.

usage :

import { createGetP } from 'redux-polyglot';

const options = {
  allowMissing: true,
}

export const getP = createGetP(options);

Please note you cannot use translate hoc with a custom getP selector.

Team

These folks keep the project moving and are resources for help:

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].