fix: getString from ftl

remove: getStringAsync
This commit is contained in:
windingwind 2023-06-16 10:00:19 +08:00
parent 25081bf450
commit a4741696e6
5 changed files with 62 additions and 57 deletions

View File

@ -10,7 +10,8 @@ class Addon {
// ztoolkit: MyToolkit;
ztoolkit: ZoteroToolkit;
locale?: {
stringBundle: any;
current: any;
default: any;
};
prefs?: {
window: Window;

View File

@ -6,7 +6,7 @@ import {
UIExampleFactory,
} from "./modules/examples";
import { config } from "../package.json";
import { getStringAsync, initLocale } from "./utils/locale";
import { getString, initLocale } from "./utils/locale";
import { registerPrefsScripts } from "./modules/preferenceScript";
async function onStartup() {
@ -26,7 +26,7 @@ async function onStartup() {
closeTime: -1,
})
.createLine({
text: await getStringAsync("startup-begin"),
text: getString("startup-begin"),
type: "default",
progress: 0,
})
@ -41,7 +41,7 @@ async function onStartup() {
await Zotero.Promise.delay(1000);
popupWin.changeLine({
progress: 30,
text: `[30%] ${await getStringAsync("startup-begin")}`,
text: `[30%] ${getString("startup-begin")}`,
});
UIExampleFactory.registerStyleSheet();
@ -74,7 +74,7 @@ async function onStartup() {
popupWin.changeLine({
progress: 100,
text: `[100%] ${await getStringAsync("startup-finish")}`,
text: `[100%] ${getString("startup-finish")}`,
});
popupWin.startCloseTimer(5000);

View File

@ -1,5 +1,5 @@
import { config } from "../../package.json";
import { getStringAsync } from "../utils/locale";
import { getString } from "../utils/locale";
function example(
target: any,
@ -71,11 +71,11 @@ export class BasicExampleFactory {
}
@example
static async registerPrefs() {
static registerPrefs() {
const prefOptions = {
pluginID: config.addonID,
src: rootURI + "chrome/content/preferences.xhtml",
label: await getStringAsync("prefs-title"),
label: getString("prefs-title"),
image: `chrome://${config.addonRef}/content/icons/favicon.png`,
defaultXUL: true,
};
@ -197,29 +197,29 @@ export class UIExampleFactory {
}
@example
static async registerRightClickMenuItem() {
static registerRightClickMenuItem() {
const menuIcon = `chrome://${config.addonRef}/content/icons/favicon@0.5x.png`;
// item menuitem with icon
ztoolkit.Menu.register("item", {
tag: "menuitem",
id: "zotero-itemmenu-addontemplate-test",
label: await getStringAsync("menuitem-label"),
label: getString("menuitem-label"),
commandListener: (ev) => addon.hooks.onDialogEvents("dialogExample"),
icon: menuIcon,
});
}
@example
static async registerRightClickMenuPopup() {
static registerRightClickMenuPopup() {
ztoolkit.Menu.register(
"item",
{
tag: "menu",
label: await getStringAsync("menupopup-label"),
label: getString("menupopup-label"),
children: [
{
tag: "menuitem",
label: await getStringAsync("menuitem-submenulabel"),
label: getString("menuitem-submenulabel"),
oncommand: "alert('Hello World! Sub Menuitem.')",
},
],
@ -232,14 +232,14 @@ export class UIExampleFactory {
}
@example
static async registerWindowMenuWithSeparator() {
static registerWindowMenuWithSeparator() {
ztoolkit.Menu.register("menuFile", {
tag: "menuseparator",
});
// menu->File menuitem
ztoolkit.Menu.register("menuFile", {
tag: "menuitem",
label: await getStringAsync("menuitem-filemenulabel"),
label: getString("menuitem-filemenulabel"),
oncommand: "alert('Hello World! File Menuitem.')",
});
}
@ -345,9 +345,9 @@ export class UIExampleFactory {
}
@example
static async registerLibraryTabPanel() {
static registerLibraryTabPanel() {
const tabId = ztoolkit.LibraryTabPanel.register(
await getStringAsync("tabpanel-lib-tab-label"),
getString("tabpanel-lib-tab-label"),
(panel: XUL.Element, win: Window) => {
const elem = ztoolkit.UI.createElement(win.document, "vbox", {
children: [
@ -391,7 +391,7 @@ export class UIExampleFactory {
@example
static async registerReaderTabPanel() {
const tabId = await ztoolkit.ReaderTabPanel.register(
await getStringAsync("tabpanel-reader-tab-label"),
getString("tabpanel-reader-tab-label"),
(
panel: XUL.TabPanel | undefined,
deck: XUL.Deck,

View File

@ -1,5 +1,5 @@
import { config } from "../../package.json";
import { getStringAsync } from "../utils/locale";
import { getString } from "../utils/locale";
export async function registerPrefsScripts(_window: Window) {
// This function is called when the prefs window is opened
@ -10,13 +10,13 @@ export async function registerPrefsScripts(_window: Window) {
columns: [
{
dataKey: "title",
label: await getStringAsync("prefs-table-title"),
label: getString("prefs-table-title"),
fixedWidth: true,
width: 100,
},
{
dataKey: "detail",
label: await getStringAsync("prefs-table-detail"),
label: getString("prefs-table-detail"),
},
],
rows: [

View File

@ -1,52 +1,56 @@
import { config } from "../../package.json";
import { waitUntil } from "./wait";
/**
* Initialize locale data
*/
export function initLocale() {
ztoolkit.UI.appendElement(
{
tag: "link",
namespace: "html",
properties: {
rel: "localization",
href: `${config.addonRef}-addon.ftl`,
},
},
document.querySelector("linkset")!
const l10n = ztoolkit.getGlobal("L10nRegistry").getInstance();
const bundleGenerator = l10n.generateBundlesSync(
[Zotero.locale, "en-US"],
[`${config.addonRef}-addon.ftl`]
);
const currentBundle = bundleGenerator.next().value;
const defaultBundle = bundleGenerator.next().value;
addon.data.locale = {
current: currentBundle,
default: defaultBundle,
};
}
/**
* Get locale string
* @param localString
* @deprecated
* @param branch branch name
* @example
* ```ftl
* # addon.ftl
* addon-name = Addon Template
* .label = Addon Template Label
* ```
* ```js
* getString("addon-name"); // Addon Template
* getString("addon-name", "label"); // Addon Template Label
* ```
*/
export function getString(localString: string): string {
let result = "";
let flag = false;
getStringAsync(localString)
.then((value) => {
result = value;
flag = true;
})
.catch((e) => {
ztoolkit.log(e);
flag = true;
});
const t = new Date().getTime();
while (!flag && t < new Date().getTime() - 3000) {
// wait until the string is loaded
}
return result;
export function getString(localString: string, branch = ""): string {
return (
getStringFromBundle(addon.data.locale?.current, localString, branch) ||
getStringFromBundle(addon.data.locale?.default, localString, branch) ||
localString
);
}
/**
* Get locale string async
* @param localString
*/
export async function getStringAsync(localString: string): Promise<string> {
// @ts-ignore
return (await document.l10n.formatValue(localString)) || localString;
function getStringFromBundle(bundle: any, localString: string, branch = "") {
if (!bundle) {
return "";
}
const patterns = bundle.getMessage(localString);
if (!patterns) {
return "";
}
if (branch) {
return bundle.formatPattern(patterns.attributes[branch]);
} else {
return bundle.formatPattern(patterns.value);
}
}