1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- /**
- * 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 = class Translator {
- constructor (opts) {
- const defaultOptions = {
- locale: {
- strings: {},
- pluralize: function (n) {
- if (n === 1) {
- return 0
- }
- return 1
- }
- }
- }
- this.opts = Object.assign({}, defaultOptions, opts)
- this.locale = Object.assign({}, 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
- */
- interpolate (phrase, options) {
- const replace = String.prototype.replace
- const dollarRegex = /\$/g
- const dollarBillsYall = '$$$$'
- for (let 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)
- */
- 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)
- }
- }
|