Skip to content

Commit

Permalink
Merge pull request #18636 from Snuffleupagus/PDFDocumentProperties-#g…
Browse files Browse the repository at this point in the history
…etL10nStr

Introduce a helper method for fetching l10n-data in `PDFDocumentProperties`
  • Loading branch information
timvandermeij authored Aug 23, 2024
2 parents 5f7637b + 2c34b64 commit f025cec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion web/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class L10n {
args,
},
]);
return messages?.[0].value || fallback;
return messages[0]?.value || fallback;
}

/** @inheritdoc */
Expand Down
33 changes: 14 additions & 19 deletions web/pdf_document_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,17 @@ class PDFDocumentProperties {
}
}

#getL10nStr(id, args = null) {
return this.l10n.get(`pdfjs-document-properties-${id}`, args);
}

async #parseFileSize(fileSize = 0) {
const kb = fileSize / 1024,
mb = kb / 1024;
if (!kb) {
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_kb: mb < 1 && (+kb.toPrecision(3)).toLocaleString(),
size_b: fileSize.toLocaleString(),
Expand Down Expand Up @@ -314,24 +318,17 @@ class PDFDocumentProperties {

const [{ width, height }, unit, name, orientation] = await Promise.all([
this._isNonMetricLocale ? sizeInches : sizeMillimeters,
this.l10n.get(
`pdfjs-document-properties-page-size-unit-${
this._isNonMetricLocale ? "inches" : "millimeters"
}`
this.#getL10nStr(
`page-size-unit-${this._isNonMetricLocale ? "inches" : "millimeters"}`
),
rawName &&
this.l10n.get(`pdfjs-document-properties-page-size-name-${rawName}`),
this.l10n.get(
`pdfjs-document-properties-page-size-orientation-${
isPortrait ? "portrait" : "landscape"
}`
rawName && this.#getL10nStr(`page-size-name-${rawName}`),
this.#getL10nStr(
`page-size-orientation-${isPortrait ? "portrait" : "landscape"}`
),
]);

return this.l10n.get(
`pdfjs-document-properties-page-size-dimension-${
name ? "name-" : ""
}string`,
return this.#getL10nStr(
`page-size-dimension-${name ? "name-" : ""}string`,
{
width: width.toLocaleString(),
height: height.toLocaleString(),
Expand All @@ -347,16 +344,14 @@ class PDFDocumentProperties {
if (!dateObject) {
return undefined;
}
return this.l10n.get("pdfjs-document-properties-date-string", {
return this.#getL10nStr("date-string", {
date: dateObject.toLocaleDateString(),
time: dateObject.toLocaleTimeString(),
});
}

#parseLinearization(isLinearized) {
return this.l10n.get(
`pdfjs-document-properties-linearized-${isLinearized ? "yes" : "no"}`
);
return this.#getL10nStr(`linearized-${isLinearized ? "yes" : "no"}`);
}
}

Expand Down

0 comments on commit f025cec

Please sign in to comment.