feat: generate type define for fluent messages

This commit is contained in:
Northword 2025-05-02 21:45:56 +08:00
parent c154923886
commit 05f0a9d5ec
4 changed files with 320 additions and 293 deletions

574
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -39,7 +39,7 @@
"prettier": "^3.5.3", "prettier": "^3.5.3",
"typescript": "^5.8.3", "typescript": "^5.8.3",
"typescript-eslint": "^8.31.1", "typescript-eslint": "^8.31.1",
"zotero-plugin-scaffold": "^0.5.0", "zotero-plugin-scaffold": "^0.6.0",
"zotero-types": "^4.0.0-beta.10" "zotero-types": "^4.0.0-beta.10"
}, },
"prettier": { "prettier": {

View File

@ -1,4 +1,5 @@
import { config } from "../../package.json"; import { config } from "../../package.json";
import { FluentMessageId } from "../../typings/i10n";
export { initLocale, getString, getLocaleID }; export { initLocale, getString, getLocaleID };
@ -39,10 +40,10 @@ function initLocale() {
* getString("addon-dynamic-example", { args: { count: 2 } }); // I have 2 apples * getString("addon-dynamic-example", { args: { count: 2 } }); // I have 2 apples
* ``` * ```
*/ */
function getString(localString: string): string; function getString(localString: FluentMessageId): string;
function getString(localString: string, branch: string): string; function getString(localString: FluentMessageId, branch: string): string;
function getString( function getString(
localeString: string, localeString: FluentMessageId,
options: { branch?: string | undefined; args?: Record<string, unknown> }, options: { branch?: string | undefined; args?: Record<string, unknown> },
): string; ): string;
function getString(...inputs: any[]) { function getString(...inputs: any[]) {
@ -60,7 +61,7 @@ function getString(...inputs: any[]) {
} }
function _getString( function _getString(
localeString: string, localeString: FluentMessageId,
options: { branch?: string | undefined; args?: Record<string, unknown> } = {}, options: { branch?: string | undefined; args?: Record<string, unknown> } = {},
): string { ): string {
const localStringWithPrefix = `${config.addonRef}-${localeString}`; const localStringWithPrefix = `${config.addonRef}-${localeString}`;
@ -83,6 +84,6 @@ function _getString(
} }
} }
function getLocaleID(id: string) { function getLocaleID(id: FluentMessageId) {
return `${config.addonRef}-${id}`; return `${config.addonRef}-${id}`;
} }

26
typings/i10n.d.ts vendored Normal file
View File

@ -0,0 +1,26 @@
// Generated by zotero-plugin-scaffold
/* prettier-ignore */
/* eslint-disable */
// @ts-nocheck
export type FluentMessageId =
| 'item-info-row-example-label'
| 'item-section-example1-head-text'
| 'item-section-example1-sidenav-tooltip'
| 'item-section-example2-button-tooltip'
| 'item-section-example2-head-text'
| 'item-section-example2-sidenav-tooltip'
| 'menuitem-filemenulabel'
| 'menuitem-label'
| 'menuitem-submenulabel'
| 'menupopup-label'
| 'pref-enable'
| 'pref-help'
| 'pref-input'
| 'pref-title'
| 'prefs-table-detail'
| 'prefs-table-title'
| 'prefs-title'
| 'startup-begin'
| 'startup-finish'
| 'tabpanel-lib-tab-label'
| 'tabpanel-reader-tab-label';