mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +02:00
Introduce a helper method for fetching l10n-data in PDFDocumentProperties
Given the length of the l10n-strings we can slightly reduce verbosity, and thus overall code-size, by introducing a helper method for fetching l10n-data.
While testing this I stumbled upon an issue in the `L10n`-class, where an optional chaining operator was placed incorrectly since the underlying method always return an Array; see 48e2a62ed4/fluent-dom/src/localization.js (L38-L77)
This commit is contained in:
parent
908f453384
commit
2c34b64415
2 changed files with 15 additions and 20 deletions
|
@ -66,7 +66,7 @@ class L10n {
|
||||||
args,
|
args,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
return messages?.[0].value || fallback;
|
return messages[0]?.value || fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritdoc */
|
/** @inheritdoc */
|
||||||
|
|
|
@ -235,13 +235,17 @@ class PDFDocumentProperties {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#getL10nStr(id, args = null) {
|
||||||
|
return this.l10n.get(`pdfjs-document-properties-${id}`, args);
|
||||||
|
}
|
||||||
|
|
||||||
async #parseFileSize(fileSize = 0) {
|
async #parseFileSize(fileSize = 0) {
|
||||||
const kb = fileSize / 1024,
|
const kb = fileSize / 1024,
|
||||||
mb = kb / 1024;
|
mb = kb / 1024;
|
||||||
if (!kb) {
|
if (!kb) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return this.l10n.get(`pdfjs-document-properties-${mb >= 1 ? "mb" : "kb"}`, {
|
return this.#getL10nStr(mb >= 1 ? "mb" : "kb", {
|
||||||
size_mb: mb >= 1 && (+mb.toPrecision(3)).toLocaleString(),
|
size_mb: mb >= 1 && (+mb.toPrecision(3)).toLocaleString(),
|
||||||
size_kb: mb < 1 && (+kb.toPrecision(3)).toLocaleString(),
|
size_kb: mb < 1 && (+kb.toPrecision(3)).toLocaleString(),
|
||||||
size_b: fileSize.toLocaleString(),
|
size_b: fileSize.toLocaleString(),
|
||||||
|
@ -314,24 +318,17 @@ class PDFDocumentProperties {
|
||||||
|
|
||||||
const [{ width, height }, unit, name, orientation] = await Promise.all([
|
const [{ width, height }, unit, name, orientation] = await Promise.all([
|
||||||
this._isNonMetricLocale ? sizeInches : sizeMillimeters,
|
this._isNonMetricLocale ? sizeInches : sizeMillimeters,
|
||||||
this.l10n.get(
|
this.#getL10nStr(
|
||||||
`pdfjs-document-properties-page-size-unit-${
|
`page-size-unit-${this._isNonMetricLocale ? "inches" : "millimeters"}`
|
||||||
this._isNonMetricLocale ? "inches" : "millimeters"
|
|
||||||
}`
|
|
||||||
),
|
),
|
||||||
rawName &&
|
rawName && this.#getL10nStr(`page-size-name-${rawName}`),
|
||||||
this.l10n.get(`pdfjs-document-properties-page-size-name-${rawName}`),
|
this.#getL10nStr(
|
||||||
this.l10n.get(
|
`page-size-orientation-${isPortrait ? "portrait" : "landscape"}`
|
||||||
`pdfjs-document-properties-page-size-orientation-${
|
|
||||||
isPortrait ? "portrait" : "landscape"
|
|
||||||
}`
|
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return this.l10n.get(
|
return this.#getL10nStr(
|
||||||
`pdfjs-document-properties-page-size-dimension-${
|
`page-size-dimension-${name ? "name-" : ""}string`,
|
||||||
name ? "name-" : ""
|
|
||||||
}string`,
|
|
||||||
{
|
{
|
||||||
width: width.toLocaleString(),
|
width: width.toLocaleString(),
|
||||||
height: height.toLocaleString(),
|
height: height.toLocaleString(),
|
||||||
|
@ -347,16 +344,14 @@ class PDFDocumentProperties {
|
||||||
if (!dateObject) {
|
if (!dateObject) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return this.l10n.get("pdfjs-document-properties-date-string", {
|
return this.#getL10nStr("date-string", {
|
||||||
date: dateObject.toLocaleDateString(),
|
date: dateObject.toLocaleDateString(),
|
||||||
time: dateObject.toLocaleTimeString(),
|
time: dateObject.toLocaleTimeString(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#parseLinearization(isLinearized) {
|
#parseLinearization(isLinearized) {
|
||||||
return this.l10n.get(
|
return this.#getL10nStr(`linearized-${isLinearized ? "yes" : "no"}`);
|
||||||
`pdfjs-document-properties-linearized-${isLinearized ? "yes" : "no"}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue