Merge pull request #12981 from Snuffleupagus/app-localizeMessage

Collect the l10n error/warning message lookup, in `web/app.js`, in a new helper method
This commit is contained in:
Tim van der Meij 2021-02-12 00:14:05 +01:00 committed by GitHub
commit d5cad9ad3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -259,6 +259,28 @@ const PDFViewerApplication = {
_scriptingInstance: null, _scriptingInstance: null,
_mouseState: Object.create(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. // Called once when the document is loaded.
async initialize(appConfig) { async initialize(appConfig) {
this.preferences = this.externalServices.createPreferences(); this.preferences = this.externalServices.createPreferences();
@ -702,36 +724,25 @@ const PDFViewerApplication = {
throw new Error("Not implemented: initPassiveLoading"); throw new Error("Not implemented: initPassiveLoading");
} }
this.externalServices.initPassiveLoading({ this.externalServices.initPassiveLoading({
onOpenWithTransport(url, length, transport) { onOpenWithTransport: (url, length, transport) => {
PDFViewerApplication.open(url, { length, range: transport }); this.open(url, { length, range: transport });
}, },
onOpenWithData(data) { onOpenWithData: data => {
PDFViewerApplication.open(data); this.open(data);
}, },
onOpenWithURL(url, length, originalUrl) { onOpenWithURL: (url, length, originalUrl) => {
let file = url, const file = originalUrl !== undefined ? { url, originalUrl } : url;
args = null; const args = length !== undefined ? { length } : null;
if (length !== undefined) {
args = { length }; this.open(file, args);
}
if (originalUrl !== undefined) {
file = { url, originalUrl };
}
PDFViewerApplication.open(file, args);
}, },
onError(err) { onError: err => {
PDFViewerApplication.l10n this._localizeMessage("loading_error").then(msg => {
.get( this._documentError(msg, err);
"loading_error", });
null,
"An error occurred while loading the PDF."
)
.then(msg => {
PDFViewerApplication._documentError(msg, err);
});
}, },
onProgress(loaded, total) { onProgress: (loaded, total) => {
PDFViewerApplication.progress(loaded / total); this.progress(loaded / total);
}, },
}); });
}, },
@ -948,38 +959,16 @@ const PDFViewerApplication = {
return undefined; // Ignore errors for previously opened PDF files. return undefined; // Ignore errors for previously opened PDF files.
} }
const message = exception?.message; let key = "loading_error";
let loadingErrorMessage;
if (exception instanceof InvalidPDFException) { if (exception instanceof InvalidPDFException) {
// change error message also for other builds key = "invalid_file_error";
loadingErrorMessage = this.l10n.get(
"invalid_file_error",
null,
"Invalid or corrupted PDF file."
);
} else if (exception instanceof MissingPDFException) { } else if (exception instanceof MissingPDFException) {
// special message for missing PDF's key = "missing_file_error";
loadingErrorMessage = this.l10n.get(
"missing_file_error",
null,
"Missing PDF file."
);
} else if (exception instanceof UnexpectedResponseException) { } else if (exception instanceof UnexpectedResponseException) {
loadingErrorMessage = this.l10n.get( key = "unexpected_response_error";
"unexpected_response_error",
null,
"Unexpected server response."
);
} else {
loadingErrorMessage = this.l10n.get(
"loading_error",
null,
"An error occurred while loading the PDF."
);
} }
return this._localizeMessage(key).then(msg => {
return loadingErrorMessage.then(msg => { this._documentError(msg, { message: exception?.message });
this._documentError(msg, { message });
throw exception; throw exception;
}); });
} }
@ -1132,45 +1121,28 @@ const PDFViewerApplication = {
*/ */
_otherError(message, moreInfo = null) { _otherError(message, moreInfo = null) {
const moreInfoText = [ const moreInfoText = [
this.l10n.get( this._localizeMessage("error_version_info", {
"error_version_info", version: version || "?",
{ version: version || "?", build: build || "?" }, build: build || "?",
"PDF.js v{{version}} (build: {{build}})" }),
),
]; ];
if (moreInfo) { if (moreInfo) {
moreInfoText.push( moreInfoText.push(
this.l10n.get( this._localizeMessage("error_message", { message: moreInfo.message })
"error_message",
{ message: moreInfo.message },
"Message: {{message}}"
)
); );
if (moreInfo.stack) { if (moreInfo.stack) {
moreInfoText.push( moreInfoText.push(
this.l10n.get( this._localizeMessage("error_stack", { stack: moreInfo.stack })
"error_stack",
{ stack: moreInfo.stack },
"Stack: {{stack}}"
)
); );
} else { } else {
if (moreInfo.filename) { if (moreInfo.filename) {
moreInfoText.push( moreInfoText.push(
this.l10n.get( this._localizeMessage("error_file", { file: moreInfo.filename })
"error_file",
{ file: moreInfo.filename },
"File: {{file}}"
)
); );
} }
if (moreInfo.lineNumber) { if (moreInfo.lineNumber) {
moreInfoText.push( moreInfoText.push(
this.l10n.get( this._localizeMessage("error_line", { line: moreInfo.lineNumber })
"error_line",
{ line: moreInfo.lineNumber },
"Line: {{line}}"
)
); );
} }
} }
@ -2054,31 +2026,19 @@ const PDFViewerApplication = {
} }
if (!this.supportsPrinting) { if (!this.supportsPrinting) {
this.l10n this._localizeMessage("printing_not_supported").then(msg => {
.get( this._otherError(msg);
"printing_not_supported", });
null,
"Warning: Printing is not fully supported by this browser."
)
.then(printMessage => {
this._otherError(printMessage);
});
return; return;
} }
// The beforePrint is a sync method and we need to know layout before // 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. // returning from this method. Ensure that we can get sizes of the pages.
if (!this.pdfViewer.pageViewsReady) { if (!this.pdfViewer.pageViewsReady) {
this.l10n this._localizeMessage("printing_not_ready").then(msg => {
.get( // eslint-disable-next-line no-alert
"printing_not_ready", window.alert(msg);
null, });
"Warning: The PDF is not fully loaded for printing."
)
.then(notReadyMessage => {
// eslint-disable-next-line no-alert
window.alert(notReadyMessage);
});
return; return;
} }
@ -2399,13 +2359,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
throw new Error("file origin does not match viewer's"); throw new Error("file origin does not match viewer's");
} }
} catch (ex) { } catch (ex) {
PDFViewerApplication.l10n PDFViewerApplication._localizeMessage("loading_error").then(msg => {
.get("loading_error", null, "An error occurred while loading the PDF.") PDFViewerApplication._documentError(msg, { message: ex?.message });
.then(loadingErrorMessage => { });
PDFViewerApplication._documentError(loadingErrorMessage, {
message: ex?.message,
});
});
throw ex; throw ex;
} }
}; };
@ -2514,15 +2470,9 @@ function webViewerInitialized() {
if (!PDFViewerApplication.supportsDocumentFonts) { if (!PDFViewerApplication.supportsDocumentFonts) {
AppOptions.set("disableFontFace", true); AppOptions.set("disableFontFace", true);
PDFViewerApplication.l10n PDFViewerApplication._localizeMessage("web_fonts_disabled").then(msg => {
.get( console.warn(msg);
"web_fonts_disabled", });
null,
"Web fonts are disabled: unable to use embedded PDF fonts."
)
.then(msg => {
console.warn(msg);
});
} }
if (!PDFViewerApplication.supportsPrinting) { if (!PDFViewerApplication.supportsPrinting) {
@ -2552,11 +2502,9 @@ function webViewerInitialized() {
try { try {
webViewerOpenFileViaURL(file); webViewerOpenFileViaURL(file);
} catch (reason) { } catch (reason) {
PDFViewerApplication.l10n PDFViewerApplication._localizeMessage("loading_error").then(msg => {
.get("loading_error", null, "An error occurred while loading the PDF.") PDFViewerApplication._documentError(msg, reason);
.then(msg => { });
PDFViewerApplication._documentError(msg, reason);
});
} }
} }
@ -2625,15 +2573,9 @@ function webViewerPageRendered({ pageNumber, timestamp, error }) {
} }
if (error) { if (error) {
PDFViewerApplication.l10n PDFViewerApplication._localizeMessage("rendering_error").then(msg => {
.get( PDFViewerApplication._otherError(msg, error);
"rendering_error", });
null,
"An error occurred while rendering the page."
)
.then(msg => {
PDFViewerApplication._otherError(msg, error);
});
} }
PDFViewerApplication.externalServices.reportTelemetry({ PDFViewerApplication.externalServices.reportTelemetry({