Vulcan/packages/vulcan-i18n/lib/modules/provider.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-06-01 11:49:32 +09:00
import React, { Component } from 'react';
import { Strings } from 'meteor/vulcan:lib';
2017-06-01 11:49:32 +09:00
import { intlShape } from './shape.js';
export default class IntlProvider extends Component{
formatMessage = ({ id, defaultMessage }, values) => {
const messages = Strings[this.props.locale] || {};
2017-06-03 09:24:43 +09:00
let message = messages[id] || defaultMessage;
2017-06-01 18:23:36 +09:00
if (values) {
_.forEach(values, (value, key) => {
// note: see replaceAll definition in vulcan:lib/utils
message = message.replaceAll(`{${key}}`, value);
2017-06-01 18:23:36 +09:00
});
}
return message;
2017-06-01 11:49:32 +09:00
}
formatStuff = (something) => {
2017-06-01 11:49:32 +09:00
return something;
}
getChildContext() {
return {
intl: {
formatDate: this.formatStuff,
formatTime: this.formatStuff,
formatRelative: this.formatStuff,
formatNumber: this.formatStuff,
formatPlural: this.formatStuff,
formatMessage: this.formatMessage,
formatHTMLMessage: this.formatStuff,
now: this.formatStuff,
}
};
}
render(){
return this.props.children;
}
}
IntlProvider.childContextTypes = {
intl: intlShape
2018-01-25 15:03:03 -06:00
}