lingui-react
Advanced tools
Comparing version 1.0.0-3 to 1.0.0-4
@@ -20,6 +20,11 @@ import { date, number } from 'lingui-formats'; | ||
if (render) { | ||
// Built-in element: h1, p | ||
if (typeof render === 'string') { | ||
return React.createElement(render, {}, children); | ||
} | ||
return React.isValidElement(render) | ||
// $FlowIgnore: Don't know how to handle this type union | ||
// Custom element: <p className="lear' /> | ||
? React.cloneElement(render, {}, children) | ||
// $FlowIgnore: Don't know how to handle this type union | ||
// Custom component: ({ translation }) => <a title={translation}>x</a> | ||
: React.createElement(render, { translation: children }); | ||
@@ -55,4 +60,2 @@ } | ||
return function (WrappedComponent) { | ||
var _class, _temp2; | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -69,4 +72,3 @@ if (typeof options === 'function' || React.isValidElement(options)) { | ||
return _temp2 = _class = function (_React$Component) { | ||
var WithI18n = function (_React$Component) { | ||
_inherits(WithI18n, _React$Component); | ||
@@ -101,8 +103,6 @@ | ||
value: function componentDidMount() { | ||
if (update) { | ||
var _getI18n = this.getI18n(), | ||
subscribe = _getI18n.subscribe; | ||
var _getI18n = this.getI18n(), | ||
subscribe = _getI18n.subscribe; | ||
if (subscribe) subscribe(this.checkUpdate); | ||
} | ||
if (update && subscribe) subscribe(this.checkUpdate); | ||
} | ||
@@ -112,8 +112,6 @@ }, { | ||
value: function componentWillUnmount() { | ||
if (update) { | ||
var _getI18n2 = this.getI18n(), | ||
unsubscribe = _getI18n2.unsubscribe; | ||
var _getI18n2 = this.getI18n(), | ||
unsubscribe = _getI18n2.unsubscribe; | ||
if (unsubscribe) unsubscribe(this.checkUpdate); | ||
} | ||
if (update && unsubscribe) unsubscribe(this.checkUpdate); | ||
} | ||
@@ -123,3 +121,3 @@ }, { | ||
value: function getI18n() { | ||
return this.context.i18nManager || {}; | ||
return this.context.linguiPublisher || {}; | ||
} | ||
@@ -131,5 +129,3 @@ }, { | ||
i18n = _getI18n3.i18n; | ||
// $FlowIgnore: https://github.com/facebook/flow/issues/3241 | ||
return React.createElement(WrappedComponent, _extends({ ref: this.setWrappedInstance }, this.props, { i18n: i18n })); | ||
@@ -140,5 +136,12 @@ } | ||
return WithI18n; | ||
}(React.Component), _class.contextTypes = { | ||
i18nManager: PropTypes.object | ||
}, _temp2; | ||
}(React.Component); | ||
// return needs to be here, otherwise flow complains about {...this.props} | ||
// in WrappedComponent | ||
WithI18n.contextTypes = { | ||
linguiPublisher: PropTypes.object | ||
}; | ||
return WithI18n; | ||
}; | ||
@@ -148,56 +151,41 @@ }); | ||
/* | ||
* I18nManager - Connects to lingui-i18n/I18n class | ||
* I18nPublisher - Connects to lingui-i18n/I18n class | ||
* Allows listeners to subscribe for changes | ||
*/ | ||
var I18nManager = function I18nManager(_ref) { | ||
var language = _ref.language, | ||
messages = _ref.messages, | ||
languageData = _ref.languageData, | ||
development = _ref.development, | ||
i18n = _ref.i18n; | ||
function LinguiPublisher(i18n) { | ||
var subscribers = []; | ||
_classCallCheck(this, I18nManager); | ||
return { | ||
i18n: i18n, | ||
_initialiseProps.call(this); | ||
getSubscribers: function getSubscribers() { | ||
return subscribers; | ||
}, | ||
subscribe: function subscribe(callback) { | ||
subscribers.push(callback); | ||
}, | ||
unsubscribe: function unsubscribe(callback) { | ||
subscribers = subscribers.filter(function (cb) { | ||
return cb !== callback; | ||
}); | ||
}, | ||
update: function update() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
messages = _ref.messages, | ||
language = _ref.language, | ||
languageData = _ref.languageData; | ||
this.i18n = i18n || setupI18n({ | ||
language: language, | ||
messages: messages, | ||
languageData: languageData, | ||
development: development | ||
}); | ||
}; | ||
if (!messages && !language && !languageData) return; | ||
var _initialiseProps = function _initialiseProps() { | ||
var _this2 = this; | ||
if (messages) i18n.load(messages); | ||
if (language) i18n.activate(language); | ||
if (languageData) i18n.loadLanguageData(languageData); | ||
this.subscribers = []; | ||
this.subscribe = function (callback) { | ||
_this2.subscribers.push(callback); | ||
subscribers.forEach(function (f) { | ||
return f(); | ||
}); | ||
} | ||
}; | ||
} | ||
this.unsubscribe = function (callback) { | ||
_this2.subscribers = _this2.subscribers.filter(function (cb) { | ||
return cb !== callback; | ||
}); | ||
}; | ||
this.update = function () { | ||
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
messages = _ref2.messages, | ||
language = _ref2.language, | ||
languageData = _ref2.languageData; | ||
if (!messages && !language && !languageData) return; | ||
if (messages) _this2.i18n.load(messages); | ||
if (language) _this2.i18n.activate(language); | ||
if (languageData) _this2.i18n.loadLanguageData(languageData); | ||
_this2.subscribers.forEach(function (f) { | ||
return f(); | ||
}); | ||
}; | ||
}; | ||
var I18nProvider = function (_React$Component) { | ||
@@ -215,6 +203,11 @@ _inherits(I18nProvider, _React$Component); | ||
languageData = _this$props.languageData, | ||
development = _this$props.development, | ||
i18n = _this$props.i18n; | ||
development = _this$props.development; | ||
_this.i18nManager = new I18nManager({ language: language, messages: messages, languageData: languageData, i18n: i18n, development: development }); | ||
var i18n = _this.props.i18n || setupI18n({ | ||
language: language, | ||
messages: messages, | ||
languageData: languageData, | ||
development: development | ||
}); | ||
_this.linguiPublisher = new LinguiPublisher(i18n); | ||
return _this; | ||
@@ -232,3 +225,3 @@ } | ||
if (language !== prevProps.language || messages !== prevProps.messages || languageData !== prevProps.languageData) { | ||
this.i18nManager.update({ language: language, messages: messages, languageData: languageData }); | ||
this.linguiPublisher.update({ language: language, messages: messages, languageData: languageData }); | ||
} | ||
@@ -240,3 +233,3 @@ } | ||
return { | ||
i18nManager: this.i18nManager | ||
linguiPublisher: this.linguiPublisher | ||
}; | ||
@@ -261,3 +254,3 @@ } | ||
I18nProvider.childContextTypes = { | ||
i18nManager: PropTypes.object.isRequired | ||
linguiPublisher: PropTypes.object.isRequired | ||
}; | ||
@@ -484,10 +477,5 @@ | ||
var InjectI18n = function InjectI18n(WrappedComponent) { | ||
console.warn('DEPRECATED (removal in 1.x): InjectI18n was replaced with WithI18n([ options ])'); | ||
return WithI18n()(WrappedComponent); | ||
}; | ||
var DateFormat = WithI18n()(createFormat(date)); | ||
var NumberFormat = WithI18n()(createFormat(number)); | ||
export { WithI18n, InjectI18n, DateFormat, NumberFormat, I18nProvider, Trans$1 as Trans, Plural$1 as Plural, Select }; | ||
export { WithI18n, DateFormat, NumberFormat, I18nProvider, Trans$1 as Trans, Plural$1 as Plural, Select }; |
@@ -27,6 +27,11 @@ 'use strict'; | ||
if (render) { | ||
// Built-in element: h1, p | ||
if (typeof render === 'string') { | ||
return React__default.createElement(render, {}, children); | ||
} | ||
return React__default.isValidElement(render) | ||
// $FlowIgnore: Don't know how to handle this type union | ||
// Custom element: <p className="lear' /> | ||
? React__default.cloneElement(render, {}, children) | ||
// $FlowIgnore: Don't know how to handle this type union | ||
// Custom component: ({ translation }) => <a title={translation}>x</a> | ||
: React__default.createElement(render, { translation: children }); | ||
@@ -62,4 +67,2 @@ } | ||
return function (WrappedComponent) { | ||
var _class, _temp2; | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -76,4 +79,3 @@ if (typeof options === 'function' || React__default.isValidElement(options)) { | ||
return _temp2 = _class = function (_React$Component) { | ||
var WithI18n = function (_React$Component) { | ||
_inherits(WithI18n, _React$Component); | ||
@@ -108,8 +110,6 @@ | ||
value: function componentDidMount() { | ||
if (update) { | ||
var _getI18n = this.getI18n(), | ||
subscribe = _getI18n.subscribe; | ||
var _getI18n = this.getI18n(), | ||
subscribe = _getI18n.subscribe; | ||
if (subscribe) subscribe(this.checkUpdate); | ||
} | ||
if (update && subscribe) subscribe(this.checkUpdate); | ||
} | ||
@@ -119,8 +119,6 @@ }, { | ||
value: function componentWillUnmount() { | ||
if (update) { | ||
var _getI18n2 = this.getI18n(), | ||
unsubscribe = _getI18n2.unsubscribe; | ||
var _getI18n2 = this.getI18n(), | ||
unsubscribe = _getI18n2.unsubscribe; | ||
if (unsubscribe) unsubscribe(this.checkUpdate); | ||
} | ||
if (update && unsubscribe) unsubscribe(this.checkUpdate); | ||
} | ||
@@ -130,3 +128,3 @@ }, { | ||
value: function getI18n() { | ||
return this.context.i18nManager || {}; | ||
return this.context.linguiPublisher || {}; | ||
} | ||
@@ -138,5 +136,3 @@ }, { | ||
i18n = _getI18n3.i18n; | ||
// $FlowIgnore: https://github.com/facebook/flow/issues/3241 | ||
return React__default.createElement(WrappedComponent, _extends({ ref: this.setWrappedInstance }, this.props, { i18n: i18n })); | ||
@@ -147,5 +143,12 @@ } | ||
return WithI18n; | ||
}(React__default.Component), _class.contextTypes = { | ||
i18nManager: PropTypes.object | ||
}, _temp2; | ||
}(React__default.Component); | ||
// return needs to be here, otherwise flow complains about {...this.props} | ||
// in WrappedComponent | ||
WithI18n.contextTypes = { | ||
linguiPublisher: PropTypes.object | ||
}; | ||
return WithI18n; | ||
}; | ||
@@ -155,56 +158,41 @@ }); | ||
/* | ||
* I18nManager - Connects to lingui-i18n/I18n class | ||
* I18nPublisher - Connects to lingui-i18n/I18n class | ||
* Allows listeners to subscribe for changes | ||
*/ | ||
var I18nManager = function I18nManager(_ref) { | ||
var language = _ref.language, | ||
messages = _ref.messages, | ||
languageData = _ref.languageData, | ||
development = _ref.development, | ||
i18n = _ref.i18n; | ||
function LinguiPublisher(i18n) { | ||
var subscribers = []; | ||
_classCallCheck(this, I18nManager); | ||
return { | ||
i18n: i18n, | ||
_initialiseProps.call(this); | ||
getSubscribers: function getSubscribers() { | ||
return subscribers; | ||
}, | ||
subscribe: function subscribe(callback) { | ||
subscribers.push(callback); | ||
}, | ||
unsubscribe: function unsubscribe(callback) { | ||
subscribers = subscribers.filter(function (cb) { | ||
return cb !== callback; | ||
}); | ||
}, | ||
update: function update() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
messages = _ref.messages, | ||
language = _ref.language, | ||
languageData = _ref.languageData; | ||
this.i18n = i18n || linguiI18n.setupI18n({ | ||
language: language, | ||
messages: messages, | ||
languageData: languageData, | ||
development: development | ||
}); | ||
}; | ||
if (!messages && !language && !languageData) return; | ||
var _initialiseProps = function _initialiseProps() { | ||
var _this2 = this; | ||
if (messages) i18n.load(messages); | ||
if (language) i18n.activate(language); | ||
if (languageData) i18n.loadLanguageData(languageData); | ||
this.subscribers = []; | ||
this.subscribe = function (callback) { | ||
_this2.subscribers.push(callback); | ||
subscribers.forEach(function (f) { | ||
return f(); | ||
}); | ||
} | ||
}; | ||
} | ||
this.unsubscribe = function (callback) { | ||
_this2.subscribers = _this2.subscribers.filter(function (cb) { | ||
return cb !== callback; | ||
}); | ||
}; | ||
this.update = function () { | ||
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
messages = _ref2.messages, | ||
language = _ref2.language, | ||
languageData = _ref2.languageData; | ||
if (!messages && !language && !languageData) return; | ||
if (messages) _this2.i18n.load(messages); | ||
if (language) _this2.i18n.activate(language); | ||
if (languageData) _this2.i18n.loadLanguageData(languageData); | ||
_this2.subscribers.forEach(function (f) { | ||
return f(); | ||
}); | ||
}; | ||
}; | ||
var I18nProvider = function (_React$Component) { | ||
@@ -222,6 +210,11 @@ _inherits(I18nProvider, _React$Component); | ||
languageData = _this$props.languageData, | ||
development = _this$props.development, | ||
i18n = _this$props.i18n; | ||
development = _this$props.development; | ||
_this.i18nManager = new I18nManager({ language: language, messages: messages, languageData: languageData, i18n: i18n, development: development }); | ||
var i18n = _this.props.i18n || linguiI18n.setupI18n({ | ||
language: language, | ||
messages: messages, | ||
languageData: languageData, | ||
development: development | ||
}); | ||
_this.linguiPublisher = new LinguiPublisher(i18n); | ||
return _this; | ||
@@ -239,3 +232,3 @@ } | ||
if (language !== prevProps.language || messages !== prevProps.messages || languageData !== prevProps.languageData) { | ||
this.i18nManager.update({ language: language, messages: messages, languageData: languageData }); | ||
this.linguiPublisher.update({ language: language, messages: messages, languageData: languageData }); | ||
} | ||
@@ -247,3 +240,3 @@ } | ||
return { | ||
i18nManager: this.i18nManager | ||
linguiPublisher: this.linguiPublisher | ||
}; | ||
@@ -268,3 +261,3 @@ } | ||
I18nProvider.childContextTypes = { | ||
i18nManager: PropTypes.object.isRequired | ||
linguiPublisher: PropTypes.object.isRequired | ||
}; | ||
@@ -491,7 +484,2 @@ | ||
var InjectI18n = function InjectI18n(WrappedComponent) { | ||
console.warn('DEPRECATED (removal in 1.x): InjectI18n was replaced with WithI18n([ options ])'); | ||
return WithI18n()(WrappedComponent); | ||
}; | ||
var DateFormat = WithI18n()(createFormat(linguiFormats.date)); | ||
@@ -501,3 +489,2 @@ var NumberFormat = WithI18n()(createFormat(linguiFormats.number)); | ||
exports.WithI18n = WithI18n; | ||
exports.InjectI18n = InjectI18n; | ||
exports.DateFormat = DateFormat; | ||
@@ -504,0 +491,0 @@ exports.NumberFormat = NumberFormat; |
{ | ||
"name": "lingui-react", | ||
"version": "1.0.0-3", | ||
"version": "1.0.0-4", | ||
"description": "React components for translations", | ||
@@ -37,4 +37,5 @@ "main": "index.js", | ||
"babel-runtime": "^6.23.0", | ||
"lingui-formats": "^1.0.0-3", | ||
"lingui-i18n": "^1.0.0-3" | ||
"lingui-formats": "^1.0.0-4", | ||
"lingui-i18n": "^1.0.0-4", | ||
"prop-types": "^15.5.10" | ||
}, | ||
@@ -41,0 +42,0 @@ "scripts": { |
@@ -8,3 +8,3 @@ // @flow | ||
describe('DateFormat', function () { | ||
const languageContext = (code) => ({ context: { i18nManager: { i18n: { language: code } } } }) | ||
const languageContext = (code) => ({ context: { linguiPublisher: { i18n: { language: code } } } }) | ||
@@ -11,0 +11,0 @@ it('should render', function () { |
@@ -8,3 +8,3 @@ /* @flow */ | ||
type I18nProviderProps = { | ||
export type I18nProviderProps = { | ||
children?: any, | ||
@@ -19,51 +19,58 @@ language: string, | ||
/* | ||
* I18nManager - Connects to lingui-i18n/I18n class | ||
* I18nPublisher - Connects to lingui-i18n/I18n class | ||
* Allows listeners to subscribe for changes | ||
*/ | ||
class I18nManager { | ||
i18n: I18n | ||
subscribers = [] | ||
export function LinguiPublisher (i18n: I18n) { | ||
let subscribers = [] | ||
constructor ({ language, messages, languageData, development, i18n }: { | ||
language: string, | ||
messages?: Catalogs, | ||
languageData?: AllLanguageData, | ||
development?: Object, | ||
i18n?: I18n | ||
}) { | ||
this.i18n = i18n || setupI18n({ | ||
language, | ||
messages, | ||
languageData, | ||
development | ||
}) | ||
} | ||
return { | ||
i18n, | ||
subscribe = (callback: Function) => { | ||
this.subscribers.push(callback) | ||
} | ||
getSubscribers () { | ||
return subscribers | ||
}, | ||
unsubscribe = (callback: Function) => { | ||
this.subscribers = this.subscribers.filter(cb => cb !== callback) | ||
} | ||
subscribe (callback: Function) { | ||
subscribers.push(callback) | ||
}, | ||
update = ({ messages, language, languageData }: { messages?: Catalogs, language?: string, languageData?: AllLanguageData } = {}) => { | ||
if (!messages && !language && !languageData) return | ||
unsubscribe (callback: Function) { | ||
subscribers = subscribers.filter(cb => cb !== callback) | ||
}, | ||
if (messages) this.i18n.load(messages) | ||
if (language) this.i18n.activate(language) | ||
if (languageData) this.i18n.loadLanguageData(languageData) | ||
this.subscribers.forEach(f => f()) | ||
update ({ messages, language, languageData }: { | ||
messages?: Catalogs, | ||
language?: string, | ||
languageData?: AllLanguageData | ||
} = {}) { | ||
if (!messages && !language && !languageData) return | ||
if (messages) i18n.load(messages) | ||
if (language) i18n.activate(language) | ||
if (languageData) i18n.loadLanguageData(languageData) | ||
subscribers.forEach(f => f()) | ||
} | ||
} | ||
} | ||
class I18nProvider extends React.Component { | ||
export default class I18nProvider extends React.Component { | ||
props: I18nProviderProps | ||
i18nManager: I18nManager | ||
linguiPublisher: LinguiPublisher | ||
static childContextTypes = { | ||
linguiPublisher: PropTypes.object.isRequired | ||
} | ||
constructor (props: I18nProviderProps) { | ||
super(props) | ||
const { language, messages, languageData, development, i18n } = this.props | ||
this.i18nManager = new I18nManager({ language, messages, languageData, i18n, development }) | ||
const { language, messages, languageData, development } = this.props | ||
const i18n = this.props.i18n || setupI18n({ | ||
language, | ||
messages, | ||
languageData, | ||
development | ||
}) | ||
this.linguiPublisher = new LinguiPublisher(i18n) | ||
} | ||
@@ -78,3 +85,3 @@ | ||
) { | ||
this.i18nManager.update({ language, messages, languageData }) | ||
this.linguiPublisher.update({ language, messages, languageData }) | ||
} | ||
@@ -85,3 +92,3 @@ } | ||
return { | ||
i18nManager: this.i18nManager | ||
linguiPublisher: this.linguiPublisher | ||
} | ||
@@ -95,9 +102,1 @@ } | ||
} | ||
I18nProvider.childContextTypes = { | ||
i18nManager: PropTypes.object.isRequired | ||
} | ||
export default I18nProvider | ||
export { I18nManager } | ||
export type { I18nProviderProps } |
@@ -6,5 +6,6 @@ // @flow | ||
import { I18nProvider } from '.' | ||
import { I18nManager } from './I18nProvider' | ||
import { LinguiPublisher } from './I18nProvider' | ||
import { mockConsole } from './mocks' | ||
import linguiDev from './dev' | ||
import { setupI18n } from 'lingui-i18n' | ||
@@ -26,7 +27,8 @@ describe('I18nProvider', function () { | ||
</I18nProvider>).instance() | ||
const { subscribe, unsubscribe, i18n } = component.getChildContext()['i18nManager'] | ||
expect(i18n.messages).toEqual(props.messages[props.language]) | ||
expect(i18n.language).toEqual(props.language) | ||
expect(subscribe).toBeInstanceOf(Function) | ||
expect(unsubscribe).toBeInstanceOf(Function) | ||
const linguiPublisher = component.getChildContext()['linguiPublisher'] | ||
expect(linguiPublisher.i18n.messages).toEqual(props.messages[props.language]) | ||
expect(linguiPublisher.i18n.language).toEqual(props.language) | ||
expect(linguiPublisher.subscribe).toBeInstanceOf(Function) | ||
expect(linguiPublisher.unsubscribe).toBeInstanceOf(Function) | ||
expect(linguiPublisher.update).toBeInstanceOf(Function) | ||
}) | ||
@@ -68,3 +70,3 @@ | ||
// $FlowIgnore - Instance returned from enzyme doesn't have custom attrs | ||
instance.i18nManager.subscribe(listener) | ||
instance.linguiPublisher.subscribe(listener) | ||
expect(listener).not.toBeCalled() | ||
@@ -80,5 +82,5 @@ | ||
describe('I18nManager', function () { | ||
describe('I18nPublisher', function () { | ||
it('should pass active language and messages to underlying I18n class', function () { | ||
const i18nManager = new I18nManager({ | ||
const linguiPublisher = new LinguiPublisher(setupI18n({ | ||
language: 'en', | ||
@@ -90,35 +92,35 @@ messages: { | ||
development: linguiDev | ||
}) | ||
})) | ||
expect(i18nManager.i18n.language).toEqual('en') | ||
expect(i18nManager.i18n.messages).toEqual({ msg: 'hello' }) | ||
expect(linguiPublisher.i18n.language).toEqual('en') | ||
expect(linguiPublisher.i18n.messages).toEqual({ msg: 'hello' }) | ||
i18nManager.update({ language: 'fr' }) | ||
expect(i18nManager.i18n.language).toEqual('fr') | ||
expect(i18nManager.i18n.messages).toEqual({ msg: 'salut' }) | ||
linguiPublisher.update({ language: 'fr' }) | ||
expect(linguiPublisher.i18n.language).toEqual('fr') | ||
expect(linguiPublisher.i18n.messages).toEqual({ msg: 'salut' }) | ||
i18nManager.update({ messages: { fr: { msg: 'salut!' } } }) | ||
expect(i18nManager.i18n.language).toEqual('fr') | ||
expect(i18nManager.i18n.messages).toEqual({ msg: 'salut!' }) | ||
linguiPublisher.update({ messages: { fr: { msg: 'salut!' } } }) | ||
expect(linguiPublisher.i18n.language).toEqual('fr') | ||
expect(linguiPublisher.i18n.messages).toEqual({ msg: 'salut!' }) | ||
i18nManager.update({ languageData: { fr: { plurals: () => 'Function' } } }) | ||
expect(i18nManager.i18n.language).toEqual('fr') | ||
expect(i18nManager.i18n.messages).toEqual({ msg: 'salut!' }) | ||
expect(i18nManager.i18n.languageData.plurals()).toEqual('Function') | ||
linguiPublisher.update({ languageData: { fr: { plurals: () => 'Function' } } }) | ||
expect(linguiPublisher.i18n.language).toEqual('fr') | ||
expect(linguiPublisher.i18n.messages).toEqual({ msg: 'salut!' }) | ||
expect(linguiPublisher.i18n.languageData.plurals()).toEqual('Function') | ||
}) | ||
it('should subscribe/unsubscribe listeners for context changes', function () { | ||
const i18nManager = new I18nManager({ | ||
const linguiPublisher = new LinguiPublisher(setupI18n({ | ||
language: 'en', | ||
messages: { en: {}, fr: {} } | ||
}) | ||
})) | ||
const listener = jest.fn() | ||
expect(i18nManager.subscribers).toEqual([]) | ||
expect(linguiPublisher.getSubscribers()).toEqual([]) | ||
i18nManager.subscribe(listener) | ||
expect(i18nManager.subscribers).toEqual([listener]) | ||
linguiPublisher.subscribe(listener) | ||
expect(linguiPublisher.getSubscribers()).toEqual([listener]) | ||
i18nManager.unsubscribe(listener) | ||
expect(i18nManager.subscribers).toEqual([]) | ||
linguiPublisher.unsubscribe(listener) | ||
expect(linguiPublisher.getSubscribers()).toEqual([]) | ||
}) | ||
@@ -128,24 +130,24 @@ | ||
const listener = jest.fn() | ||
const i18nManager = new I18nManager({ | ||
const linguiPublisher = new LinguiPublisher(setupI18n({ | ||
language: 'en', | ||
messages: { en: {}, fr: {} }, | ||
development: linguiDev | ||
}) | ||
i18nManager.subscribe(listener) | ||
})) | ||
linguiPublisher.subscribe(listener) | ||
expect(listener).not.toBeCalled() | ||
i18nManager.update() | ||
linguiPublisher.update() | ||
expect(listener).not.toBeCalled() | ||
i18nManager.update({ completelyDifferentProp: 42 }) | ||
linguiPublisher.update({ completelyDifferentProp: 42 }) | ||
expect(listener).not.toBeCalled() | ||
i18nManager.update({ language: 'fr' }) | ||
linguiPublisher.update({ language: 'fr' }) | ||
expect(listener).toBeCalled() | ||
listener.mockReset() | ||
i18nManager.update({ messages: { en: { id: 'hello' } } }) | ||
linguiPublisher.update({ messages: { en: { id: 'hello' } } }) | ||
expect(listener).toBeCalled() | ||
}) | ||
}) |
@@ -16,8 +16,3 @@ // @flow | ||
export const InjectI18n = (WrappedComponent: any) => { | ||
console.warn('DEPRECATED (removal in 1.x): InjectI18n was replaced with WithI18n([ options ])') | ||
return WithI18n()(WrappedComponent) | ||
} | ||
export const DateFormat = WithI18n()(createFormat(date)) | ||
export const NumberFormat = WithI18n()(createFormat(number)) |
@@ -8,3 +8,3 @@ // @flow | ||
describe('NumberFormat', function () { | ||
const languageContext = (code) => ({ context: { i18nManager: { i18n: { language: code } } } }) | ||
const languageContext = (code) => ({ context: { linguiPublisher: { i18n: { language: code } } } }) | ||
@@ -11,0 +11,0 @@ it('should render', function () { |
@@ -15,3 +15,3 @@ // @flow | ||
}) | ||
const languageContext = (code) => ({ context: { i18nManager: { i18n: i18n(code) } } }) | ||
const languageContext = (code) => ({ context: { linguiPublisher: { i18n: i18n(code) } } }) | ||
@@ -18,0 +18,0 @@ it('should render translation inside custom component', function () { |
@@ -5,3 +5,3 @@ // @flow | ||
export type RenderProps = { | ||
render?: Function | React$Element<*> | ReactClass<*>, | ||
render?: any, | ||
className?: string | ||
@@ -16,6 +16,11 @@ } | ||
if (render) { | ||
// Built-in element: h1, p | ||
if (typeof render === 'string') { | ||
return React.createElement(render, {}, children) | ||
} | ||
return React.isValidElement(render) | ||
// $FlowIgnore: Don't know how to handle this type union | ||
// Custom element: <p className="lear' /> | ||
? React.cloneElement(render, {}, children) | ||
// $FlowIgnore: Don't know how to handle this type union | ||
// Custom component: ({ translation }) => <a title={translation}>x</a> | ||
: React.createElement(render, { translation: children }) | ||
@@ -22,0 +27,0 @@ } |
@@ -15,3 +15,3 @@ // @flow | ||
}) | ||
const languageContext = (code) => ({ context: { i18nManager: { i18n: i18n(code) } } }) | ||
const languageContext = (code) => ({ context: { linguiPublisher: { i18n: i18n(code) } } }) | ||
@@ -18,0 +18,0 @@ it('should render ordinal correctly', function () { |
@@ -28,3 +28,3 @@ /* @flow */ | ||
const context = { i18nManager: { i18n } } | ||
const context = { linguiPublisher: { i18n } } | ||
const text = (node) => mount(node, { context }).find('Render').text() | ||
@@ -31,0 +31,0 @@ |
@@ -15,3 +15,3 @@ // @flow | ||
export default (options: WithI18nOptions = {}) => function<P, C: React$Component<*, P, *>> (WrappedComponent: Class<C>): Class<React.Component<void, $Diff<P, WithI18nProps>, void>> { | ||
export default (options: WithI18nOptions = {}) => function<P, C: React$Component<any, P, any>> (WrappedComponent: Class<C>): Class<React.Component<any, $Diff<P, WithI18nProps>, any>> { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -29,5 +29,5 @@ if (typeof options === 'function' || React.isValidElement(options)) { | ||
return class WithI18n extends React.Component { | ||
class WithI18n extends React.Component<*, *, *> { | ||
static contextTypes = { | ||
i18nManager: PropTypes.object | ||
linguiPublisher: PropTypes.object | ||
} | ||
@@ -53,13 +53,9 @@ | ||
componentDidMount () { | ||
if (update) { | ||
const { subscribe } = this.getI18n() | ||
if (subscribe) subscribe(this.checkUpdate) | ||
} | ||
const { subscribe } = this.getI18n() | ||
if (update && subscribe) subscribe(this.checkUpdate) | ||
} | ||
componentWillUnmount () { | ||
if (update) { | ||
const { unsubscribe } = this.getI18n() | ||
if (unsubscribe) unsubscribe(this.checkUpdate) | ||
} | ||
const { unsubscribe } = this.getI18n() | ||
if (update && unsubscribe) unsubscribe(this.checkUpdate) | ||
} | ||
@@ -72,3 +68,3 @@ | ||
getI18n () { | ||
return this.context.i18nManager || {} | ||
return this.context.linguiPublisher || {} | ||
} | ||
@@ -78,8 +74,11 @@ | ||
const { i18n } = this.getI18n() | ||
// $FlowIgnore: https://github.com/facebook/flow/issues/3241 | ||
return <WrappedComponent ref={this.setWrappedInstance} {...this.props} i18n={i18n} /> | ||
} | ||
} | ||
// return needs to be here, otherwise flow complains about {...this.props} | ||
// in WrappedComponent | ||
return WithI18n | ||
} | ||
export type { WithI18nProps } |
@@ -36,3 +36,3 @@ // @flow | ||
const { Sink, spy } = sinkFactory(hocOptions) | ||
const node = mount(<Sink {...props} />, { context: { i18nManager: context } }) | ||
const node = mount(<Sink {...props} />, { context: { linguiPublisher: context } }) | ||
const receivedProps = spy.mock.calls[spy.mock.calls.length - 1][0] | ||
@@ -39,0 +39,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46
98719
5
1847
+ Addedprop-types@^15.5.10
Updatedlingui-formats@^1.0.0-4
Updatedlingui-i18n@^1.0.0-4