UNPKG

uppy

Version:

Extensible JavaScript file upload widget with support for drag&drop, resumable uploads, previews, restrictions, file processing/encoding, remote providers like Instagram, Dropbox, Google Drive, S3 and more :dog:

101 lines (85 loc) 3.8 kB
'use strict'; 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; }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Translates strings with interpolation & pluralization support. * Extensible with custom dictionaries and pluralization functions. * * Borrows heavily from and inspired by Polyglot https://github.com/airbnb/polyglot.js, * basically a stripped-down version of it. Differences: pluralization functions are not hardcoded * and can be easily added among with dictionaries, nested objects are used for pluralization * as opposed to `||||` delimeter * * Usage example: `translator.translate('files_chosen', {smart_count: 3})` * * @param {object} opts */ module.exports = function () { function Translator(opts) { _classCallCheck(this, Translator); var defaultOptions = { locale: { strings: {}, pluralize: function pluralize(n) { if (n === 1) { return 0; } return 1; } } }; this.opts = _extends({}, defaultOptions, opts); this.locale = _extends({}, defaultOptions.locale, opts.locale); // console.log(this.opts.locale) // this.locale.pluralize = this.locale ? this.locale.pluralize : defaultPluralize // this.locale.strings = Object.assign({}, en_US.strings, this.opts.locale.strings) } /** * Takes a string with placeholder variables like `%{smart_count} file selected` * and replaces it with values from options `{smart_count: 5}` * * @license https://github.com/airbnb/polyglot.js/blob/master/LICENSE * taken from https://github.com/airbnb/polyglot.js/blob/master/lib/polyglot.js#L299 * * @param {string} phrase that needs interpolation, with placeholders * @param {object} options with values that will be used to replace placeholders * @return {string} interpolated */ Translator.prototype.interpolate = function interpolate(phrase, options) { var replace = String.prototype.replace; var dollarRegex = /\$/g; var dollarBillsYall = '$$$$'; for (var arg in options) { if (arg !== '_' && options.hasOwnProperty(arg)) { // Ensure replacement value is escaped to prevent special $-prefixed // regex replace tokens. the "$$$$" is needed because each "$" needs to // be escaped with "$" itself, and we need two in the resulting output. var replacement = options[arg]; if (typeof replacement === 'string') { replacement = replace.call(options[arg], dollarRegex, dollarBillsYall); } // We create a new `RegExp` each time instead of using a more-efficient // string replace so that the same argument can be replaced multiple times // in the same phrase. phrase = replace.call(phrase, new RegExp('%\\{' + arg + '\\}', 'g'), replacement); } } return phrase; }; /** * Public translate method * * @param {string} key * @param {object} options with values that will be used later to replace placeholders in string * @return {string} translated (and interpolated) */ Translator.prototype.translate = function translate(key, options) { if (options && options.smart_count) { var plural = this.locale.pluralize(options.smart_count); return this.interpolate(this.opts.locale.strings[key][plural], options); } return this.interpolate(this.opts.locale.strings[key], options); }; return Translator; }(); //# sourceMappingURL=Translator.js.map