Skip to content

Collect the l10n error/warning message lookup, in web/app.js, in a new helper method #12981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 70 additions & 128 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,28 @@ const PDFViewerApplication = {
_scriptingInstance: null,
_mouseState: Object.create(null),

_localizeMessage(key, args = null) {
const DEFAULT_L10N_STRINGS = {
error_file: "File: {{file}}",
error_line: "Line: {{line}}",
error_message: "Message: {{message}}",
error_stack: "Stack: {{stack}}",
error_version_info: "PDF.js v{{version}} (build: {{build}})",
invalid_file_error: "Invalid or corrupted PDF file.",
loading_error: "An error occurred while loading the PDF.",
missing_file_error: "Missing PDF file.",
printing_not_ready: "Warning: The PDF is not fully loaded for printing.",
printing_not_supported:
"Warning: Printing is not fully supported by this browser.",
rendering_error: "An error occurred while rendering the page.",
unexpected_response_error: "Unexpected server response.",
web_fonts_disabled:
"Web fonts are disabled: unable to use embedded PDF fonts.",
};

return this.l10n.get(key || "", args, DEFAULT_L10N_STRINGS[key]);
},

// Called once when the document is loaded.
async initialize(appConfig) {
this.preferences = this.externalServices.createPreferences();
Expand Down Expand Up @@ -702,36 +724,25 @@ const PDFViewerApplication = {
throw new Error("Not implemented: initPassiveLoading");
}
this.externalServices.initPassiveLoading({
onOpenWithTransport(url, length, transport) {
PDFViewerApplication.open(url, { length, range: transport });
onOpenWithTransport: (url, length, transport) => {
this.open(url, { length, range: transport });
},
onOpenWithData(data) {
PDFViewerApplication.open(data);
onOpenWithData: data => {
this.open(data);
},
onOpenWithURL(url, length, originalUrl) {
let file = url,
args = null;
if (length !== undefined) {
args = { length };
}
if (originalUrl !== undefined) {
file = { url, originalUrl };
}
PDFViewerApplication.open(file, args);
onOpenWithURL: (url, length, originalUrl) => {
const file = originalUrl !== undefined ? { url, originalUrl } : url;
const args = length !== undefined ? { length } : null;

this.open(file, args);
},
onError(err) {
PDFViewerApplication.l10n
.get(
"loading_error",
null,
"An error occurred while loading the PDF."
)
.then(msg => {
PDFViewerApplication._documentError(msg, err);
});
onError: err => {
this._localizeMessage("loading_error").then(msg => {
this._documentError(msg, err);
});
},
onProgress(loaded, total) {
PDFViewerApplication.progress(loaded / total);
onProgress: (loaded, total) => {
this.progress(loaded / total);
},
});
},
Expand Down Expand Up @@ -948,38 +959,16 @@ const PDFViewerApplication = {
return undefined; // Ignore errors for previously opened PDF files.
}

const message = exception?.message;
let loadingErrorMessage;
let key = "loading_error";
if (exception instanceof InvalidPDFException) {
// change error message also for other builds
loadingErrorMessage = this.l10n.get(
"invalid_file_error",
null,
"Invalid or corrupted PDF file."
);
key = "invalid_file_error";
} else if (exception instanceof MissingPDFException) {
// special message for missing PDF's
loadingErrorMessage = this.l10n.get(
"missing_file_error",
null,
"Missing PDF file."
);
key = "missing_file_error";
} else if (exception instanceof UnexpectedResponseException) {
loadingErrorMessage = this.l10n.get(
"unexpected_response_error",
null,
"Unexpected server response."
);
} else {
loadingErrorMessage = this.l10n.get(
"loading_error",
null,
"An error occurred while loading the PDF."
);
key = "unexpected_response_error";
}

return loadingErrorMessage.then(msg => {
this._documentError(msg, { message });
return this._localizeMessage(key).then(msg => {
this._documentError(msg, { message: exception?.message });
throw exception;
});
}
Expand Down Expand Up @@ -1132,45 +1121,28 @@ const PDFViewerApplication = {
*/
_otherError(message, moreInfo = null) {
const moreInfoText = [
this.l10n.get(
"error_version_info",
{ version: version || "?", build: build || "?" },
"PDF.js v{{version}} (build: {{build}})"
),
this._localizeMessage("error_version_info", {
version: version || "?",
build: build || "?",
}),
];
if (moreInfo) {
moreInfoText.push(
this.l10n.get(
"error_message",
{ message: moreInfo.message },
"Message: {{message}}"
)
this._localizeMessage("error_message", { message: moreInfo.message })
);
if (moreInfo.stack) {
moreInfoText.push(
this.l10n.get(
"error_stack",
{ stack: moreInfo.stack },
"Stack: {{stack}}"
)
this._localizeMessage("error_stack", { stack: moreInfo.stack })
);
} else {
if (moreInfo.filename) {
moreInfoText.push(
this.l10n.get(
"error_file",
{ file: moreInfo.filename },
"File: {{file}}"
)
this._localizeMessage("error_file", { file: moreInfo.filename })
);
}
if (moreInfo.lineNumber) {
moreInfoText.push(
this.l10n.get(
"error_line",
{ line: moreInfo.lineNumber },
"Line: {{line}}"
)
this._localizeMessage("error_line", { line: moreInfo.lineNumber })
);
}
}
Expand Down Expand Up @@ -2054,31 +2026,19 @@ const PDFViewerApplication = {
}

if (!this.supportsPrinting) {
this.l10n
.get(
"printing_not_supported",
null,
"Warning: Printing is not fully supported by this browser."
)
.then(printMessage => {
this._otherError(printMessage);
});
this._localizeMessage("printing_not_supported").then(msg => {
this._otherError(msg);
});
return;
}

// The beforePrint is a sync method and we need to know layout before
// returning from this method. Ensure that we can get sizes of the pages.
if (!this.pdfViewer.pageViewsReady) {
this.l10n
.get(
"printing_not_ready",
null,
"Warning: The PDF is not fully loaded for printing."
)
.then(notReadyMessage => {
// eslint-disable-next-line no-alert
window.alert(notReadyMessage);
});
this._localizeMessage("printing_not_ready").then(msg => {
// eslint-disable-next-line no-alert
window.alert(msg);
});
return;
}

Expand Down Expand Up @@ -2399,13 +2359,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
throw new Error("file origin does not match viewer's");
}
} catch (ex) {
PDFViewerApplication.l10n
.get("loading_error", null, "An error occurred while loading the PDF.")
.then(loadingErrorMessage => {
PDFViewerApplication._documentError(loadingErrorMessage, {
message: ex?.message,
});
});
PDFViewerApplication._localizeMessage("loading_error").then(msg => {
PDFViewerApplication._documentError(msg, { message: ex?.message });
});
throw ex;
}
};
Expand Down Expand Up @@ -2514,15 +2470,9 @@ function webViewerInitialized() {

if (!PDFViewerApplication.supportsDocumentFonts) {
AppOptions.set("disableFontFace", true);
PDFViewerApplication.l10n
.get(
"web_fonts_disabled",
null,
"Web fonts are disabled: unable to use embedded PDF fonts."
)
.then(msg => {
console.warn(msg);
});
PDFViewerApplication._localizeMessage("web_fonts_disabled").then(msg => {
console.warn(msg);
});
}

if (!PDFViewerApplication.supportsPrinting) {
Expand Down Expand Up @@ -2552,11 +2502,9 @@ function webViewerInitialized() {
try {
webViewerOpenFileViaURL(file);
} catch (reason) {
PDFViewerApplication.l10n
.get("loading_error", null, "An error occurred while loading the PDF.")
.then(msg => {
PDFViewerApplication._documentError(msg, reason);
});
PDFViewerApplication._localizeMessage("loading_error").then(msg => {
PDFViewerApplication._documentError(msg, reason);
});
}
}

Expand Down Expand Up @@ -2625,15 +2573,9 @@ function webViewerPageRendered({ pageNumber, timestamp, error }) {
}

if (error) {
PDFViewerApplication.l10n
.get(
"rendering_error",
null,
"An error occurred while rendering the page."
)
.then(msg => {
PDFViewerApplication._otherError(msg, error);
});
PDFViewerApplication._localizeMessage("rendering_error").then(msg => {
PDFViewerApplication._otherError(msg, error);
});
}

PDFViewerApplication.externalServices.reportTelemetry({
Expand Down