ext-localize 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -0
- package/LICENSE +21 -0
- package/README.md +586 -2
- package/assets/fill-core.js +398 -0
- package/dist/batch.js +25 -0
- package/dist/check.js +392 -0
- package/dist/cli.js +722 -0
- package/dist/config.js +114 -0
- package/dist/glossary.js +71 -0
- package/dist/import.js +89 -0
- package/dist/listing.js +318 -0
- package/dist/locales.js +86 -0
- package/dist/messages.js +123 -0
- package/dist/paths.js +30 -0
- package/dist/placeholders.js +171 -0
- package/dist/plurals.js +63 -0
- package/dist/providers/anthropic.js +60 -0
- package/dist/providers/deepl.js +269 -0
- package/dist/providers/index.js +40 -0
- package/dist/providers/ollama.js +48 -0
- package/dist/providers/openai.js +50 -0
- package/dist/providers/prompt.js +100 -0
- package/dist/providers/types.js +18 -0
- package/dist/retry.js +66 -0
- package/dist/stale.js +152 -0
- package/dist/state.js +237 -0
- package/dist/stores/chrome-auto.js +205 -0
- package/dist/stores/chrome.js +194 -0
- package/dist/stores/edge.js +43 -0
- package/dist/stores/firefox.js +235 -0
- package/dist/translate.js +309 -0
- package/dist/usage.js +3 -0
- package/dist/zip.js +88 -0
- package/package.json +55 -4
package/dist/config.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { FORMALITIES } from "./providers/types.js";
|
|
4
|
+
import { localizerDir } from "./paths.js";
|
|
5
|
+
/**
|
|
6
|
+
* Optional project config: .localizer/config.json. Command-line flags win over it.
|
|
7
|
+
*
|
|
8
|
+
* {
|
|
9
|
+
* "provider": "deepl",
|
|
10
|
+
* "model": "claude-opus-5-5",
|
|
11
|
+
* "effort": "medium",
|
|
12
|
+
* "formality": "prefer_more",
|
|
13
|
+
* "to": "de,fr,pt-BR", // or "all", or ["de", "fr"]
|
|
14
|
+
* "glossary": ".localizer/glossary.json",
|
|
15
|
+
* "stores": ["chrome", "firefox"], // whose listing limits `check` enforces
|
|
16
|
+
* "listing": { "dir": "listing" },
|
|
17
|
+
* "firefox": { "addon": "my-addon", "defaultLocale": "en-US", "extraLocales": [], "host": "https://addons.mozilla.org" },
|
|
18
|
+
* "chrome": { "item": "<32-letter id>", "publisher": "<uuid>", "testItems": ["<id>"], "browser": "launch", "profile": "~/.ext-localize/chrome-profile" }
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* Unknown keys are an error, so a typo does not silently do nothing.
|
|
22
|
+
*/
|
|
23
|
+
export const STORES = ["chrome", "firefox", "edge"];
|
|
24
|
+
export function configPath(root) {
|
|
25
|
+
return join(localizerDir(root), "config.json");
|
|
26
|
+
}
|
|
27
|
+
/** A bad config file or a bad --store value. The CLI exits with 2. */
|
|
28
|
+
export class ConfigError extends Error {
|
|
29
|
+
}
|
|
30
|
+
const SHAPE = {
|
|
31
|
+
provider: "string",
|
|
32
|
+
model: "string",
|
|
33
|
+
effort: "string",
|
|
34
|
+
formality: "string",
|
|
35
|
+
to: "string",
|
|
36
|
+
glossary: "string",
|
|
37
|
+
stores: "string[]",
|
|
38
|
+
listing: { dir: "string" },
|
|
39
|
+
firefox: { addon: "string", defaultLocale: "string", extraLocales: "string[]", host: "string" },
|
|
40
|
+
chrome: { item: "string", publisher: "string", testItems: "string[]", browser: "string", profile: "string" },
|
|
41
|
+
};
|
|
42
|
+
function validate(value, shape, where) {
|
|
43
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
44
|
+
throw new ConfigError(`${where} must be an object`);
|
|
45
|
+
for (const [key, v] of Object.entries(value)) {
|
|
46
|
+
const want = shape[key];
|
|
47
|
+
const at = where ? `${where}.${key}` : key;
|
|
48
|
+
if (want === undefined)
|
|
49
|
+
throw new ConfigError(`unknown setting "${at}"`);
|
|
50
|
+
if (want === "string") {
|
|
51
|
+
if (typeof v !== "string")
|
|
52
|
+
throw new ConfigError(`"${at}" must be a string`);
|
|
53
|
+
}
|
|
54
|
+
else if (want === "string[]") {
|
|
55
|
+
if (!Array.isArray(v) || v.some((x) => typeof x !== "string"))
|
|
56
|
+
throw new ConfigError(`"${at}" must be an array of strings`);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
validate(v, want, at);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export function parseConfig(data, label = "config") {
|
|
64
|
+
// "to" may also be an array; turn it into the flag form.
|
|
65
|
+
if (data && typeof data === "object" && Array.isArray(data.to)) {
|
|
66
|
+
const to = data.to;
|
|
67
|
+
if (to.some((x) => typeof x !== "string"))
|
|
68
|
+
throw new ConfigError(`${label}: "to" must be a string or an array of strings`);
|
|
69
|
+
data = { ...data, to: to.join(",") };
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
validate(data, SHAPE, "");
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
throw new ConfigError(`${label}: ${e.message}`);
|
|
76
|
+
}
|
|
77
|
+
const config = data;
|
|
78
|
+
if (config.formality !== undefined && !FORMALITIES.includes(config.formality)) {
|
|
79
|
+
throw new ConfigError(`${label}: "formality" must be one of ${FORMALITIES.join(", ")}`);
|
|
80
|
+
}
|
|
81
|
+
for (const s of config.stores ?? []) {
|
|
82
|
+
if (!STORES.includes(s))
|
|
83
|
+
throw new ConfigError(`${label}: unknown store "${s}" (use ${STORES.join(", ")})`);
|
|
84
|
+
}
|
|
85
|
+
return config;
|
|
86
|
+
}
|
|
87
|
+
/** Load .localizer/config.json (or an explicit file). A missing default file is an empty config. */
|
|
88
|
+
export function loadConfig(root, explicitPath) {
|
|
89
|
+
const path = explicitPath ?? configPath(root);
|
|
90
|
+
if (!existsSync(path)) {
|
|
91
|
+
if (explicitPath)
|
|
92
|
+
throw new ConfigError(`config file not found: ${explicitPath}`);
|
|
93
|
+
return {};
|
|
94
|
+
}
|
|
95
|
+
let data;
|
|
96
|
+
try {
|
|
97
|
+
data = JSON.parse(readFileSync(path, "utf8"));
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
throw new ConfigError(`${path}: invalid JSON: ${e.message}`);
|
|
101
|
+
}
|
|
102
|
+
return parseConfig(data, path);
|
|
103
|
+
}
|
|
104
|
+
/** Parse a --store list ("chrome,firefox"). */
|
|
105
|
+
export function parseStores(value) {
|
|
106
|
+
const out = [];
|
|
107
|
+
for (const raw of value.split(",").map((s) => s.trim().toLowerCase()).filter(Boolean)) {
|
|
108
|
+
if (!STORES.includes(raw))
|
|
109
|
+
throw new ConfigError(`unknown store "${raw}" (use ${STORES.join(", ")})`);
|
|
110
|
+
if (!out.includes(raw))
|
|
111
|
+
out.push(raw);
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
package/dist/glossary.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { normalizeLocale } from "./locales.js";
|
|
4
|
+
import { localizerDir } from "./paths.js";
|
|
5
|
+
export const EMPTY_GLOSSARY = { keep: [], terms: {} };
|
|
6
|
+
export function defaultGlossaryPath(root) {
|
|
7
|
+
return join(localizerDir(root), "glossary.json");
|
|
8
|
+
}
|
|
9
|
+
export function parseGlossary(data, label = "glossary") {
|
|
10
|
+
if (!data || typeof data !== "object" || Array.isArray(data))
|
|
11
|
+
throw new Error(`${label}: must be a JSON object`);
|
|
12
|
+
const obj = data;
|
|
13
|
+
const keep = [];
|
|
14
|
+
if (obj.keep !== undefined) {
|
|
15
|
+
if (!Array.isArray(obj.keep) || obj.keep.some((t) => typeof t !== "string")) {
|
|
16
|
+
throw new Error(`${label}: "keep" must be an array of strings`);
|
|
17
|
+
}
|
|
18
|
+
keep.push(...obj.keep.filter((t) => t.trim()));
|
|
19
|
+
}
|
|
20
|
+
const terms = {};
|
|
21
|
+
if (obj.terms !== undefined) {
|
|
22
|
+
if (!obj.terms || typeof obj.terms !== "object" || Array.isArray(obj.terms)) {
|
|
23
|
+
throw new Error(`${label}: "terms" must be an object`);
|
|
24
|
+
}
|
|
25
|
+
for (const [term, map] of Object.entries(obj.terms)) {
|
|
26
|
+
if (!map || typeof map !== "object" || Array.isArray(map)) {
|
|
27
|
+
throw new Error(`${label}: terms["${term}"] must be an object of locale -> translation`);
|
|
28
|
+
}
|
|
29
|
+
const byLocale = {};
|
|
30
|
+
for (const [loc, value] of Object.entries(map)) {
|
|
31
|
+
const n = normalizeLocale(loc);
|
|
32
|
+
if (!n)
|
|
33
|
+
throw new Error(`${label}: terms["${term}"] has unknown locale "${loc}"`);
|
|
34
|
+
if (typeof value !== "string")
|
|
35
|
+
throw new Error(`${label}: terms["${term}"]["${loc}"] must be a string`);
|
|
36
|
+
byLocale[n] = value;
|
|
37
|
+
}
|
|
38
|
+
terms[term] = byLocale;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return { keep, terms };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Load the glossary. An explicit path must exist; the default path is optional.
|
|
45
|
+
*/
|
|
46
|
+
export function loadGlossary(root, explicitPath) {
|
|
47
|
+
const path = explicitPath ?? defaultGlossaryPath(root);
|
|
48
|
+
if (!existsSync(path)) {
|
|
49
|
+
if (explicitPath)
|
|
50
|
+
throw new Error(`Glossary file not found: ${explicitPath}`);
|
|
51
|
+
return EMPTY_GLOSSARY;
|
|
52
|
+
}
|
|
53
|
+
let data;
|
|
54
|
+
try {
|
|
55
|
+
data = JSON.parse(readFileSync(path, "utf8"));
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
throw new Error(`${path}: invalid JSON: ${e.message}`);
|
|
59
|
+
}
|
|
60
|
+
return parseGlossary(data, path);
|
|
61
|
+
}
|
|
62
|
+
/** Fixed translations that apply to one locale: [[term, translation], ...]. */
|
|
63
|
+
export function termsFor(glossary, locale) {
|
|
64
|
+
const out = [];
|
|
65
|
+
for (const [term, map] of Object.entries(glossary.terms)) {
|
|
66
|
+
const t = map[locale];
|
|
67
|
+
if (t !== undefined)
|
|
68
|
+
out.push([term, t]);
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
package/dist/import.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join } from "node:path";
|
|
3
|
+
import { UsageError } from "./usage.js";
|
|
4
|
+
import { LISTING_FIELDS, listingPath, localeMessage, readListingFile, syncedFields } from "./listing.js";
|
|
5
|
+
import { normalizeLocale } from "./locales.js";
|
|
6
|
+
import { writeJsonAtomic } from "./messages.js";
|
|
7
|
+
/**
|
|
8
|
+
* Find description files. `pattern` is a folder (files named <locale>.txt) or a
|
|
9
|
+
* path whose file name contains "{locale}", for example store/descriptions/{locale}.txt.
|
|
10
|
+
* Locale codes in file names are normalized (pt-BR -> pt_BR).
|
|
11
|
+
*/
|
|
12
|
+
export function findDescriptionFiles(pattern) {
|
|
13
|
+
let dir;
|
|
14
|
+
let re;
|
|
15
|
+
if (pattern.includes("{locale}")) {
|
|
16
|
+
dir = dirname(pattern);
|
|
17
|
+
const name = basename(pattern);
|
|
18
|
+
if (dir.includes("{locale}"))
|
|
19
|
+
throw new UsageError(`"{locale}" may only appear in the file name: ${pattern}`);
|
|
20
|
+
const [before, after] = name.split("{locale}");
|
|
21
|
+
const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
22
|
+
re = new RegExp(`^${esc(before)}([A-Za-z]{2,3}(?:[-_][A-Za-z0-9]{2,4})?)${esc(after)}$`);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
dir = pattern;
|
|
26
|
+
re = /^([A-Za-z]{2,3}(?:[-_][A-Za-z0-9]{2,4})?)\.txt$/;
|
|
27
|
+
}
|
|
28
|
+
if (!existsSync(dir) || !statSync(dir).isDirectory())
|
|
29
|
+
throw new UsageError(`folder not found: ${dir}`);
|
|
30
|
+
const out = [];
|
|
31
|
+
for (const f of readdirSync(dir).sort()) {
|
|
32
|
+
const m = re.exec(f);
|
|
33
|
+
if (m)
|
|
34
|
+
out.push({ locale: normalizeLocale(m[1]), raw: m[1], path: join(dir, f) });
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
/** Write listing/<locale>.json from description files. Existing files are kept unless force. */
|
|
39
|
+
export function importDescriptions(project, listingDirAbs, pattern, opts = {}) {
|
|
40
|
+
const files = findDescriptionFiles(pattern);
|
|
41
|
+
if (!files.length)
|
|
42
|
+
throw new UsageError(`no description files found for ${pattern}`);
|
|
43
|
+
const synced = syncedFields(project);
|
|
44
|
+
const rows = [];
|
|
45
|
+
for (const f of files) {
|
|
46
|
+
if (!f.locale) {
|
|
47
|
+
rows.push({ locale: f.raw, action: "skipped", note: "not a Chrome locale code" });
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const target = listingPath(listingDirAbs, f.locale);
|
|
51
|
+
let existing = {};
|
|
52
|
+
if (existsSync(target)) {
|
|
53
|
+
if (!opts.force) {
|
|
54
|
+
rows.push({ locale: f.locale, action: "skipped", note: `${basename(target)} exists (use --force to overwrite)` });
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const r = readListingFile(target, f.locale === project.defaultLocale);
|
|
58
|
+
if (r.ok)
|
|
59
|
+
existing = r.data;
|
|
60
|
+
}
|
|
61
|
+
let text = readFileSync(f.path, "utf8");
|
|
62
|
+
if (text.charCodeAt(0) === 0xfeff)
|
|
63
|
+
text = text.slice(1);
|
|
64
|
+
text = text.replace(/\r\n/g, "\n").replace(/\n+$/, "");
|
|
65
|
+
if (!text.trim()) {
|
|
66
|
+
rows.push({ locale: f.locale, action: "skipped", note: "empty file" });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const out = {};
|
|
70
|
+
const notes = [];
|
|
71
|
+
for (const field of LISTING_FIELDS) {
|
|
72
|
+
if (field === "description") {
|
|
73
|
+
out.description = text;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const key = synced[field];
|
|
77
|
+
const fromLocales = key ? localeMessage(project, f.locale, key) : undefined;
|
|
78
|
+
if (fromLocales !== undefined)
|
|
79
|
+
out[field] = fromLocales;
|
|
80
|
+
else if (existing[field] !== undefined)
|
|
81
|
+
out[field] = existing[field];
|
|
82
|
+
else
|
|
83
|
+
notes.push(`no ${field}`);
|
|
84
|
+
}
|
|
85
|
+
writeJsonAtomic(target, out);
|
|
86
|
+
rows.push({ locale: f.locale, action: "written", note: notes.length ? `${notes.join(", ")}: add it by hand or run translate --listing` : undefined });
|
|
87
|
+
}
|
|
88
|
+
return rows;
|
|
89
|
+
}
|
package/dist/listing.js
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
2
|
+
import { basename, join, resolve } from "node:path";
|
|
3
|
+
import { relPath } from "./paths.js";
|
|
4
|
+
import { isChromeLocale } from "./locales.js";
|
|
5
|
+
import { listLocaleDirs, messagesPath, readMessagesFile, writeJsonAtomic } from "./messages.js";
|
|
6
|
+
/**
|
|
7
|
+
* Store listing pack: listing/<locale>.json = { "name", "summary", "description" }.
|
|
8
|
+
*
|
|
9
|
+
* The source is listing/<default_locale>.json. In the source only, "description"
|
|
10
|
+
* may also be an array of lines (joined with "\n"), which is easier to edit and diff.
|
|
11
|
+
* Translated files are always written with three plain strings.
|
|
12
|
+
*
|
|
13
|
+
* When manifest.json uses "__MSG_x__" for "name" (or "description"), the listing
|
|
14
|
+
* name (or summary) is taken from _locales/<locale>/messages.json and never
|
|
15
|
+
* translated on its own: that is what Chrome shows, so the stores stay in sync.
|
|
16
|
+
*/
|
|
17
|
+
export const LISTING_FIELDS = ["name", "summary", "description"];
|
|
18
|
+
/** Character limits per store (code points). Edge: only the description is typed per language. */
|
|
19
|
+
export const STORE_LIMITS = {
|
|
20
|
+
// name 75 and summary 132 are the manifest limits; 16,000 is the dashboard textarea maxlength.
|
|
21
|
+
chrome: { name: 75, summary: 132, description: 16000 },
|
|
22
|
+
// addons-server Addon model (name 50, summary 250, description 15000); the API returns 400 above them.
|
|
23
|
+
firefox: { name: 50, summary: 250, description: 15000 },
|
|
24
|
+
// Partner Center: "Minimum 250 characters, maximum 10,000 characters" for the description.
|
|
25
|
+
edge: { description: 10000 },
|
|
26
|
+
};
|
|
27
|
+
export const EDGE_DESCRIPTION_MIN = 250;
|
|
28
|
+
export const STORE_LABEL = {
|
|
29
|
+
chrome: "Chrome Web Store",
|
|
30
|
+
firefox: "Firefox Add-ons (AMO)",
|
|
31
|
+
edge: "Edge Add-ons",
|
|
32
|
+
};
|
|
33
|
+
const FIELD_CONTEXT = {
|
|
34
|
+
name: "Extension name in the store listing.",
|
|
35
|
+
summary: "Short summary shown under the name in the store listing.",
|
|
36
|
+
description: "Long description on the store page. Keep paragraphs, line breaks and list markers.",
|
|
37
|
+
};
|
|
38
|
+
/** Manifest field that feeds each listing field when it uses __MSG_x__. */
|
|
39
|
+
const MANIFEST_FIELD = { name: "name", summary: "description" };
|
|
40
|
+
/**
|
|
41
|
+
* The listing folder. A relative `dir` is resolved from `base` (the extension
|
|
42
|
+
* folder by default); an absolute one is used as is.
|
|
43
|
+
*/
|
|
44
|
+
export function listingDir(root, dir = "listing", base = root) {
|
|
45
|
+
return resolve(base, dir);
|
|
46
|
+
}
|
|
47
|
+
export function listingPath(dir, locale) {
|
|
48
|
+
return join(dir, `${locale}.json`);
|
|
49
|
+
}
|
|
50
|
+
export const codePoints = (s) => [...s].length;
|
|
51
|
+
/**
|
|
52
|
+
* Read one listing file. `allowLines`: description may be an array of strings
|
|
53
|
+
* (source file only). Problems are shape errors that make a field unusable.
|
|
54
|
+
*/
|
|
55
|
+
export function readListingFile(path, allowLines = false) {
|
|
56
|
+
let raw;
|
|
57
|
+
try {
|
|
58
|
+
let text = readFileSync(path, "utf8");
|
|
59
|
+
if (text.charCodeAt(0) === 0xfeff)
|
|
60
|
+
text = text.slice(1);
|
|
61
|
+
raw = JSON.parse(text);
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
return { ok: false, error: `invalid JSON: ${e.message}` };
|
|
65
|
+
}
|
|
66
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
67
|
+
return { ok: false, error: "top level must be a JSON object" };
|
|
68
|
+
const obj = raw;
|
|
69
|
+
const data = {};
|
|
70
|
+
const problems = [];
|
|
71
|
+
for (const field of LISTING_FIELDS) {
|
|
72
|
+
const v = obj[field];
|
|
73
|
+
if (v === undefined)
|
|
74
|
+
continue;
|
|
75
|
+
if (typeof v === "string")
|
|
76
|
+
data[field] = v;
|
|
77
|
+
else if (field === "description" && allowLines && Array.isArray(v) && v.every((x) => typeof x === "string"))
|
|
78
|
+
data[field] = v.join("\n");
|
|
79
|
+
else
|
|
80
|
+
problems.push(`"${field}" must be a string${field === "description" && allowLines ? " or an array of lines" : ""}`);
|
|
81
|
+
}
|
|
82
|
+
const unknownKeys = Object.keys(obj).filter((k) => !LISTING_FIELDS.includes(k));
|
|
83
|
+
return { ok: true, data, problems, unknownKeys };
|
|
84
|
+
}
|
|
85
|
+
/** Locales that have a listing file (by file name, valid Chrome codes only). */
|
|
86
|
+
export function listListingLocales(dir) {
|
|
87
|
+
if (!existsSync(dir))
|
|
88
|
+
return [];
|
|
89
|
+
return readdirSync(dir)
|
|
90
|
+
.filter((f) => f.endsWith(".json"))
|
|
91
|
+
.map((f) => basename(f, ".json"))
|
|
92
|
+
.filter(isChromeLocale)
|
|
93
|
+
.sort();
|
|
94
|
+
}
|
|
95
|
+
/** "__MSG_extName__" -> "extName", else null. */
|
|
96
|
+
export function msgRef(value) {
|
|
97
|
+
const m = typeof value === "string" ? /^__MSG_([A-Za-z0-9_@]+)__$/.exec(value) : null;
|
|
98
|
+
return m ? m[1] : null;
|
|
99
|
+
}
|
|
100
|
+
/** Message text as shown to users: "$$" is a literal "$". */
|
|
101
|
+
function shownText(message) {
|
|
102
|
+
return message.replace(/\$\$/g, "$");
|
|
103
|
+
}
|
|
104
|
+
/** Look up a message (case-insensitive key) in _locales/<locale>; undefined if the file or key is missing. */
|
|
105
|
+
export function localeMessage(project, locale, key) {
|
|
106
|
+
const path = messagesPath(project.localesDir, locale);
|
|
107
|
+
if (!existsSync(path))
|
|
108
|
+
return undefined;
|
|
109
|
+
const r = readMessagesFile(path);
|
|
110
|
+
if (!r.ok)
|
|
111
|
+
return undefined;
|
|
112
|
+
const lower = key.toLowerCase();
|
|
113
|
+
for (const [k, v] of Object.entries(r.data)) {
|
|
114
|
+
const m = v?.message;
|
|
115
|
+
if (k.toLowerCase() === lower && typeof m === "string" && m.trim() !== "")
|
|
116
|
+
return shownText(m);
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
/** Listing fields that come from _locales: field -> message key. */
|
|
121
|
+
export function syncedFields(project) {
|
|
122
|
+
const out = {};
|
|
123
|
+
for (const [field, mf] of Object.entries(MANIFEST_FIELD)) {
|
|
124
|
+
const ref = msgRef(project.manifest[mf]);
|
|
125
|
+
if (ref)
|
|
126
|
+
out[field] = ref;
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
export class ListingError extends Error {
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Load the source listing. Throws ListingError with a fixable message.
|
|
134
|
+
* A synced field may be left out of the source file; it is read from _locales.
|
|
135
|
+
*/
|
|
136
|
+
export function loadListing(project, dir) {
|
|
137
|
+
const sourcePath = listingPath(dir, project.defaultLocale);
|
|
138
|
+
if (!existsSync(sourcePath)) {
|
|
139
|
+
throw new ListingError(`No source listing: ${sourcePath} is missing. Create it with "name", "summary" and "description" (see README, "Store listing").`);
|
|
140
|
+
}
|
|
141
|
+
const r = readListingFile(sourcePath, true);
|
|
142
|
+
if (!r.ok)
|
|
143
|
+
throw new ListingError(`${sourcePath}: ${r.error}`);
|
|
144
|
+
if (r.problems.length)
|
|
145
|
+
throw new ListingError(`${sourcePath}: ${r.problems.join("; ")}`);
|
|
146
|
+
const synced = syncedFields(project);
|
|
147
|
+
const source = { ...r.data };
|
|
148
|
+
for (const [field, key] of Object.entries(synced)) {
|
|
149
|
+
if (source[field] === undefined) {
|
|
150
|
+
const text = localeMessage(project, project.defaultLocale, key);
|
|
151
|
+
if (text !== undefined)
|
|
152
|
+
source[field] = text;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const field of LISTING_FIELDS) {
|
|
156
|
+
if (source[field] === undefined || source[field].trim() === "") {
|
|
157
|
+
throw new ListingError(`${sourcePath}: "${field}" is missing or empty`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return { dir, sourceLocale: project.defaultLocale, source, synced };
|
|
161
|
+
}
|
|
162
|
+
/** The tightest limit per field over the given stores. */
|
|
163
|
+
export function limitsFor(stores) {
|
|
164
|
+
const out = {};
|
|
165
|
+
for (const store of stores) {
|
|
166
|
+
for (const [field, max] of Object.entries(STORE_LIMITS[store])) {
|
|
167
|
+
out[field] = Math.min(out[field] ?? Infinity, max);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
function toMessages(listing) {
|
|
173
|
+
const out = {};
|
|
174
|
+
for (const field of LISTING_FIELDS)
|
|
175
|
+
if (listing[field] !== undefined)
|
|
176
|
+
out[field] = { message: listing[field] };
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
/** Translation job for the listing. Name and summary are copied from _locales where the manifest uses __MSG_. */
|
|
180
|
+
export function listingJob(project, lp, stores) {
|
|
181
|
+
const source = {};
|
|
182
|
+
for (const field of LISTING_FIELDS) {
|
|
183
|
+
source[field] = { message: lp.source[field], description: FIELD_CONTEXT[field] };
|
|
184
|
+
}
|
|
185
|
+
const maxLength = limitsFor(stores);
|
|
186
|
+
return {
|
|
187
|
+
kind: "listing",
|
|
188
|
+
root: project.root,
|
|
189
|
+
sourceLocale: lp.sourceLocale,
|
|
190
|
+
source,
|
|
191
|
+
section: "listing",
|
|
192
|
+
formatTokens: false,
|
|
193
|
+
maxLength: maxLength,
|
|
194
|
+
read(locale) {
|
|
195
|
+
const path = listingPath(lp.dir, locale);
|
|
196
|
+
if (!existsSync(path))
|
|
197
|
+
return { ok: true, data: null };
|
|
198
|
+
const r = readListingFile(path);
|
|
199
|
+
if (!r.ok)
|
|
200
|
+
return { ok: false, error: r.error };
|
|
201
|
+
return { ok: true, data: toMessages(r.data) };
|
|
202
|
+
},
|
|
203
|
+
write(locale, file) {
|
|
204
|
+
const out = {};
|
|
205
|
+
for (const field of LISTING_FIELDS) {
|
|
206
|
+
const m = file[field]?.message;
|
|
207
|
+
if (m !== undefined)
|
|
208
|
+
out[field] = m;
|
|
209
|
+
}
|
|
210
|
+
writeJsonAtomic(listingPath(lp.dir, locale), out);
|
|
211
|
+
},
|
|
212
|
+
fileFor: (locale) => relPath(project.root, listingPath(lp.dir, locale)),
|
|
213
|
+
fixed(locale) {
|
|
214
|
+
const out = {};
|
|
215
|
+
for (const [field, key] of Object.entries(lp.synced)) {
|
|
216
|
+
const text = localeMessage(project, locale, key);
|
|
217
|
+
if (text !== undefined)
|
|
218
|
+
out[field] = text;
|
|
219
|
+
}
|
|
220
|
+
return out;
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/** Default listing targets: existing _locales folders and existing listing files. */
|
|
225
|
+
export function defaultListingTargets(project, dir) {
|
|
226
|
+
const set = new Set([...listLocaleDirs(project.localesDir).filter(isChromeLocale), ...listListingLocales(dir)]);
|
|
227
|
+
set.delete(project.defaultLocale);
|
|
228
|
+
return [...set].sort();
|
|
229
|
+
}
|
|
230
|
+
/** Read every listing file (source first rules: description lines allowed only in the source). */
|
|
231
|
+
export function readAllListings(dir, sourceLocale) {
|
|
232
|
+
const out = {};
|
|
233
|
+
for (const locale of listListingLocales(dir)) {
|
|
234
|
+
const r = readListingFile(listingPath(dir, locale), locale === sourceLocale);
|
|
235
|
+
if (!r.ok)
|
|
236
|
+
throw new ListingError(`${listingPath(dir, locale)}: ${r.error}`);
|
|
237
|
+
if (r.problems.length)
|
|
238
|
+
throw new ListingError(`${listingPath(dir, locale)}: ${r.problems.join("; ")}`);
|
|
239
|
+
out[locale] = r.data;
|
|
240
|
+
}
|
|
241
|
+
return out;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Listing checks for `check`. Returns [] when the listing folder does not exist,
|
|
245
|
+
* so projects without a listing get the same report as before.
|
|
246
|
+
*/
|
|
247
|
+
export function checkListing(project, dir, stores,
|
|
248
|
+
/** How to show the folder in messages, for example "../store/listing". */
|
|
249
|
+
shown = "listing") {
|
|
250
|
+
if (!existsSync(dir))
|
|
251
|
+
return [];
|
|
252
|
+
const issues = [];
|
|
253
|
+
const add = (level, locale, key, message) => issues.push(key ? { level, locale, key, message } : { level, locale, message });
|
|
254
|
+
const files = readdirSync(dir).filter((f) => f.endsWith(".json")).sort();
|
|
255
|
+
const sourceFile = `${project.defaultLocale}.json`;
|
|
256
|
+
if (!files.includes(sourceFile))
|
|
257
|
+
add("error", project.defaultLocale, undefined, `listing: no source file ${shown}/${sourceFile}`);
|
|
258
|
+
const synced = syncedFields(project);
|
|
259
|
+
for (const file of files) {
|
|
260
|
+
const locale = basename(file, ".json");
|
|
261
|
+
if (!isChromeLocale(locale)) {
|
|
262
|
+
add("error", locale, undefined, `listing: file name "${file}" is not a Chrome locale code (for example de.json, pt_BR.json)`);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
const isSource = locale === project.defaultLocale;
|
|
266
|
+
const r = readListingFile(join(dir, file), isSource);
|
|
267
|
+
if (!r.ok) {
|
|
268
|
+
add("error", locale, undefined, `listing: ${shown}/${file}: ${r.error}`);
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
for (const p of r.problems)
|
|
272
|
+
add("error", locale, undefined, `listing: ${shown}/${file}: ${p}`);
|
|
273
|
+
for (const k of r.unknownKeys)
|
|
274
|
+
add("warning", locale, `listing.${k}`, `listing: unknown field "${k}" (only name, summary, description are used)`);
|
|
275
|
+
const data = { ...r.data };
|
|
276
|
+
for (const field of LISTING_FIELDS) {
|
|
277
|
+
const key = `listing.${field}`;
|
|
278
|
+
let value = data[field];
|
|
279
|
+
const msgKey = synced[field];
|
|
280
|
+
const fromLocales = msgKey ? localeMessage(project, locale, msgKey) : undefined;
|
|
281
|
+
if (msgKey && fromLocales !== undefined && value !== undefined && value !== fromLocales) {
|
|
282
|
+
add("warning", locale, key, `listing ${field} "${short(value)}" differs from _locales/${locale} ${msgKey} "${short(fromLocales)}"; run "ext-localize translate --listing" to sync`);
|
|
283
|
+
}
|
|
284
|
+
if (value === undefined && isSource && fromLocales !== undefined)
|
|
285
|
+
value = fromLocales;
|
|
286
|
+
if (value === undefined) {
|
|
287
|
+
if (isSource || !(msgKey && fromLocales !== undefined))
|
|
288
|
+
add("error", locale, key, `listing: "${field}" is missing`);
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (value.trim() === "") {
|
|
292
|
+
add("error", locale, key, `listing: "${field}" is empty`);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const len = codePoints(value);
|
|
296
|
+
for (const store of stores) {
|
|
297
|
+
const max = STORE_LIMITS[store][field];
|
|
298
|
+
if (max !== undefined && len > max)
|
|
299
|
+
add("error", locale, key, `listing ${field} is ${len} characters; ${STORE_LABEL[store]} limit is ${max}`);
|
|
300
|
+
}
|
|
301
|
+
if (field === "description" && stores.includes("edge") && len < EDGE_DESCRIPTION_MIN) {
|
|
302
|
+
add("warning", locale, key, `listing description is ${len} characters; Edge Add-ons needs at least ${EDGE_DESCRIPTION_MIN}`);
|
|
303
|
+
}
|
|
304
|
+
if (stores.includes("firefox")) {
|
|
305
|
+
if (field === "description" && /^#{1,6}\s/m.test(value)) {
|
|
306
|
+
add("warning", locale, key, `listing description has "#" heading lines; AMO drops Markdown headings`);
|
|
307
|
+
}
|
|
308
|
+
if (field === "summary" && /https?:\/\/|www\./i.test(value)) {
|
|
309
|
+
add("warning", locale, key, `listing summary has a URL; AMO does not allow URLs in the summary`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return issues;
|
|
315
|
+
}
|
|
316
|
+
function short(s) {
|
|
317
|
+
return s.length > 40 ? `${s.slice(0, 39)}…` : s;
|
|
318
|
+
}
|
package/dist/locales.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locales that the Chrome Web Store and chrome.i18n accept as folder names
|
|
3
|
+
* under _locales/. Source: https://developer.chrome.com/docs/extensions/reference/api/i18n
|
|
4
|
+
*/
|
|
5
|
+
export const CHROME_LOCALES = [
|
|
6
|
+
"ar", "am", "bg", "bn", "ca", "cs", "da", "de", "el", "en", "en_AU", "en_GB",
|
|
7
|
+
"en_US", "es", "es_419", "et", "fa", "fi", "fil", "fr", "gu", "he", "hi", "hr",
|
|
8
|
+
"hu", "id", "it", "ja", "kn", "ko", "lt", "lv", "ml", "mr", "ms", "nl", "no",
|
|
9
|
+
"pl", "pt_BR", "pt_PT", "ro", "ru", "sk", "sl", "sr", "sv", "sw", "ta", "te",
|
|
10
|
+
"th", "tr", "uk", "vi", "zh_CN", "zh_TW",
|
|
11
|
+
];
|
|
12
|
+
const CHROME_LOCALE_SET = new Set(CHROME_LOCALES);
|
|
13
|
+
/** Locales written right to left. They get an extra warning-level check. */
|
|
14
|
+
export const RTL_LOCALES = new Set(["ar", "he", "fa"]);
|
|
15
|
+
/** Common codes people type that Chrome spells differently. */
|
|
16
|
+
const ALIASES = {
|
|
17
|
+
nb: "no",
|
|
18
|
+
nn: "no",
|
|
19
|
+
iw: "he",
|
|
20
|
+
tl: "fil",
|
|
21
|
+
"zh_hans": "zh_CN",
|
|
22
|
+
"zh_hant": "zh_TW",
|
|
23
|
+
};
|
|
24
|
+
export function isChromeLocale(code) {
|
|
25
|
+
return CHROME_LOCALE_SET.has(code);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Turn user input like "pt-BR", "PT_br", "zh-cn" or "es-419" into the Chrome
|
|
29
|
+
* folder spelling ("pt_BR", "zh_CN", "es_419"). Returns null when the result
|
|
30
|
+
* is not a Chrome locale.
|
|
31
|
+
*/
|
|
32
|
+
export function normalizeLocale(input) {
|
|
33
|
+
const raw = input.trim().replace(/-/g, "_");
|
|
34
|
+
if (!raw)
|
|
35
|
+
return null;
|
|
36
|
+
const lower = raw.toLowerCase();
|
|
37
|
+
if (ALIASES[lower])
|
|
38
|
+
return ALIASES[lower];
|
|
39
|
+
const [lang, ...rest] = lower.split("_");
|
|
40
|
+
let code = lang ?? "";
|
|
41
|
+
if (rest.length > 0) {
|
|
42
|
+
const region = rest.join("_");
|
|
43
|
+
code += "_" + (/^\d+$/.test(region) ? region : region.toUpperCase());
|
|
44
|
+
}
|
|
45
|
+
return isChromeLocale(code) ? code : null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Parse a --to value: "all", or a comma separated list. Throws on unknown codes.
|
|
49
|
+
* The source locale is always removed: we never write to it.
|
|
50
|
+
*/
|
|
51
|
+
export function parseTargetLocales(value, sourceLocale) {
|
|
52
|
+
const parts = value.split(",").map((p) => p.trim()).filter(Boolean);
|
|
53
|
+
const out = new Set();
|
|
54
|
+
const unknown = [];
|
|
55
|
+
for (const part of parts) {
|
|
56
|
+
if (part.toLowerCase() === "all") {
|
|
57
|
+
for (const l of CHROME_LOCALES)
|
|
58
|
+
out.add(l);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const n = normalizeLocale(part);
|
|
62
|
+
if (n)
|
|
63
|
+
out.add(n);
|
|
64
|
+
else
|
|
65
|
+
unknown.push(part);
|
|
66
|
+
}
|
|
67
|
+
if (unknown.length > 0) {
|
|
68
|
+
throw new Error(`Unknown locale code(s): ${unknown.join(", ")}. Chrome supports: ${CHROME_LOCALES.join(", ")}`);
|
|
69
|
+
}
|
|
70
|
+
out.delete(sourceLocale);
|
|
71
|
+
return [...out].sort();
|
|
72
|
+
}
|
|
73
|
+
/** "pt_BR" -> "pt-BR", for Intl and for prompts. */
|
|
74
|
+
export function toBcp47(locale) {
|
|
75
|
+
return locale.replace(/_/g, "-");
|
|
76
|
+
}
|
|
77
|
+
/** Human name of a locale in English, e.g. "Brazilian Portuguese". */
|
|
78
|
+
export function localeDisplayName(locale) {
|
|
79
|
+
try {
|
|
80
|
+
const names = new Intl.DisplayNames(["en"], { type: "language" });
|
|
81
|
+
return names.of(toBcp47(locale)) ?? locale;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return locale;
|
|
85
|
+
}
|
|
86
|
+
}
|