add: getString with args
This commit is contained in:
parent
a254ff8bc3
commit
4d3bc65b94
@ -14,23 +14,35 @@ export function initLocale() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get locale string
|
* Get locale string, see https://firefox-source-docs.mozilla.org/l10n/fluent/tutorial.html#fluent-translation-list-ftl
|
||||||
* @param localString
|
* @param localString ftl key
|
||||||
* @param branch branch name
|
* @param options.branch branch name
|
||||||
|
* @param options.args args
|
||||||
* @example
|
* @example
|
||||||
* ```ftl
|
* ```ftl
|
||||||
* # addon.ftl
|
* # addon.ftl
|
||||||
* addon-name = Addon Template
|
* addon-static-example = This is default branch!
|
||||||
* .label = Addon Template Label
|
* .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
|
* ```js
|
||||||
* getString("addon-name"); // Addon Template
|
* getString("addon-static-example"); // This is default branch!
|
||||||
* getString("addon-name", "label"); // Addon Template Label
|
* 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, unknown> } = {}
|
||||||
|
): string {
|
||||||
|
const { branch, args } = options;
|
||||||
const pattern = addon.data.locale?.current.formatMessagesSync([
|
const pattern = addon.data.locale?.current.formatMessagesSync([
|
||||||
{ id: localString },
|
{ id: localString, args },
|
||||||
])[0];
|
])[0];
|
||||||
if (!pattern) {
|
if (!pattern) {
|
||||||
return localString;
|
return localString;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user