Skip to content

Commit b468110

Browse files
committed
Backport MessageContext parser from [email protected]
In order to make it easier to upgrade to the new syntax, we'll release fluent 0.4.3 which will keep the API compatibility with 0.4.2 and will be able to read Fluent Syntax 0.5 files (just like fluent 0.6.0). These changes make fluent 0.4.x able to read both Syntax 0.4 and Syntax 0.5 files: - Supported Syntax 0.4 features: - `//` comments. - Sections (`[[ Foo ]]`). - Message definitions may omit the `=` after the identifier if they don't have a value. - Supported Syntax 0.5 features: - `#`, `##` and `###` comments. - Identifiers starting with a dash (`-`) are parsed as terms. The only feature of the old Syntax 0.4 which isn't supported anymore are Tags. In practice we didn't see any of the current consumers of `fluent` 0.4.2 take advantage of tags. In Syntax 0.5, [tags were replaced by attribute defined on terms][terms]. [terms]: https://github.com/projectfluent/fluent/blob/master/spec/CHANGELOG.md#050-january-31-2018
1 parent 124c2aa commit b468110

File tree

10 files changed

+295
-129
lines changed

10 files changed

+295
-129
lines changed

compat_config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default {
1313
],
1414
'plugins': [
1515
'external-helpers',
16+
'babel-plugin-optimize-starts-with',
1617
['babel-plugin-transform-builtin-extend', {
1718
globals: ['Error']
1819
}]

fluent/src/context.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ export class MessageContext {
5151
constructor(locales, { functions = {}, useIsolating = true } = {}) {
5252
this.locales = Array.isArray(locales) ? locales : [locales];
5353

54+
this._terms = new Map();
5455
this._messages = new Map();
5556
this._functions = functions;
5657
this._useIsolating = useIsolating;
5758
this._intls = new WeakMap();
5859
}
5960

6061
/*
61-
* Return an iterator over `[id, message]` pairs.
62+
* Return an iterator over public `[id, message]` pairs.
6263
*
6364
* @returns {Iterator}
6465
*/
@@ -110,7 +111,13 @@ export class MessageContext {
110111
addMessages(source) {
111112
const [entries, errors] = parse(source);
112113
for (const id in entries) {
113-
this._messages.set(id, entries[id]);
114+
if (id.startsWith('-')) {
115+
// Identifiers starting with a dash (-) define terms. Terms are private
116+
// and cannot be retrieved from MessageContext.
117+
this._terms.set(id, entries[id]);
118+
} else {
119+
this._messages.set(id, entries[id]);
120+
}
114121
}
115122

116123
return errors;

0 commit comments

Comments
 (0)