Skip to content

Commit 4741c9a

Browse files
committed
feat(messageformat): Add internal options object
1 parent a5f59f1 commit 4741c9a

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

packages/messageformat/src/compiler.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse } from 'messageformat-parser';
2-
import { bidiMarkText, funcname, propname } from './utils';
2+
import { biDiMarkText, funcname, propname } from './utils';
33

44
/** @private */
55
export default class Compiler {
@@ -35,7 +35,7 @@ export default class Compiler {
3535
if (typeof src != 'object') {
3636
this.lc = lc;
3737
const pc = plurals[lc] || { cardinal: [], ordinal: [] };
38-
pc.strict = !!this.mf.strictNumberSign;
38+
pc.strict = !!this.mf.options.strictNumberSign;
3939
const r = parse(src, pc).map(token => this.token(token));
4040
return `function(d) { return ${r.join(' + ') || '""'}; }`;
4141
} else {
@@ -69,11 +69,13 @@ export default class Compiler {
6969
let args = [propname(token.arg, 'd')];
7070
switch (token.type) {
7171
case 'argument':
72-
return this.mf.bidiSupport ? bidiMarkText(args[0], this.lc) : args[0];
72+
return this.mf.options.biDiSupport
73+
? biDiMarkText(args[0], this.lc)
74+
: args[0];
7375

7476
case 'select':
7577
fn = 'select';
76-
if (plural && this.mf.strictNumberSign) plural = null;
78+
if (plural && this.mf.options.strictNumberSign) plural = null;
7779
args.push(this.cases(token, plural));
7880
this.runtime.select = true;
7981
break;
@@ -110,7 +112,7 @@ export default class Compiler {
110112
);
111113
args.push(JSON.stringify(this.lc));
112114
if (token.param) {
113-
if (plural && this.mf.strictNumberSign) plural = null;
115+
if (plural && this.mf.options.strictNumberSign) plural = null;
114116
const s = token.param.tokens.map(tok => this.token(tok, plural));
115117
args.push('(' + (s.join(' + ') || '""') + ').trim()');
116118
}

packages/messageformat/src/messageformat.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,18 @@ export default class MessageFormat {
6161
* ```
6262
*/
6363
constructor(locale) {
64+
this.options = {
65+
biDiSupport: false,
66+
pluralKeyChecks: true,
67+
strictNumberSign: false
68+
};
6469
this.pluralFuncs = {};
6570
if (typeof locale === 'string') {
66-
this.pluralFuncs[locale] = getPlural(locale);
71+
this.pluralFuncs[locale] = getPlural(locale, this.options);
6772
this.defaultLocale = locale;
6873
} else if (Array.isArray(locale)) {
6974
locale.forEach(lc => {
70-
this.pluralFuncs[lc] = getPlural(lc);
75+
this.pluralFuncs[lc] = getPlural(lc, this.options);
7176
});
7277
this.defaultLocale = locale[0];
7378
} else {
@@ -85,6 +90,7 @@ export default class MessageFormat {
8590
this.hasCustomPluralFuncs = true;
8691
} else {
8792
this.defaultLocale = MessageFormat.defaultLocale;
93+
this.hasCustomPluralFuncs = false;
8894
}
8995
}
9096
this.fmt = {};
@@ -160,7 +166,7 @@ export default class MessageFormat {
160166
* mf.compile(msg)({ X: 0 }) // '0 answers'
161167
*/
162168
disablePluralKeyChecks() {
163-
this.noPluralKeyChecks = true;
169+
this.options.pluralKeyChecks = false;
164170
for (const lc in this.pluralFuncs) {
165171
const pf = this.pluralFuncs[lc];
166172
if (pf) {
@@ -194,7 +200,7 @@ export default class MessageFormat {
194200
* // 'first >> SECOND >> THIRD'
195201
*/
196202
setBiDiSupport(enable) {
197-
this.bidiSupport = !!enable || typeof enable == 'undefined';
203+
this.options.biDiSupport = !!enable || typeof enable == 'undefined';
198204
return this;
199205
}
200206

@@ -232,8 +238,8 @@ export default class MessageFormat {
232238
* messages.pastry({ X: 3, P: 'pie' }) // '# pies'
233239
*/
234240
setStrictNumberSign(enable) {
235-
this.strictNumberSign = !!enable || typeof enable == 'undefined';
236-
this.runtime.setStrictNumber(this.strictNumberSign);
241+
this.options.strictNumberSign = !!enable || typeof enable == 'undefined';
242+
this.runtime.setStrictNumber(this.options.strictNumberSign);
237243
return this;
238244
}
239245

@@ -332,15 +338,15 @@ export default class MessageFormat {
332338
let pf = {};
333339
if (Object.keys(this.pluralFuncs).length === 0) {
334340
if (locale) {
335-
const pfn0 = getPlural(locale, this.noPluralKeyChecks);
341+
const pfn0 = getPlural(locale, this.options);
336342
if (!pfn0) {
337343
const lcs = JSON.stringify(locale);
338344
throw new Error(`Locale ${lcs} not found!`);
339345
}
340346
pf[locale] = pfn0;
341347
} else {
342348
locale = this.defaultLocale;
343-
pf = getAllPlurals(this.noPluralKeyChecks);
349+
pf = getAllPlurals(this.options);
344350
}
345351
} else if (locale) {
346352
const pfn1 = this.pluralFuncs[locale];

packages/messageformat/src/plurals.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,38 @@ import plurals from 'make-plural/umd/plurals';
99
* {@link http://github.com/eemeli/make-plural.js make-plural}
1010
*/
1111

12-
function wrapPluralFunc(lc, pf, noPluralKeyChecks) {
12+
function wrapPluralFunc(lc, pf, pluralKeyChecks) {
1313
var fn = function() {
1414
return pf.apply(this, arguments);
1515
};
1616
fn.toString = () => pf.toString();
17-
if (noPluralKeyChecks) {
18-
fn.cardinal = [];
19-
fn.ordinal = [];
20-
} else {
17+
if (pluralKeyChecks) {
2118
const pc = pluralCategories[lc] || {};
2219
fn.cardinal = pc.cardinal;
2320
fn.ordinal = pc.ordinal;
21+
} else {
22+
fn.cardinal = [];
23+
fn.ordinal = [];
2424
}
2525
return fn;
2626
}
2727

28-
export function getPlural(locale, noPluralKeyChecks) {
28+
export function getPlural(locale, { pluralKeyChecks }) {
2929
for (let lc = String(locale); lc; lc = lc.replace(/[-_]?[^-_]*$/, '')) {
3030
const pf = plurals[lc];
31-
if (pf) return wrapPluralFunc(lc, pf, noPluralKeyChecks);
31+
if (pf) return wrapPluralFunc(lc, pf, pluralKeyChecks);
3232
}
3333
throw new Error(
3434
'Localisation function not found for locale ' + JSON.stringify(locale)
3535
);
3636
}
3737

38-
export function getAllPlurals(noPluralKeyChecks) {
38+
export function getAllPlurals({ pluralKeyChecks }) {
3939
const locales = {};
4040
const keys = Object.keys(plurals);
4141
for (let i = 0; i < keys.length; ++i) {
4242
const lc = keys[i];
43-
locales[lc] = wrapPluralFunc(lc, plurals[lc], noPluralKeyChecks);
43+
locales[lc] = wrapPluralFunc(lc, plurals[lc], pluralKeyChecks);
4444
}
4545
return locales;
4646
}

packages/messageformat/src/runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default class Runtime {
5050

5151
constructor(mf) {
5252
this.mf = mf;
53-
this.setStrictNumber(mf.strictNumberSign);
53+
this.setStrictNumber(mf.options.strictNumberSign);
5454
}
5555

5656
/** Utility function for `{N, plural|selectordinal, ...}`

packages/messageformat/src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const rtlRegExp = new RegExp('^' + rtlLanguages.join('|^'));
106106
*
107107
* @private
108108
*/
109-
export function bidiMarkText(text, locale) {
109+
export function biDiMarkText(text, locale) {
110110
const isLocaleRTL = rtlRegExp.test(locale);
111111
const mark = JSON.stringify(isLocaleRTL ? '\u200F' : '\u200E');
112112
return `${mark} + ${text} + ${mark}`;

0 commit comments

Comments
 (0)