diff --git a/src/utils/locale.ts b/src/utils/locale.ts index f20401c..00a41dd 100644 --- a/src/utils/locale.ts +++ b/src/utils/locale.ts @@ -14,23 +14,35 @@ export function initLocale() { } /** - * Get locale string - * @param localString - * @param branch branch name + * Get locale string, see https://firefox-source-docs.mozilla.org/l10n/fluent/tutorial.html#fluent-translation-list-ftl + * @param localString ftl key + * @param options.branch branch name + * @param options.args args * @example * ```ftl * # addon.ftl - * addon-name = Addon Template - * .label = Addon Template Label + * addon-static-example = This is default branch! + * .branch-example = This is a branch under addon-static-example! + * addon-dynamic-example = + { $count -> + [one] I have { $count } apple + *[other] I have { $count } apples + } * ``` * ```js - * getString("addon-name"); // Addon Template - * getString("addon-name", "label"); // Addon Template Label + * getString("addon-static-example"); // This is default branch! + * getString("addon-static-example", { branch: "branch-example" }); // This is a branch under addon-static-example! + * getString("addon-dynamic-example", { args: { count: 1 } }); // I have 1 apple + * getString("addon-dynamic-example", { args: { count: 2 } }); // I have 2 apples * ``` */ -export function getString(localString: string, branch = ""): string { +export function getString( + localString: string, + options: { branch?: string | undefined; args?: Record } = {} +): string { + const { branch, args } = options; const pattern = addon.data.locale?.current.formatMessagesSync([ - { id: localString }, + { id: localString, args }, ])[0]; if (!pattern) { return localString;