'use strict'; /** * Simple module to localize the React interface using the same syntax * used in the ReactNativeLocalization module * (https://github.com/stefalda/ReactNativeLocalization) * * Originally developed by Stefano Falda (stefano.falda@gmail.com) * * It uses a call to the Navigator/Browser object to get the current interface language, * then display the correct language strings or the default language (the first * one if a match is not found). * * This library has been refactored to use the newly created localized-strings package so to * unify the code and make it easier to mantain * * How to use: * Check the instructions at: * https://github.com/stefalda/react-localization */ Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _localizedStrings = require('localized-strings'); var _localizedStrings2 = _interopRequireDefault(_localizedStrings); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var placeholderRegex = /(\{[\d|\w]+\})/; /** * Format the passed string replacing the numbered or tokenized placeholders * eg. 1: I'd like some {0} and {1}, or just {0} * eg. 2: I'd like some {bread} and {butter}, or just {bread} * Use example: * eg. 1: strings.formatString(strings.question, strings.bread, strings.butter) * eg. 2: strings.formatString(strings.question, { bread: strings.bread, butter: strings.butter } * * THIS METHOD OVERRIDE the one of the parent class by adding support for JSX code */ _localizedStrings2.default.prototype.formatString = function (str) { for (var _len = arguments.length, valuesForPlaceholders = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { valuesForPlaceholders[_key - 1] = arguments[_key]; } var hasObject = false; var res = (str || '').split(placeholderRegex).filter(function (textPart) { return !!textPart; }).map(function (textPart, index) { if (textPart.match(placeholderRegex)) { var matchedKey = textPart.slice(1, -1); var valueForPlaceholder = valuesForPlaceholders[matchedKey]; // If no value found, check if working with an object instead if (valueForPlaceholder == undefined) { var valueFromObjectPlaceholder = valuesForPlaceholders[0][matchedKey]; if (valueFromObjectPlaceholder !== undefined) { valueForPlaceholder = valueFromObjectPlaceholder; } else { // If value still isn't found, then it must have been undefined/null return valueForPlaceholder; } } if (_react2.default.isValidElement(valueForPlaceholder)) { hasObject = true; return _react2.default.Children.toArray(valueForPlaceholder).map(function (component) { return _extends({}, component, { key: index.toString() }); }); } return valueForPlaceholder; } return textPart; }); // If the results contains a object return an array otherwise return a string if (hasObject) return res; return res.join(''); }; exports.default = _localizedStrings2.default;