Merge pull request #16 from l0o0/bootstrap

Add AddonLocale class for different language
This commit is contained in:
windingwind 2022-12-29 20:20:11 +08:00 committed by GitHub
commit 7e92f919f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 49 additions and 8 deletions

View File

@ -148,7 +148,7 @@ This section shows the directory structure of a template.
- All `.js/.ts` code files are in `./src`; - All `.js/.ts` code files are in `./src`;
- Addon config files: `./addon/chrome.manifest`, `./addon/install.rdf`; - Addon config files: `./addon/chrome.manifest`, `./addon/install.rdf`;
- UI files: `./addon/chrome/content/*.xul`. The `overlay.xul` also defines the main entrance; - UI files: `./addon/chrome/content/*.xul`. The `overlay.xul` also defines the main entrance;
- Locale files: `./addon/chrome/locale/*.dtd`; - Locale files: `./addon/chrome/locale/[*.dtd, *.properties]`;
- Resource files: `./addon/chrome/skin/default/__addonRef__/*.dtd`; - Resource files: `./addon/chrome/skin/default/__addonRef__/*.dtd`;
- Preferences file: `./addon/chrome/defaults/preferences/defaults.js`; - Preferences file: `./addon/chrome/defaults/preferences/defaults.js`;
> Don't break the lines in the `defaults.js` > Don't break the lines in the `defaults.js`
@ -182,9 +182,11 @@ This section shows the directory structure of a template.
│ └─locale # locale │ └─locale # locale
│ ├─en-US │ ├─en-US
│ │ overlay.dtd │ │ overlay.dtd
│ │ addontemplate.properties
│ │ │ │
│ └─zh-CN │ └─zh-CN
│ overlay.dtd │ | overlay.dtd
│ │ addontemplate.properties
├─builds # build dir ├─builds # build dir
│ └─.xpi │ └─.xpi
@ -195,6 +197,7 @@ This section shows the directory structure of a template.
│ addon.ts # base class │ addon.ts # base class
│ events.ts # events class │ events.ts # events class
│ views.ts # UI class │ views.ts # UI class
│ locale.ts # Locale class for properties files
└─ prefs.ts # preferences class └─ prefs.ts # preferences class
``` ```

View File

@ -3,7 +3,7 @@
onload="Zotero.AddonTemplate.prefs.initPreferences(window)" onload="Zotero.AddonTemplate.prefs.initPreferences(window)"
> >
<groupbox> <groupbox>
<label><html:h2>Addon Template Example</html:h2></label> <label><html:h2>&zotero.__addonRef__.pref.title;</html:h2></label>
<checkbox <checkbox
id="zotero-prefpane-__addonRef__-enable" id="zotero-prefpane-__addonRef__-enable"
label="&zotero.__addonRef__.pref.enable.label;" label="&zotero.__addonRef__.pref.enable.label;"
@ -21,3 +21,6 @@
</hbox> </hbox>
</groupbox> </groupbox>
</vbox> </vbox>
<vbox>
<label value="&zotero.__addonRef__.help.version.label; &zotero.__addonRef__.help.releasetime.label;"></label>
</vbox>

View File

@ -0,0 +1,5 @@
menuitem.label=Addon Template: Menuitem
menupopup.label=Addon Template: Menupopup
menuitem.submenulabel=Addon Template
menuitem.filemenulabel=Addon Template: File Menuitem
prefs.title=Template

View File

@ -1,3 +1,4 @@
<!ENTITY zotero.__addonRef__.pref.title "Addon Template Example">
<!ENTITY zotero.__addonRef__.itemmenu.test.label "addon template"> <!ENTITY zotero.__addonRef__.itemmenu.test.label "addon template">
<!ENTITY zotero.__addonRef__.pref.enable.label "Enable"> <!ENTITY zotero.__addonRef__.pref.enable.label "Enable">
<!ENTITY zotero.__addonRef__.pref.input.label "Input"> <!ENTITY zotero.__addonRef__.pref.input.label "Input">

View File

@ -0,0 +1,5 @@
menuitem.label=Addon Template: 菜单
menupopup.label=Addon Template: 弹出菜单
menuitem.submenulabel=Addon Template
menuitem.filemenulabel=Addon Template: 文件菜单
prefs.title=插件模板

View File

@ -1,3 +1,4 @@
<!ENTITY zotero.__addonRef__.pref.title "插件模板设置示例">
<!ENTITY zotero.__addonRef__.itemmenu.test.label "插件模板"> <!ENTITY zotero.__addonRef__.itemmenu.test.label "插件模板">
<!ENTITY zotero.__addonRef__.pref.enable.label "开启"> <!ENTITY zotero.__addonRef__.pref.enable.label "开启">
<!ENTITY zotero.__addonRef__.pref.input.label "输入"> <!ENTITY zotero.__addonRef__.pref.input.label "输入">

