Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiate FluentResource via a constructor #401

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions fluent/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,24 @@ export default class FluentBundle {
* @returns {Array<Error>}
*/
addMessages(source, options) {
const res = FluentResource.fromString(source);
const res = new FluentResource(source);
return this.addResource(res, options);
}

/**
* Add a translation resource to the bundle.
*
* The translation resource must be an instance of FluentResource,
* e.g. parsed by `FluentResource.fromString`.
* The translation resource must be an instance of `FluentResource`.
*
* let res = FluentResource.fromString("foo = Foo");
* let res = new FluentResource("foo = Foo");
* bundle.addResource(res);
* bundle.getMessage('foo');
*
* // Returns a raw representation of the 'foo' message.
*
* let res = FluentResource.fromString("bar = Bar");
* let res = new FluentResource("bar = Bar");
* bundle.addResource(res);
* res = FluentResource.fromString("bar = Newbar");
* res = new FluentResource("bar = Newbar");
* bundle.addResource(res, { allowOverrides: true });
* bundle.getMessage('bar');
*
Expand All @@ -163,8 +162,8 @@ export default class FluentBundle {
} = {}) {
const errors = [];

for (let i = 0; i < res.length; i++) {
let entry = res[i];
for (let i = 0; i < res.body.length; i++) {
let entry = res.body[i];
if (entry.id.startsWith("-")) {
// Identifiers starting with a dash (-) define terms. Terms are private
// and cannot be retrieved from FluentBundle.
Expand Down
18 changes: 10 additions & 8 deletions fluent/src/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ const TOKEN_BLANK = /\s+/y;
const MAX_PLACEABLES = 100;

/**
* Fluent Resource is a structure storing a map of parsed localization entries.
* Fluent Resource is a structure storing parsed localization entries.
*/
export default class FluentResource extends Array {
/**
* Create a new FluentResource from Fluent code.
*/
static fromString(source) {
export default class FluentResource {
constructor(source) {
this.body = this.parse(source);
}

parse(source) {
RE_MESSAGE_START.lastIndex = 0;

let resource = new this();
let resource = [];
let cursor = 0;

// Iterate over the beginnings of messages and terms to efficiently skip
Expand All @@ -88,7 +89,8 @@ export default class FluentResource extends Array {

return resource;

// The parser implementation is inlined below for performance reasons.
// The parser implementation is inlined below for performance reasons,
// as well as for convenience of accessing `source` and `cursor`.

// The parser focuses on minimizing the number of false negatives at the
// expense of increasing the risk of false positives. In other words, it
Expand Down
8 changes: 4 additions & 4 deletions fluent/test/context_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ suite('Bundle', function() {
suite('addResource', function(){
suiteSetup(function() {
bundle = new FluentBundle('en-US', { useIsolating: false });
let resource = FluentResource.fromString(ftl`
let resource = new FluentResource(ftl`
foo = Foo
-bar = Bar
`);
Expand All @@ -108,20 +108,20 @@ suite('Bundle', function() {
suite('allowOverrides', function(){
suiteSetup(function() {
bundle = new FluentBundle('en-US', { useIsolating: false });
let resource1 = FluentResource.fromString('key = Foo');
let resource1 = new FluentResource('key = Foo');
bundle.addResource(resource1);
});

test('addResource allowOverrides is false', function() {
let resource2 = FluentResource.fromString('key = Bar');
let resource2 = new FluentResource('key = Bar');
let errors = bundle.addResource(resource2);
assert.strictEqual(errors.length, 1);
let msg = bundle.getMessage('key');
assert.strictEqual(bundle.formatPattern(msg.value), 'Foo');
});

test('addResource allowOverrides is true', function() {
let resource2 = FluentResource.fromString('key = Bar');
let resource2 = new FluentResource('key = Bar');
let errors = bundle.addResource(resource2, { allowOverrides: true });
assert.strictEqual(errors.length, 0);
let msg = bundle.getMessage('key');
Expand Down
36 changes: 19 additions & 17 deletions fluent/test/fixtures_reference/any_char.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
[
{
"id": "control0",
"value": "abc\u0007def",
"attributes": {}
},
{
"id": "delete",
"value": "abcdef",
"attributes": {}
},
{
"id": "control1",
"value": "abc‚def",
"attributes": {}
}
]
{
"body": [
{
"id": "control0",
"value": "abc\u0007def",
"attributes": {}
},
{
"id": "delete",
"value": "abcdef",
"attributes": {}
},
{
"id": "control1",
"value": "abc‚def",
"attributes": {}
}
]
}
114 changes: 58 additions & 56 deletions fluent/test/fixtures_reference/astral.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
[
{
"id": "face-with-tears-of-joy",
"value": "😂",
"attributes": {}
},
{
"id": "tetragram-for-centre",
"value": "𝌆",
"attributes": {}
},
{
"id": "surrogates-in-text",
"value": "\\uD83D\\uDE02",
"attributes": {}
},
{
"id": "surrogates-in-string",
"value": [
{
"type": "str",
"value": "��"
}
],
"attributes": {}
},
{
"id": "surrogates-in-adjacent-strings",
"value": [
{
"type": "str",
"value": "�"
},
{
"type": "str",
"value": "�"
}
],
"attributes": {}
},
{
"id": "emoji-in-text",
"value": "A face 😂 with tears of joy.",
"attributes": {}
},
{
"id": "emoji-in-string",
"value": [
{
"type": "str",
"value": "A face 😂 with tears of joy."
}
],
"attributes": {}
}
]
{
"body": [
{
"id": "face-with-tears-of-joy",
"value": "😂",
"attributes": {}
},
{
"id": "tetragram-for-centre",
"value": "𝌆",
"attributes": {}
},
{
"id": "surrogates-in-text",
"value": "\\uD83D\\uDE02",
"attributes": {}
},
{
"id": "surrogates-in-string",
"value": [
{
"type": "str",
"value": "��"
}
],
"attributes": {}
},
{
"id": "surrogates-in-adjacent-strings",
"value": [
{
"type": "str",
"value": "�"
},
{
"type": "str",
"value": "�"
}
],
"attributes": {}
},
{
"id": "emoji-in-text",
"value": "A face 😂 with tears of joy.",
"attributes": {}
},
{
"id": "emoji-in-string",
"value": [
{
"type": "str",
"value": "A face 😂 with tears of joy."
}
],
"attributes": {}
}
]
}
Loading