Skip to content
Open
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
3 changes: 3 additions & 0 deletions fluent-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
"devDependencies": {
"@fluent/bundle": "^0.19.0"
},
"peerDependencies": {
"@fluent/bundle": ">=0.16.0"
},
"dependencies": {
"cached-iterable": "^0.3"
}
Expand Down
12 changes: 9 additions & 3 deletions fluent-dom/src/dom_localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const L10N_ELEMENT_QUERY = `[${L10NID_ATTR_NAME}]`;
*/
export default class DOMLocalization extends Localization {
/**
* @param {Array<String>} resourceIds - List of resource IDs
* @param {Function} generateBundles - Function that returns a
* generator over FluentBundles
* @param {string[]} resourceIds - List of resource IDs
* @param {import("./localization.js").GenerateBundles} generateBundles
* - Function that returns an iterable over FluentBundles
* @returns {DOMLocalization}
*/
constructor(resourceIds, generateBundles) {
Expand All @@ -42,6 +42,12 @@ export default class DOMLocalization extends Localization {
};
}

/**
* Regenerate the bundles and retranslate all connected roots.
*
* @param {boolean} [eager] - Whether to start fetching the first bundles
* right away
*/
onChange(eager = false) {
super.onChange(eager);
if (this.roots) {
Expand Down
4 changes: 4 additions & 0 deletions fluent-dom/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export { default as DOMLocalization } from "./dom_localization.js";
export { default as Localization } from "./localization.js";

/** @typedef {import("./localization.js").GenerateBundles} GenerateBundles */
/** @typedef {import("./localization.js").L10nKey} L10nKey */
/** @typedef {import("./localization.js").L10nMessage} L10nMessage */
54 changes: 47 additions & 7 deletions fluent-dom/src/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@

import { CachedAsyncIterable } from "cached-iterable";

/** @typedef {import("@fluent/bundle").FluentBundle} FluentBundle */
/** @typedef {import("@fluent/bundle").FluentVariable} FluentVariable */

/**
* A translation key: a message identifier and optional variables.
*
* @typedef {{ id: string, args?: Record<string, FluentVariable> }} L10nKey
*/

/**
* A formatted message: its value and the list of its attributes, either of
* which may be `null` if the message doesn't define them.
*
* @typedef {{ value: string | null, attributes: Array<{ name: string, value: string }> | null }} L10nMessage
*/

/**
* A function returning an iterable (sync or async) of `FluentBundle`s to use
* for the given resource IDs, ordered from the most to the least preferred.
*
* @typedef {(resourceIds: string[]) => Iterable<FluentBundle> | AsyncIterable<FluentBundle>} GenerateBundles
*/

/**
* The `Localization` class is a central high-level API for vanilla
* JavaScript use of Fluent.
Expand All @@ -11,9 +34,9 @@ import { CachedAsyncIterable } from "cached-iterable";
*/
export default class Localization {
/**
* @param {Array<String>} resourceIds - List of resource IDs
* @param {Function} generateBundles - Function that returns a
* generator over FluentBundles
* @param {string[]} resourceIds - List of resource IDs
* @param {GenerateBundles} generateBundles - Function that returns an
* iterable over FluentBundles
*
* @returns {Localization}
*/
Expand All @@ -23,12 +46,26 @@ export default class Localization {
this.onChange(true);
}

/**
* Add resource IDs and regenerate the bundles.
*
* @param {string[]} resourceIds - Resource IDs to add
* @param {boolean} [eager] - Whether to start fetching the first
* bundles right away
* @returns {number} The new number of resource IDs
*/
addResourceIds(resourceIds, eager = false) {
this.resourceIds.push(...resourceIds);
this.onChange(eager);
return this.resourceIds.length;
}

/**
* Remove resource IDs and regenerate the bundles.
*
* @param {string[]} resourceIds - Resource IDs to remove
* @returns {number} The new number of resource IDs
*/
removeResourceIds(resourceIds) {
this.resourceIds = this.resourceIds.filter(r => !resourceIds.includes(r));
this.onChange();
Expand Down Expand Up @@ -100,9 +137,9 @@ export default class Localization {
* // ]
* ```
*
* @param {Array<Object>} keys
* @returns {Promise<Array<{value: string, attributes: Object}>>}
* @private
* @param {L10nKey[]} keys
* @returns {Promise<Array<L10nMessage | undefined>>}
* @protected
*/
formatMessages(keys) {
return this.formatWithFallback(keys, messageFromBundle);
Expand All @@ -127,7 +164,7 @@ export default class Localization {
* // ['Hello, Mary!', 'Hello, John!', 'Welcome!']
* ```
*
* @param {Array<Object>} keys
* @param {L10nKey[]} keys
* @returns {Promise<Array<string>>}
*/
formatValues(keys) {
Expand Down Expand Up @@ -171,6 +208,9 @@ export default class Localization {
/**
* This method should be called when there's a reason to believe
* that language negotiation or available resources changed.
*
* @param {boolean} [eager] - Whether to start fetching the first bundles
* right away
*/
onChange(eager = false) {
this.bundles = CachedAsyncIterable.from(
Expand Down
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.