Socket
Socket
Sign inDemoInstall

lingui-react

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lingui-react - npm Package Compare versions

Comparing version 0.12.0 to 0.13.0-0

src/__snapshots__/I18nProvider.test.js.snap

548

lib/index.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.NumberFormat = exports.DateFormat = exports.InjectI18n = exports.Select = exports.Plural = exports.Trans = exports.I18nProvider = exports.WithI18n = undefined;
Object.defineProperty(exports, '__esModule', { value: true });
var _I18nProvider = require('./I18nProvider');
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
Object.defineProperty(exports, 'I18nProvider', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_I18nProvider).default;
var linguiFormats = require('lingui-formats');
var React = require('react');
var React__default = _interopDefault(React);
var _extends = _interopDefault(require('babel-runtime/helpers/extends'));
var _Object$getPrototypeOf = _interopDefault(require('babel-runtime/core-js/object/get-prototype-of'));
var _classCallCheck = _interopDefault(require('babel-runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('babel-runtime/helpers/createClass'));
var _possibleConstructorReturn = _interopDefault(require('babel-runtime/helpers/possibleConstructorReturn'));
var _inherits = _interopDefault(require('babel-runtime/helpers/inherits'));
var PropTypes = _interopDefault(require('prop-types'));
var linguiI18n = require('lingui-i18n');
var _getIterator = _interopDefault(require('babel-runtime/core-js/get-iterator'));
var _slicedToArray = _interopDefault(require('babel-runtime/helpers/slicedToArray'));
var Render = function Render(_ref) {
var render = _ref.render,
className = _ref.className,
children = _ref.children;
if (render) {
return React__default.isValidElement(render)
// $FlowIgnore: Don't know how to handle this type union
? React__default.cloneElement(render, {}, children)
// $FlowIgnore: Don't know how to handle this type union
: React__default.createElement(render, { translation: children });
}
return React__default.createElement(
'span',
{ className: className },
children
);
};
function createFormat(formatFunction) {
return function (_ref) {
var value = _ref.value,
format = _ref.format,
i18n = _ref.i18n,
className = _ref.className,
render = _ref.render;
var formatter = formatFunction(i18n.language, format);
return React__default.createElement(
Render,
{ className: className, render: render },
formatter(value)
);
};
}
var WithI18n = (function () {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return function (WrappedComponent) {
var _class, _temp2;
if (process.env.NODE_ENV !== 'production') {
if (typeof options === 'function' || React__default.isValidElement(options)) {
console.warn('WithI18n([options]) takes options as a first argument, ' + 'but received React component itself. Without options, the Component ' + 'should be wrapped as WithI18n()(Component), not WithI18n(Component).');
}
}
var _options$update = options.update,
update = _options$update === undefined ? true : _options$update,
_options$withRef = options.withRef,
withRef = _options$withRef === undefined ? false : _options$withRef;
return _temp2 = _class = function (_React$Component) {
_inherits(WithI18n, _React$Component);
function WithI18n() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, WithI18n);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = WithI18n.__proto__ || _Object$getPrototypeOf(WithI18n)).call.apply(_ref, [this].concat(args))), _this), _this.wrappedInstance = null, _this.setWrappedInstance = function (ref) {
if (withRef) _this.wrappedInstance = ref;
}, _this.getWrappedInstance = function () {
if (!withRef) {
throw new Error('To access the wrapped instance, you need to specify { withRef: true }' + ' in the options argument of the withI18n() call.');
}
return _this.wrappedInstance;
}, _this.checkUpdate = function () {
_this.forceUpdate();
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(WithI18n, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (update) {
var _getI18n = this.getI18n(),
subscribe = _getI18n.subscribe;
if (subscribe) subscribe(this.checkUpdate);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (update) {
var _getI18n2 = this.getI18n(),
unsubscribe = _getI18n2.unsubscribe;
if (unsubscribe) unsubscribe(this.checkUpdate);
}
}
}, {
key: 'getI18n',
value: function getI18n() {
return this.context.i18nManager || {};
}
}, {
key: 'render',
value: function render() {
var _getI18n3 = this.getI18n(),
i18n = _getI18n3.i18n;
// $FlowIgnore: https://github.com/facebook/flow/issues/3241
return React__default.createElement(WrappedComponent, _extends({ ref: this.setWrappedInstance }, this.props, { i18n: i18n }));
}
}]);
return WithI18n;
}(React__default.Component), _class.contextTypes = {
i18nManager: PropTypes.object
}, _temp2;
};
});
var _Trans = require('./Trans');
/*
* I18nManager - 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,
i18n = _ref.i18n;
Object.defineProperty(exports, 'Trans', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_Trans).default;
_classCallCheck(this, I18nManager);
_initialiseProps.call(this);
this.i18n = i18n || new linguiI18n.I18n(language, messages, languageData);
};
var _initialiseProps = function _initialiseProps() {
var _this2 = this;
this.subscribers = [];
this.subscribe = function (callback) {
_this2.subscribers.push(callback);
};
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) {
_inherits(I18nProvider, _React$Component);
function I18nProvider(props) {
_classCallCheck(this, I18nProvider);
var _this = _possibleConstructorReturn(this, (I18nProvider.__proto__ || _Object$getPrototypeOf(I18nProvider)).call(this, props));
var _this$props = _this.props,
language = _this$props.language,
messages = _this$props.messages,
languageData = _this$props.languageData,
i18n = _this$props.i18n;
_this.i18nManager = new I18nManager({ language: language, messages: messages, languageData: languageData, i18n: i18n });
return _this;
}
});
var _Plural = require('./Plural');
_createClass(I18nProvider, [{
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps) {
var _props = this.props,
language = _props.language,
messages = _props.messages,
languageData = _props.languageData;
Object.defineProperty(exports, 'Plural', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_Plural).default;
if (language !== prevProps.language || messages !== prevProps.messages || languageData !== prevProps.languageData) {
this.i18nManager.update({ language: language, messages: messages, languageData: languageData });
}
}
}, {
key: 'getChildContext',
value: function getChildContext() {
return {
i18nManager: this.i18nManager
};
}
}, {
key: 'render',
value: function render() {
var children = this.props.children;
return children && children.length > 1 ? React__default.createElement(
'div',
null,
children
) : children;
}
}]);
return I18nProvider;
}(React__default.Component);
I18nProvider.childContextTypes = {
i18nManager: PropTypes.object.isRequired
};
// match <0>paired</0> and <1/> unpaired tags
var tagRe = /<(\d+)>(.*)<\/\1>|<(\d+)\/>/;
/**
* `formatElements` - parse string and return tree of react elements
*
* `value` is string to be formatted with <0>Paired<0/> or <0/> (unpaired)
* placeholders. `elements` is a array of react elements which indexes
* correspond to element indexes in formatted string
*/
function formatElements(value) {
var elements = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
// TODO: warn if there're any unprocessed elements
// TODO: warn if element at `index` doesn't exist
var parts = value.split(tagRe);
// no inline elements, return
if (parts.length === 1) return value;
var tree = [];
var before = parts.shift();
if (before) tree.push(before);
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = _getIterator(getElements(parts)), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _ref = _step.value;
var _ref2 = _slicedToArray(_ref, 3);
var index = _ref2[0];
var children = _ref2[1];
var after = _ref2[2];
tree.push(React.cloneElement(elements[index], { key: index },
// unpaired tag shouldn't receive children (return null instead)
children ? formatElements(children, elements) : null));
if (after) tree.push(after);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
});
var _Select = require('./Select');
return tree;
}
Object.defineProperty(exports, 'Select', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_Select).default;
/*
* `getElements` - return array of element indexes and element childrens
*
* `parts` is array of [pairedIndex, children, unpairedIndex, textAfter, ...]
* where:
* - `pairedIndex` is index of paired element (undef for unpaired)
* - `children` are children of paired element (undef for unpaired)
* - `unpairedIndex` is index of unpaired element (undef for paired)
* - `textAfter` is string after all elements (empty string, if there's nothing)
*
* `parts` length is always multiply of 4
*
* Returns: Array<[elementIndex, children, after]>
*/
function getElements(parts) {
if (!parts.length) return [];
var _parts$slice = parts.slice(0, 4),
_parts$slice2 = _slicedToArray(_parts$slice, 4),
paired = _parts$slice2[0],
children = _parts$slice2[1],
unpaired = _parts$slice2[2],
after = _parts$slice2[3];
return [[parseInt(paired || unpaired), children || '', after]].concat(getElements(parts.slice(4, parts.length)));
}
var Trans = function (_React$Component) {
_inherits(Trans, _React$Component);
function Trans(props) {
_classCallCheck(this, Trans);
var _this = _possibleConstructorReturn(this, (Trans.__proto__ || _Object$getPrototypeOf(Trans)).call(this, props));
var translation = _this.getTranslation(props);
if (process.env.NODE_ENV !== 'production') {
if (!translation && props.children) {
console.warn('lingui-react preset is probably missing in babel config. ' + 'It causes that no content is rendered from any lingui-react ' + 'components. See installation guide for more info - ' + 'https://l.lingui.io/tutorial-i18n-react');
}
}
_this.state = {
msgCache: _this.compileMessage(translation),
language: props.i18n.language,
translation: translation
};
return _this;
}
});
var _linguiFormats = require('lingui-formats');
_createClass(Trans, [{
key: 'getTranslation',
value: function getTranslation(props) {
var _props$id = props.id,
id = _props$id === undefined ? '' : _props$id,
defaults = props.defaults,
i18n = props.i18n;
var _createFormat = require('./createFormat');
var _createFormat2 = _interopRequireDefault(_createFormat);
return (i18n.messages && id ? i18n.messages[id] : '') || defaults || id;
}
}, {
key: 'compileMessage',
value: function compileMessage(translation) {
var _props = this.props,
i18n = _props.i18n,
formats = _props.formats;
var _WithI18n = require('./WithI18n');
var _WithI18n2 = _interopRequireDefault(_WithI18n);
if (!i18n.compile) return function () {
return translation;
};
return i18n.compile(translation, formats);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
var i18n = this.props.i18n;
var _state = this.state,
language = _state.language,
translation = _state.translation;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var nextTranslation = this.getTranslation(nextProps);
exports.WithI18n = _WithI18n2.default;
var InjectI18n = exports.InjectI18n = function InjectI18n(WrappedComponent) {
if (translation !== nextTranslation || language !== i18n.language) {
this.setState({
msgCache: this.compileMessage(nextTranslation),
language: i18n.language,
translation: nextTranslation
});
}
}
}, {
key: 'render',
value: function render() {
var _props2 = this.props,
params = _props2.params,
components = _props2.components;
var msgCache = this.state.msgCache;
var translation = formatElements(msgCache(params), components);
var _props3 = this.props,
className = _props3.className,
render = _props3.render;
return React__default.createElement(
Render,
{ className: className, render: render },
translation
);
}
}]);
return Trans;
}(React__default.Component);
Trans.defaultProps = {
id: '',
i18n: {}
};
var Trans$1 = WithI18n()(Trans);
var Plural = function (_React$Component) {
_inherits(Plural, _React$Component);
function Plural() {
_classCallCheck(this, Plural);
return _possibleConstructorReturn(this, (Plural.__proto__ || _Object$getPrototypeOf(Plural)).apply(this, arguments));
}
_createClass(Plural, [{
key: 'render',
value: function render() {
var _props = this.props,
value = _props.value,
offset = _props.offset,
i18n = _props.i18n;
var n = parseInt(value) - parseInt(offset);
var form = i18n.pluralForm(n);
var translation = (this.props['_' + value] || this.props[form]).replace('#', n);
var _props2 = this.props,
className = _props2.className,
render = _props2.render;
return React__default.createElement(
Render,
{ className: className, render: render },
translation
);
}
}]);
return Plural;
}(React__default.Component);
Plural.defaultProps = {
offset: 0
};
var Plural$1 = WithI18n()(Plural);
var Select = function (_React$Component) {
_inherits(Select, _React$Component);
function Select() {
_classCallCheck(this, Select);
return _possibleConstructorReturn(this, (Select.__proto__ || _Object$getPrototypeOf(Select)).apply(this, arguments));
}
_createClass(Select, [{
key: 'render',
value: function render() {
var _props = this.props,
value = _props.value,
other = _props.other;
var translation = this.props[value] || other;
var _props2 = this.props,
className = _props2.className,
render = _props2.render;
return React__default.createElement(
Render,
{ className: className, render: render },
translation
);
}
}]);
return Select;
}(React__default.Component);
var InjectI18n = function InjectI18n(WrappedComponent) {
console.warn('DEPRECATED (removal in 1.x): InjectI18n was replaced with WithI18n([ options ])');
return (0, _WithI18n2.default)()(WrappedComponent);
return WithI18n()(WrappedComponent);
};
var DateFormat = exports.DateFormat = (0, _WithI18n2.default)()((0, _createFormat2.default)(_linguiFormats.date));
var NumberFormat = exports.NumberFormat = (0, _WithI18n2.default)()((0, _createFormat2.default)(_linguiFormats.number));
var DateFormat = WithI18n()(createFormat(linguiFormats.date));
var NumberFormat = WithI18n()(createFormat(linguiFormats.number));
exports.WithI18n = WithI18n;
exports.InjectI18n = InjectI18n;
exports.DateFormat = DateFormat;
exports.NumberFormat = NumberFormat;
exports.I18nProvider = I18nProvider;
exports.Trans = Trans$1;
exports.Plural = Plural$1;
exports.Select = Select;

8

package.json
{
"name": "lingui-react",
"version": "0.12.0",
"version": "0.13.0-0",
"description": "React components for translations",
"main": "lib/index.js",
"module": "src/index.js",
"author": {

@@ -35,4 +36,5 @@ "name": "Tomáš Ehrlich",

"dependencies": {
"lingui-formats": "^0.4.0",
"lingui-i18n": "^0.6.1",
"babel-runtime": "^6.23.0",
"lingui-formats": "^0.4.0-0",
"lingui-i18n": "^0.7.0-0",
"prop-types": "^15.5.10"

@@ -39,0 +41,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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc