Last active
September 23, 2018 09:08
-
-
Save jamuhl/e00544af5de3e30f11ba967cc31dd3ce to your computer and use it in GitHub Desktop.
Used in medium post - unleash the hidden superpowers of react-intl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a hoc to extend components with locize features | |
function supportLocize() { | |
return function Wrapper(WrappedComponent) { | |
class LocizeExtension extends Component { | |
constructor(props, context) { | |
super(props, context); | |
// get needed props | |
const { id, defaultMessage, description, namespace } = props; | |
// get current value in message catalog | |
const currentValue = translations[currentLocale] && translations[currentLocale][namespace] && translations[currentLocale][namespace][id] | |
// depeding on not yet exists or changed | |
// save or update the value on locize | |
if (SAVE_NEW_VALUES && !currentValue) { | |
locizer.add(namespace, id, defaultMessage, description); | |
} else if (UPDATE_VALUES && currentValue !== defaultMessage) { | |
locizer.update(namespace, id, defaultMessage, description) | |
} | |
} | |
// render the wrapped component (react-intl's FormattedMessage) | |
render() { | |
return <WrappedComponent {...this.props} /> | |
} | |
} | |
return withContext()(LocizeExtension); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment