Skip to content

Commit cfa14ab

Browse files
committed
Align fluent-dom with Gecko Localization.jsm updates
1 parent 77c1406 commit cfa14ab

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

fluent-dom/src/dom_localization.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ export default class DOMLocalization extends Localization {
4242
};
4343
}
4444

45-
onChange() {
46-
super.onChange();
47-
this.translateRoots();
45+
onChange(eager = false) {
46+
super.onChange(eager);
47+
if (this.roots) {
48+
this.translateRoots();
49+
}
4850
}
4951

5052
/**

fluent-dom/src/localization.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ export default class Localization {
2020
constructor(resourceIds = [], generateBundles) {
2121
this.resourceIds = resourceIds;
2222
this.generateBundles = generateBundles;
23-
this.bundles = CachedAsyncIterable.from(
24-
this.generateBundles(this.resourceIds));
23+
this.onChange(true);
2524
}
2625

27-
addResourceIds(resourceIds) {
26+
addResourceIds(resourceIds, eager = false) {
2827
this.resourceIds.push(...resourceIds);
29-
this.onChange();
28+
this.onChange(eager);
3029
return this.resourceIds.length;
3130
}
3231

@@ -50,8 +49,10 @@ export default class Localization {
5049
*/
5150
async formatWithFallback(keys, method) {
5251
const translations = [];
52+
let hasAtLeastOneBundle = false;
5353

5454
for await (const bundle of this.bundles) {
55+
hasAtLeastOneBundle = true;
5556
const missingIds = keysFromBundle(method, bundle, keys, translations);
5657

5758
if (missingIds.size === 0) {
@@ -61,10 +62,17 @@ export default class Localization {
6162
if (typeof console !== "undefined") {
6263
const locale = bundle.locales[0];
6364
const ids = Array.from(missingIds).join(", ");
64-
console.warn(`Missing translations in ${locale}: ${ids}`);
65+
console.warn(`[fluent] Missing translations in ${locale}: ${ids}`);
6566
}
6667
}
6768

69+
if (!hasAtLeastOneBundle && typeof console !== "undefined") {
70+
// eslint-disable-next-line max-len
71+
console.warn(`[fluent] Request for keys failed because no resource bundles got generated.
72+
keys: ${JSON.stringify(keys)}.
73+
resourceIds: ${JSON.stringify(this.resourceIds)}.`);
74+
}
75+
6876
return translations;
6977
}
7078

@@ -156,10 +164,12 @@ export default class Localization {
156164
* This method should be called when there's a reason to believe
157165
* that language negotiation or available resources changed.
158166
*/
159-
onChange() {
167+
onChange(eager = false) {
160168
this.bundles = CachedAsyncIterable.from(
161169
this.generateBundles(this.resourceIds));
162-
this.bundles.touchNext(2);
170+
if (eager) {
171+
this.bundles.touchNext(2);
172+
}
163173
}
164174
}
165175

@@ -269,7 +279,12 @@ function keysFromBundle(method, bundle, keys, translations) {
269279
if (message) {
270280
messageErrors.length = 0;
271281
translations[i] = method(bundle, messageErrors, message, args);
272-
// XXX: Report resolver errors
282+
if (messageErrors.length > 0 && typeof console !== "undefined") {
283+
const locale = bundle.locales[0];
284+
const errors = messageErrors.join(", ");
285+
// eslint-disable-next-line max-len
286+
console.warn(`[fluent][resolver] errors in ${locale}/${id}: ${errors}.`);
287+
}
273288
} else {
274289
missingIds.add(id);
275290
}

0 commit comments

Comments
 (0)