Skip to content

Commit

Permalink
Use "full" localization ids in the PDFDocumentProperties class
Browse files Browse the repository at this point in the history
It was recently brought to my attention that using partial or generated localization ids is bad for maintainability, which means that PR 18636 wasn't the correct thing to do.
However, just reverting that one doesn't really fix the problems which is why this patch updates *every* l10n-id in the `PDFDocumentProperties` class (but doesn't touch any `viewer.ftl`-files). Obviously this leads to more verbose code, but that cannot really be helped.
  • Loading branch information
Snuffleupagus committed Aug 29, 2024
1 parent 044e761 commit a6e5416
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions web/pdf_document_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ const NON_METRIC_LOCALES = ["en-us", "en-lr", "my"];
// which are l10n-ids, should be lowercase.
// See https://en.wikipedia.org/wiki/Paper_size
const US_PAGE_NAMES = {
"8.5x11": "letter",
"8.5x14": "legal",
"8.5x11": "pdfjs-document-properties-page-size-name-letter",
"8.5x14": "pdfjs-document-properties-page-size-name-legal",
};
const METRIC_PAGE_NAMES = {
"297x420": "a-three",
"210x297": "a-four",
"297x420": "pdfjs-document-properties-page-size-name-a-three",
"210x297": "pdfjs-document-properties-page-size-name-a-four",
};

function getPageName(size, isPortrait, pageNames) {
Expand Down Expand Up @@ -227,15 +227,16 @@ class PDFDocumentProperties {
}
}

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

async #parseFileSize(b = 0) {
const kb = b / 1024,
mb = kb / 1024;
return kb
? this.#getL10nStr(`size-${mb >= 1 ? "mb" : "kb"}`, { mb, kb, b })
? this.l10n.get(
mb >= 1
? "pdfjs-document-properties-size-mb"
: "pdfjs-document-properties-size-kb",
{ mb, kb, b }
)
: undefined;
}

Expand All @@ -262,12 +263,12 @@ class PDFDocumentProperties {
height: Math.round(pageSizeInches.height * 25.4 * 10) / 10,
};

let rawName =
let nameId =
getPageName(sizeInches, isPortrait, US_PAGE_NAMES) ||
getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);

if (
!rawName &&
!nameId &&
!(
Number.isInteger(sizeMillimeters.width) &&
Number.isInteger(sizeMillimeters.height)
Expand All @@ -290,8 +291,8 @@ class PDFDocumentProperties {
Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 &&
Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1
) {
rawName = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
if (rawName) {
nameId = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
if (nameId) {
// Update *both* sizes, computed above, to ensure that the displayed
// dimensions always correspond to the detected page name.
sizeInches = {
Expand All @@ -305,30 +306,40 @@ class PDFDocumentProperties {

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

return this.#getL10nStr(
`page-size-dimension-${name ? "name-" : ""}string`,
return this.l10n.get(
name
? "pdfjs-document-properties-page-size-dimension-name-string"
: "pdfjs-document-properties-page-size-dimension-string",
{ width, height, unit, name, orientation }
);
}

async #parseDate(inputDate) {
const dateObj = PDFDateString.toDateObject(inputDate);
return dateObj
? this.#getL10nStr("date-time-string", { dateObj })
? this.l10n.get("pdfjs-document-properties-date-time-string", { dateObj })
: undefined;
}

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

Expand Down

0 comments on commit a6e5416

Please sign in to comment.