Skip to content

Commit

Permalink
Use locales provided by us, irrespective of whether they are supporte…
Browse files Browse the repository at this point in the history
…d by Zotero or not
  • Loading branch information
diegodlh committed Aug 21, 2024
1 parent 3b5d688 commit 15de93b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
"zotero-plugin-toolkit": "^2.3.37"
},
"devDependencies": {
"@types/language-tags": "^1.0.4",
"@types/node": "^20.10.4",
"@types/react-dom": "^18.3.0",
"eslint": "^8.55.0",
"language-tags": "^1.0.9",
"prettier": "^3.1.1",
"typescript": "^5.3.3",
"typescript-eslint": "^7.14.1",
Expand Down
70 changes: 70 additions & 0 deletions src/utils/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,76 @@ function initLocale() {
addon.data.locale = {
current: l10n,
};

// Zotero 7 automatically registers Fluent sources for enabled plugins
// found in [plugin-root]/locale/{locale}/
// See https://www.zotero.org/support/dev/zotero_7_for_developers#registering_fluent_files
// This is handled by function Zotero.Plugins.registerLocales() in
// chrome://zotero/content/xpcom/plugins.js
// However, because of this function's implementation, only exact matches of
// locales supported by Zotero (as listed by
// Services.locale.availableLocales) are registered. This results in, for
// example, our locale "es" not being registered because Zotero supports
// "es-ES" instead.
// The code below removes the source registered by the registerLocales()
// function and replaces it with one that includes all locales supported by
// us. This results in:
// * Our locale "es" will be used even if requested locale is "es-ES",
// * Our locale "pt-BR" will be used even if reqeusted locel is "pt-PT",
// * Locales not supported by Zotero but supported by us (such as kaa)
// will be used if requested, even if the rest of the interface falls
// back to English.
// TODO: Consider moving this code to bootstrap.js.
// TODO: Consider requesting Zotero developers to change the
// registerLocales() function.

ztoolkit.getGlobal("L10nRegistry").getInstance().removeSources([config.addonID]);

let source = new (ztoolkit.getGlobal("L10nFileSource"))(
config.addonID,
'app',
[
// List of locales supported by us
// TODO: consider picking this up from manifest.json, maybe using
// "l10n_resources" property, as used in Zotero's
// resource://gre/modules/Extension.sys.mjs
"ar",
"ca",
"de",
"en-US",
"es",
"fa",
"fi",
"fr",
"gl",
"he",
"hi",
"id",
"io",
"it",
"kaa",
"ko",
"lt",
"mk",
"nl",
"pms",
"pt-br",
// qqq,
"ro",
"ru",
"sk",
"sl",
"sv",
"tk",
"tr",
"uk",
"zh-hans",
"zh-hant"
],
rootURI + 'locale/{locale}/'
);

ztoolkit.getGlobal("L10nRegistry").getInstance().registerSources([source]);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions zotero-plugin.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { copyFileSync, readdirSync, renameSync, mkdirSync, cpSync } from "fs";
import fse from "fs-extra";
import { replaceInFileSync } from "zotero-plugin-scaffold/tools";

import tags from "language-tags";

export default defineConfig({
source: ["src", "static"],
dist: "build",
Expand Down Expand Up @@ -54,6 +56,23 @@ export default defineConfig({
"build:copyAssets": (ctx) => {
const localePath = "build/addon/locale/";
fse.moveSync("build/addon/chrome/locale/", localePath);

// rename language tags using the correct casing
// otherwise they are ignored by Zotero
for (const dirent of readdirSync(
localePath, { withFileTypes: true }
)) {
if (dirent.isDirectory()) {
const langTag = tags(dirent.name);
if (langTag.valid()) {
renameSync(
localePath + dirent.name,
localePath + langTag.format()
)
}
}
}

// rename wikicite.properties to addon.ftl
for (const path of readdirSync(localePath, {
encoding: "utf-8",
Expand Down

0 comments on commit 15de93b

Please sign in to comment.