Skip to content

Commit f4438bd

Browse files
authored
Localization.formatValues should accept an array of object. (#198)
1 parent b44b802 commit f4438bd

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

fluent-dom/src/dom_localization.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,13 @@ export default class DOMLocalization extends Localization {
314314
* array.
315315
*
316316
* @param {Element} element
317-
* @returns {Array<string, Object>}
317+
* @returns {Object}
318318
* @private
319319
*/
320320
getKeysForElement(element) {
321-
return [
322-
element.getAttribute(L10NID_ATTR_NAME),
323-
JSON.parse(element.getAttribute(L10NARGS_ATTR_NAME) || null)
324-
];
321+
return {
322+
id: element.getAttribute(L10NID_ATTR_NAME),
323+
args: JSON.parse(element.getAttribute(L10NARGS_ATTR_NAME) || null)
324+
};
325325
}
326326
}

fluent-dom/src/localization.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default class Localization {
3131
* DOMLocalization. In case of errors, fetch the next context in the
3232
* fallback chain.
3333
*
34-
* @param {Array<Array>} keys - Translation keys to format.
34+
* @param {Array<Object>} keys - Translation keys to format.
3535
* @param {Function} method - Formatting function.
3636
* @returns {Promise<Array<string|Object>>}
3737
* @private
@@ -64,8 +64,8 @@ export default class Localization {
6464
* objects which are suitable for the translation of DOM elements.
6565
*
6666
* docL10n.formatMessages([
67-
* ['hello', { who: 'Mary' }],
68-
* ['welcome', undefined]
67+
* {id: 'hello', args: { who: 'Mary' }},
68+
* {id: 'welcome'}
6969
* ]).then(console.log);
7070
*
7171
* // [
@@ -75,7 +75,7 @@ export default class Localization {
7575
*
7676
* Returns a Promise resolving to an array of the translation strings.
7777
*
78-
* @param {Array<Array>} keys
78+
* @param {Array<Object>} keys
7979
* @returns {Promise<Array<{value: string, attributes: Object}>>}
8080
* @private
8181
*/
@@ -90,16 +90,16 @@ export default class Localization {
9090
* either be simple string identifiers or `[id, args]` arrays.
9191
*
9292
* docL10n.formatValues([
93-
* ['hello', { who: 'Mary' }],
94-
* ['hello', { who: 'John' }],
95-
* ['welcome']
93+
* {id: 'hello', args: { who: 'Mary' }},
94+
* {id: 'hello', args: { who: 'John' }},
95+
* {id: 'welcome'}
9696
* ]).then(console.log);
9797
*
9898
* // ['Hello, Mary!', 'Hello, John!', 'Welcome!']
9999
*
100100
* Returns a Promise resolving to an array of the translation strings.
101101
*
102-
* @param {Array<Array>} keys
102+
* @param {Array<Object>} keys
103103
* @returns {Promise<Array<string>>}
104104
*/
105105
formatValues(keys) {
@@ -129,7 +129,7 @@ export default class Localization {
129129
* @returns {Promise<string>}
130130
*/
131131
async formatValue(id, args) {
132-
const [val] = await this.formatValues([[id, args]]);
132+
const [val] = await this.formatValues([{id, args}]);
133133
return val;
134134
}
135135

@@ -249,17 +249,17 @@ function keysFromContext(method, ctx, keys, translations) {
249249
const messageErrors = [];
250250
const missingIds = new Set();
251251

252-
keys.forEach((key, i) => {
252+
keys.forEach(({id, args}, i) => {
253253
if (translations[i] !== undefined) {
254254
return;
255255
}
256256

257-
if (ctx.hasMessage(key[0])) {
257+
if (ctx.hasMessage(id)) {
258258
messageErrors.length = 0;
259-
translations[i] = method(ctx, messageErrors, key[0], key[1]);
259+
translations[i] = method(ctx, messageErrors, id, args);
260260
// XXX: Report resolver errors
261261
} else {
262-
missingIds.add(key[0]);
262+
missingIds.add(id);
263263
}
264264
});
265265

fluent-dom/test/localization_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ async function* mockGenerateMessages(resourceIds) {
1111
suite("formatMessages", function() {
1212
test("returns a translation", async function() {
1313
const loc = new Localization(["test.ftl"], mockGenerateMessages);
14-
const translations = await loc.formatMessages([["key1"]]);
14+
const translations = await loc.formatMessages([{id: "key1"}]);
1515

1616
assert.equal(translations[0].value, "Key 1");
1717
});
1818

1919
test("returns undefined for a missing translation", async function() {
2020
const loc = new Localization(["test.ftl"], mockGenerateMessages);
21-
const translations = await loc.formatMessages([["missing_key"]]);
21+
const translations = await loc.formatMessages([{id: "missing_key"}]);
2222

2323
// Make sure that the returned value here is `undefined`.
2424
// This allows bindings to handle missing translations.

0 commit comments

Comments
 (0)