Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from eugene-manuilov/release/0.4.0
Browse files Browse the repository at this point in the history
Release/0.4.0
  • Loading branch information
eugene-manuilov authored Aug 18, 2017
2 parents 79cd8c3 + cbf2a0f commit 97f7ba9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## v0.4.0 (2017-08-17)

**Implemented enhancements:**

- Implemented ability to get wrapped component.

## v0.3.3 (2017-08-12)

**Implemented enhancements:**
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# react-gettext 0.3.3
# react-gettext 0.4.0

[![Build Status](https://travis-ci.org/eugene-manuilov/react-gettext.svg?branch=master)](https://travis-ci.org/eugene-manuilov/react-gettext)

Expand Down Expand Up @@ -104,12 +104,14 @@ See an [example](https://github.com/eugene-manuilov/react-gettext/tree/master/ex

## Documentation

### withGettext(translations, pluralForms)
### withGettext(translations, pluralForms, options)

Higher-order function which is exported by default from `react-gettext` package. It accepts two arguments and returns function to create higher-order component.

- **translations**: a hash object or a function which returns hash object where keys are original messages and values are translated messages.
- **pluralForms**: a string to calculate plural form (used by [Gettext PO](http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html?id=l10n/pluralforms)) or a function which accepts a number and calculates a plural form number. Pay attentions that plural forms are zero-based what means to get 1st plural form it should return 0, to get 2nd - 1, and so on.
- **options**: a hash object with options. Currently supports following options:
- **withRef**: an optional boolean flag that determines whether or not to set `ref` property to a wrapped component what will allow you to get wrapped component instance by calling `getWrappedComponent()` function of the HOC. By default: `FALSE`.

Example:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"bugs": {
"url": "https://github.com/eugene-manuilov/react-gettext/issues"
},
"version": "0.3.3",
"version": "0.4.0",
"main": "lib/index",
"files": [
"*.md",
Expand Down
38 changes: 24 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@ import React from 'react';
import hoistNonReactStatic from 'hoist-non-react-statics';
import Textdomain from './Textdomain';

export { Textdomain };
const withGettext = (translations = {}, pluralForm = 'n != 1', options = {}) => (WrappedComponent) => {
const args = Object.assign({ withRef: false }, options);

class WithGettext extends Textdomain {

export default function withGettext(translations = {}, pluralForm = 'n != 1') {
return (WrappedComponent) => {
class WithGettext extends Textdomain {
getWrappedComponent() {
return this.refs.wrappedComponent;
}

render() {
return React.createElement(WrappedComponent, this.props);
render() {
const newprops = Object.assign({}, this.props);
if (args.withRef) {
newprops.ref = 'wrappedComponent';
}

return React.createElement(WrappedComponent, newprops);
}

WithGettext.defaultProps = {
translations,
plural: pluralForm,
};

WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
}

return hoistNonReactStatic(WithGettext, WrappedComponent);
WithGettext.defaultProps = {
translations,
plural: pluralForm,
};
}

WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;

return hoistNonReactStatic(WithGettext, WrappedComponent);
};

export { Textdomain };
export default withGettext;

0 comments on commit 97f7ba9

Please sign in to comment.