Skip to content

Commit

Permalink
Switch to using make-plural/plurals pre-compiled functions
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Apr 4, 2015
1 parent eaf2b20 commit 22ed78c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 57 deletions.
9 changes: 4 additions & 5 deletions bin/messageformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,10 @@ function build(options, callback) {
mf = new MessageFormat(lc[0]),
messages = {},
compileOpt = { global: options.namespace, locale: {} };
for (var i = 1; i < lc.length; ++i) {
var pf = MessageFormat.getPluralFunc([lc[i]]);
if (!pf) throw 'Plural function for locale `' + lc[i] + '` could not be loaded';
mf.runtime.pluralFuncs[lc[i]] = pf;
}
lc.slice(1).forEach(function(l){
var pf = mf.runtime.pluralFuncs[l] = MessageFormat.plurals[l];
if (!pf) throw 'Plural function for locale `' + l + '` not found';
});
_log('Input dir: ' + options.inputdir);
_log('Included locales: ' + lc.join(', '));
glob(options.include, {cwd: options.inputdir}, function(err, files) {
Expand Down
72 changes: 21 additions & 51 deletions lib/messageformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

(function ( root ) {

var Parser = require('./messageformat-parser'),
pluralRules = require('make-plural/data/plurals.json'),
ordinalRules = require('make-plural/data/ordinals.json'),
MakePlural = require('make-plural').load(pluralRules, ordinalRules);


/**
* Create a new message formatter
*
Expand All @@ -34,8 +28,12 @@
this.lc = locale;
}
if (!pluralFunc) {
pluralFunc = MessageFormat.getPluralFunc(this.lc);
if (!pluralFunc) throw 'Plural function for locale `' + this.lc.join(',') + '` could not be loaded';
if (this.lc.every(function(l){
pluralFunc = MessageFormat.plurals[l];
return !pluralFunc;
})) {
throw new Error('Plural function for locale `' + this.lc.join(',') + '` not found');
}
}
this.runtime.pluralFuncs = {};
this.runtime.pluralFuncs[this.lc[0]] = pluralFunc;
Expand All @@ -45,55 +43,28 @@
}
}


/**
* Publicly-accessible cache of pluralization functions, this is normally
* filled by the internal `getPluralFunc()` function, but may be set
* externally if e.g. the external dependency {@link
* http://github.com/eemeli/make-plural.js make-plural} is not available.
* Parse an input string to its AST
*
* Precompiled from `lib/messageformat-parser.pegjs` by
* {@link http://pegjs.org/ PEG.js}.
*
* @private
* @memberof MessageFormat
* @type Object.<string,function>
* @example
* > var MessageFormat = require('messageformat');
* > MessageFormat.plurals.en = function(n) { // cardinal plurals only
* return (n == 1 && !String(n).split('.')[1]) ? 'one' : 'other';
* };
* > var mf = new MessageFormat('en');
* > var mfunc = mf.compile('You have {N, plural, one{1 item} other{# items}.');
* > mfunc({N:'1.0'})
* "You have 1.0 items."
* @type Function
*/
MessageFormat.plurals = {};
MessageFormat._parse = require('./messageformat-parser').parse;

/**
* Look up the plural formatting function for a given locale code.
* Pluralization functions from
* {@link http://github.com/eemeli/make-plural.js make-plural}
*
* If the {@link http://github.com/eemeli/make-plural.js make-plural module}
* is not available, the {@link MessageFormat.plurals} object will need to be
* pre-populated for this to work.
*
* @private
* @memberof MessageFormat
* @requires module:eemeli/make-plural.js
* @param {string[]} locale - A preferentially ordered array of locale codes
* @returns {function} The first match found for the given locale(s)
* @type Object.<string,function>
*/
MessageFormat.getPluralFunc = function(locale) {
for (var i = 0; i < locale.length; ++i) {
var lc = locale[i];
if (lc in MessageFormat.plurals) {
return MessageFormat.plurals[lc];
}
try {
var fn = new MakePlural(lc, {ordinals:1, quiet:1});
MessageFormat.plurals[lc] = fn;
return fn;
} catch (e) {
if (e.message.indexOf('rules not found') == -1) throw e;
}
}
return null;
}
MessageFormat.plurals = require('make-plural/plurals');


/**
* Default number formatting functions in the style of ICU's
Expand Down Expand Up @@ -182,6 +153,7 @@
return this;
};


/**
* A set of utility functions that are called by the compiled Javascript
* functions, these are included locally in the output of {@link
Expand Down Expand Up @@ -265,9 +237,6 @@
}
};

/** Parse an input string to its AST
* @private */
MessageFormat._parse = Parser.parse;

/** Utility function for quoting an Object's key value iff required
* @private */
Expand All @@ -280,6 +249,7 @@
}
};


/** Recursively map an AST to its resulting string
* @private */
MessageFormat.prototype._precompile = function(ast, data) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"async": "~0.2.10",
"glob": "~3.2.8",
"make-plural": "3.0.0-rc2",
"make-plural": "^3.0.0-rc4",
"nopt": "~2.1.2",
"watchr": "~2.4.9"
},
Expand Down

0 comments on commit 22ed78c

Please sign in to comment.