View File

@ -1,14 +1,17 @@
import AddonEvents from "./events"; import AddonEvents from "./events";
import AddonPrefs from "./prefs"; import AddonPrefs from "./prefs";
import AddonViews from "./views"; import AddonViews from "./views";
import AddonLocale from "./locale";
import ZoteroToolkit from "zotero-plugin-toolkit"; import ZoteroToolkit from "zotero-plugin-toolkit";
import Locale from "./locale";
class Addon { class Addon {
public Zotero!: _ZoteroConstructable; public Zotero!: _ZoteroConstructable;
public events: AddonEvents; public events: AddonEvents;
public views: AddonViews; public views: AddonViews;
public prefs: AddonPrefs; public prefs: AddonPrefs;
public locale: AddonLocale;
public toolkit: ZoteroToolkit; public toolkit: ZoteroToolkit;
// root path to access the resources // root path to access the resources
public rootURI!: string; public rootURI!: string;
@ -17,6 +20,7 @@ class Addon {
this.events = new AddonEvents(this); this.events = new AddonEvents(this);
this.views = new AddonViews(this); this.views = new AddonViews(this);
this.prefs = new AddonPrefs(this); this.prefs = new AddonPrefs(this);
this.locale = new AddonLocale(this);
this.toolkit = new ZoteroToolkit(); this.toolkit = new ZoteroToolkit();
} }
} }

View File

@ -61,7 +61,7 @@ class AddonEvents extends AddonModule {
const prefOptions = { const prefOptions = {
pluginID: config.addonID, pluginID: config.addonID,
src: this._Addon.rootURI + "chrome/content/preferences.xhtml", src: this._Addon.rootURI + "chrome/content/preferences.xhtml",
label: "Template", label: this._Addon.locale.getString("prefs.title"),
image: `chrome://${config.addonRef}/content/icons/favicon.png`, image: `chrome://${config.addonRef}/content/icons/favicon.png`,
extraDTD: [`chrome://${config.addonRef}/locale/overlay.dtd`], extraDTD: [`chrome://${config.addonRef}/locale/overlay.dtd`],
defaultXUL: true, defaultXUL: true,

19
src/locale.ts Normal file
View File

@ -0,0 +1,19 @@
import Addon from "./addon";
import AddonModule from "./module";
class AddonLocale extends AddonModule {
private stringBundle: any;
constructor(parent: Addon) {
super(parent);
this.stringBundle = Components.classes['@mozilla.org/intl/stringbundle;1']
.getService(Components.interfaces.nsIStringBundleService)
.createBundle('chrome://addontemplate/locale/addontemplate.properties');
}
public getString(localString: string): string {
return this.stringBundle.GetStringFromName(localString);
}
}
export default AddonLocale;

View File

@ -24,7 +24,7 @@ class AddonViews extends AddonModule {
this._Addon.toolkit.UI.insertMenuItem("item", { this._Addon.toolkit.UI.insertMenuItem("item", {
tag: "menuitem", tag: "menuitem",
id: "zotero-itemmenu-addontemplate-test", id: "zotero-itemmenu-addontemplate-test",
label: "Addon Template: Menuitem", label: this._Addon.locale.getString("menuitem.label"),
oncommand: "alert('Hello World! Default Menuitem.')", oncommand: "alert('Hello World! Default Menuitem.')",
icon: menuIcon, icon: menuIcon,
}); });
@ -33,11 +33,11 @@ class AddonViews extends AddonModule {
"item", "item",
{ {
tag: "menu", tag: "menu",
label: "Addon Template: Menupopup", label: this._Addon.locale.getString("menupopup.label"),
subElementOptions: [ subElementOptions: [
{ {
tag: "menuitem", tag: "menuitem",
label: "Addon Template", label: this._Addon.locale.getString("menuitem.submenulabel"),
oncommand: "alert('Hello World! Sub Menuitem.')", oncommand: "alert('Hello World! Sub Menuitem.')",
}, },
], ],
@ -53,7 +53,7 @@ class AddonViews extends AddonModule {
// menu->File menuitem // menu->File menuitem
this._Addon.toolkit.UI.insertMenuItem("menuFile", { this._Addon.toolkit.UI.insertMenuItem("menuFile", {
tag: "menuitem", tag: "menuitem",
label: "Addon Template: File Menuitem", label: this._Addon.locale.getString("menuitem.filemenulabel"),
oncommand: "alert('Hello World! File Menuitem.')", oncommand: "alert('Hello World! File Menuitem.')",
}); });