Skip to content

Commit

Permalink
use prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
rmdort committed Jul 10, 2018
1 parent 6c857f2 commit 47ab602
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 34 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A simple multilingual translate component and HOC for react and redux",
"main": "lib/index.js",
"scripts": {
"prettier": "prettier --single-quote --no-semi 'src/*.js' --write",
"watch": "BABEL_ENV=production babel --watch src --out-dir lib"
},
"repository": {
Expand All @@ -23,6 +24,7 @@
"homepage": "https://github.com/rmdort/react-redux-multilingual#readme",
"dependencies": {
"hoist-non-react-statics": "^1.2.0",
"prettier-standard": "^8.0.1",
"prop-types": "^15.5.8"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function setLocale (locale) {
export function setLocale(locale) {
return {
type: 'SET_LOCALE',
locale
}
}
}
2 changes: 1 addition & 1 deletion src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ const {
} = TranslateContext

export { TranslateProvider, TranslateConsumer }
export default TranslateContext
export default TranslateContext
7 changes: 1 addition & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,4 @@ import withTranslate from './withTranslate'
import IntlProvider from './provider'
import * as IntlActions from './actions'

export {
IntlReducer,
withTranslate,
IntlProvider,
IntlActions
}
export { IntlReducer, withTranslate, IntlProvider, IntlActions }
33 changes: 20 additions & 13 deletions src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,42 @@ import { supplant, translateKey, createHTMLMarkup } from './utils'
import { TranslateProvider } from './context'

class IntlProvider extends React.Component {
constructor (props) {
constructor(props) {
super(props)
if (!props.translations || !props.locale) {
let namePart = this.constructor.displayName ? ' of ' + this.constructor.displayName : ''
throw new Error('Could not find translations or locale on this.props ' + namePart)
let namePart = this.constructor.displayName
? ' of ' + this.constructor.displayName
: ''
throw new Error(
'Could not find translations or locale on this.props ' + namePart
)
}
}
static propTypes = {
translations: PropTypes.object
};
}
static defaultProps = {
translations: {}
};
}
translate = (key, placeholders, isHTML, options = {}) => {
const result = translateKey(key, this.props.translations[this.props.locale]['messages'])
const result = translateKey(
key,
this.props.translations[this.props.locale]['messages']
)
const tagName = options.tagName || 'div'
if (typeof placeholders === 'undefined') {
return result
}
const finalResult = supplant(result, placeholders)
return isHTML
? React.createElement(
tagName,
{ dangerouslySetInnerHTML: createHTMLMarkup(finalResult) },
null
)
tagName,
{ dangerouslySetInnerHTML: createHTMLMarkup(finalResult) },
null
)
: finalResult
};
render () {
}
render() {
return (
<TranslateProvider value={this.translate}>
{this.props.children}
Expand All @@ -42,7 +49,7 @@ class IntlProvider extends React.Component {
}
}

function mapPropsToState (state) {
function mapPropsToState(state) {
const { Intl } = state
return {
...Intl,
Expand Down
4 changes: 2 additions & 2 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var initialState = {
locale: 'en'
}

export default function (state = initialState, action) {
export default function(state = initialState, action) {
switch (action.type) {
case 'SET_LOCALE':
return {
Expand All @@ -12,4 +12,4 @@ export default function (state = initialState, action) {
default:
return state
}
}
}
8 changes: 4 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export function supplant (s, d) {
export function supplant(s, d) {
for (var p in d) {
s = s.replace(new RegExp('{' + p + '}', 'g'), d[p])
}
return s
}

export function translateKey (path, obj, safe) {
export function translateKey(path, obj, safe) {
return path.split('.').reduce((prev, curr) => {
return !safe ? prev[curr] : (prev ? prev[curr] : undefined)
return !safe ? prev[curr] : prev ? prev[curr] : undefined
}, obj)
}

export function createHTMLMarkup (html) {
export function createHTMLMarkup(html) {
return { __html: html }
}
14 changes: 8 additions & 6 deletions src/withTranslate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { TranslateConsumer } from './context'
* @param {Object} WrappedComponent
* @return {Object}
*/
function withTranslate (WrappedComponent) {
return React.forwardRef((props, ref) => (
<TranslateConsumer>
{(translate) => <WrappedComponent {...props} translate={translate} ref={ref} />}
function withTranslate(WrappedComponent) {
return React.forwardRef((props, ref) => (
<TranslateConsumer>
{translate => (
<WrappedComponent {...props} translate={translate} ref={ref} />
)}
</TranslateConsumer>
))
))
}

export default withTranslate
export default withTranslate

0 comments on commit 47ab602

Please sign in to comment.