Skip to content

Collect all l10n fallback strings, used in the viewer, in one helper function (PR 12981 follow-up) #13050

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 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion web/annotation_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import { AnnotationLayer } from "pdfjs-lib";
import { NullL10n } from "./ui_utils.js";
import { NullL10n } from "./l10n_utils.js";
import { SimpleLinkService } from "./pdf_link_service.js";

/**
Expand Down
48 changes: 13 additions & 35 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,28 +260,6 @@ 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 @@ -741,7 +719,7 @@ const PDFViewerApplication = {
this.open(file, args);
},
onError: err => {
this._localizeMessage("loading_error").then(msg => {
this.l10n.get("loading_error").then(msg => {
this._documentError(msg, err);
});
},
Expand Down Expand Up @@ -973,7 +951,7 @@ const PDFViewerApplication = {
} else if (exception instanceof UnexpectedResponseException) {
key = "unexpected_response_error";
}
return this._localizeMessage(key).then(msg => {
return this.l10n.get(key).then(msg => {
this._documentError(msg, { message: exception?.message });
throw exception;
});
Expand Down Expand Up @@ -1128,28 +1106,28 @@ const PDFViewerApplication = {
*/
_otherError(message, moreInfo = null) {
const moreInfoText = [
this._localizeMessage("error_version_info", {
this.l10n.get("error_version_info", {
version: version || "?",
build: build || "?",
}),
];
if (moreInfo) {
moreInfoText.push(
this._localizeMessage("error_message", { message: moreInfo.message })
this.l10n.get("error_message", { message: moreInfo.message })
);
if (moreInfo.stack) {
moreInfoText.push(
this._localizeMessage("error_stack", { stack: moreInfo.stack })
this.l10n.get("error_stack", { stack: moreInfo.stack })
);
} else {
if (moreInfo.filename) {
moreInfoText.push(
this._localizeMessage("error_file", { file: moreInfo.filename })
this.l10n.get("error_file", { file: moreInfo.filename })
);
}
if (moreInfo.lineNumber) {
moreInfoText.push(
this._localizeMessage("error_line", { line: moreInfo.lineNumber })
this.l10n.get("error_line", { line: moreInfo.lineNumber })
);
}
}
Expand Down Expand Up @@ -2021,7 +1999,7 @@ const PDFViewerApplication = {
}

if (!this.supportsPrinting) {
this._localizeMessage("printing_not_supported").then(msg => {
this.l10n.get("printing_not_supported").then(msg => {
this._otherError(msg);
});
return;
Expand All @@ -2030,7 +2008,7 @@ const PDFViewerApplication = {
// 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._localizeMessage("printing_not_ready").then(msg => {
this.l10n.get("printing_not_ready").then(msg => {
// eslint-disable-next-line no-alert
window.alert(msg);
});
Expand Down Expand Up @@ -2354,7 +2332,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
throw new Error("file origin does not match viewer's");
}
} catch (ex) {
PDFViewerApplication._localizeMessage("loading_error").then(msg => {
PDFViewerApplication.l10n.get("loading_error").then(msg => {
PDFViewerApplication._documentError(msg, { message: ex?.message });
});
throw ex;
Expand Down Expand Up @@ -2465,7 +2443,7 @@ function webViewerInitialized() {

if (!PDFViewerApplication.supportsDocumentFonts) {
AppOptions.set("disableFontFace", true);
PDFViewerApplication._localizeMessage("web_fonts_disabled").then(msg => {
PDFViewerApplication.l10n.get("web_fonts_disabled").then(msg => {
console.warn(msg);
});
}
Expand Down Expand Up @@ -2497,7 +2475,7 @@ function webViewerInitialized() {
try {
webViewerOpenFileViaURL(file);
} catch (reason) {
PDFViewerApplication._localizeMessage("loading_error").then(msg => {
PDFViewerApplication.l10n.get("loading_error").then(msg => {
PDFViewerApplication._documentError(msg, reason);
});
}
Expand Down Expand Up @@ -2568,7 +2546,7 @@ function webViewerPageRendered({ pageNumber, timestamp, error }) {
}

if (error) {
PDFViewerApplication._localizeMessage("rendering_error").then(msg => {
PDFViewerApplication.l10n.get("rendering_error").then(msg => {
PDFViewerApplication._otherError(msg, error);
});
}
Expand Down
2 changes: 1 addition & 1 deletion web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
isValidSpreadMode,
MAX_AUTO_SCALE,
moveToEndOfArray,
NullL10n,
PresentationModeState,
RendererType,
SCROLLBAR_PADDING,
Expand All @@ -39,6 +38,7 @@ import {
} from "./ui_utils.js";
import { PDFRenderingQueue, RenderingStates } from "./pdf_rendering_queue.js";
import { AnnotationLayerBuilder } from "./annotation_layer_builder.js";
import { NullL10n } from "./l10n_utils.js";
import { PDFPageView } from "./pdf_page_view.js";
import { SimpleLinkService } from "./pdf_link_service.js";
import { TextLayerBuilder } from "./text_layer_builder.js";
Expand Down
5 changes: 3 additions & 2 deletions web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
import { isPdfFile, PDFDataRangeTransport, shadow } from "pdfjs-lib";
import { BasePreferences } from "./preferences.js";
import { DEFAULT_SCALE_VALUE } from "./ui_utils.js";
import { getL10nFallback } from "./l10n_utils.js";

if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
throw new Error(
Expand Down Expand Up @@ -200,8 +201,8 @@ class MozL10n {
return this.mozL10n.getDirection();
}

async get(property, args, fallback) {
return this.mozL10n.get(property, args, fallback);
async get(key, args = null, fallback = getL10nFallback(key, args)) {
return this.mozL10n.get(key, args, fallback);
}

async translate(element) {
Expand Down
5 changes: 3 additions & 2 deletions web/genericl10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import "../external/webL10n/l10n.js";
import { getL10nFallback } from "./l10n_utils.js";

const webL10n = document.webL10n;

Expand All @@ -37,9 +38,9 @@ class GenericL10n {
return l10n.getDirection();
}

async get(property, args, fallback) {
async get(key, args = null, fallback = getL10nFallback(key, args)) {
const l10n = await this._ready;
return l10n.get(property, args, fallback);
return l10n.get(key, args, fallback);
}

async translate(element) {
Expand Down
127 changes: 127 additions & 0 deletions web/l10n_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/* Copyright 2021 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* A subset of the l10n strings in the `l10n/en-US/viewer.properties` file.
*/
const DEFAULT_L10N_STRINGS = {
of_pages: "of {{pagesCount}}",
page_of_pages: "({{pageNumber}} of {{pagesCount}})",

document_properties_kb: "{{size_kb}} KB ({{size_b}} bytes)",
document_properties_mb: "{{size_mb}} MB ({{size_b}} bytes)",
document_properties_date_string: "{{date}}, {{time}}",
document_properties_page_size_unit_inches: "in",
document_properties_page_size_unit_millimeters: "mm",
document_properties_page_size_orientation_portrait: "portrait",
document_properties_page_size_orientation_landscape: "landscape",
document_properties_page_size_name_a3: "A3",
document_properties_page_size_name_a4: "A4",
document_properties_page_size_name_letter: "Letter",
document_properties_page_size_name_legal: "Legal",
document_properties_page_size_dimension_string:
"{{width}} × {{height}} {{unit}} ({{orientation}})",
document_properties_page_size_dimension_name_string:
"{{width}} × {{height}} {{unit}} ({{name}}, {{orientation}})",
document_properties_linearized_yes: "Yes",
document_properties_linearized_no: "No",

print_progress_percent: "{{progress}}%",

"toggle_sidebar.title": "Toggle Sidebar",
"toggle_sidebar_notification2.title":
"Toggle Sidebar (document contains outline/attachments/layers)",

additional_layers: "Additional Layers",
page_canvas: "Page {{page}}",
thumb_page_title: "Page {{page}}",
thumb_page_canvas: "Thumbnail of Page {{page}}",

find_reached_top: "Reached top of document, continued from bottom",
find_reached_bottom: "Reached end of document, continued from top",
"find_match_count[one]": "{{current}} of {{total}} match",
"find_match_count[other]": "{{current}} of {{total}} matches",
"find_match_count_limit[one]": "More than {{limit}} match",
"find_match_count_limit[other]": "More than {{limit}} matches",
find_not_found: "Phrase not found",

error_version_info: "PDF.js v{{version}} (build: {{build}})",
error_message: "Message: {{message}}",
error_stack: "Stack: {{stack}}",
error_file: "File: {{file}}",
error_line: "Line: {{line}}",
rendering_error: "An error occurred while rendering the page.",

page_scale_width: "Page Width",
page_scale_fit: "Page Fit",
page_scale_auto: "Automatic Zoom",
page_scale_actual: "Actual Size",
page_scale_percent: "{{scale}}%",

loading_error: "An error occurred while loading the PDF.",
invalid_file_error: "Invalid or corrupted PDF file.",
missing_file_error: "Missing PDF file.",
unexpected_response_error: "Unexpected server response.",

printing_not_supported:
"Warning: Printing is not fully supported by this browser.",
printing_not_ready: "Warning: The PDF is not fully loaded for printing.",
web_fonts_disabled:
"Web fonts are disabled: unable to use embedded PDF fonts.",
};

function getL10nFallback(key, args) {
switch (key) {
case "find_match_count":
key = `find_match_count[${args.total === 1 ? "one" : "other"}]`;
break;
case "find_match_count_limit":
key = `find_match_count_limit[${args.limit === 1 ? "one" : "other"}]`;
break;
}
return DEFAULT_L10N_STRINGS[key] || "";
}

// Replaces {{arguments}} with their values.
function formatL10nValue(text, args) {
if (!args) {
return text;
}
return text.replace(/\{\{\s*(\w+)\s*\}\}/g, (all, name) => {
return name in args ? args[name] : "{{" + name + "}}";
});
}

/**
* No-op implementation of the localization service.
* @implements {IL10n}
*/
const NullL10n = {
async getLanguage() {
return "en-us";
},

async getDirection() {
return "ltr";
},

async get(key, args = null, fallback = getL10nFallback(key, args)) {
return formatL10nValue(fallback, args);
},

async translate(element) {},
};

export { getL10nFallback, NullL10n };
36 changes: 10 additions & 26 deletions web/password_prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,18 @@ class PasswordPrompt {
);
}

open() {
this.overlayManager.open(this.overlayName).then(() => {
if (
!this._isViewerEmbedded ||
this.reason === PasswordResponses.INCORRECT_PASSWORD
) {
this.input.focus();
}
async open() {
await this.overlayManager.open(this.overlayName);

let promptString;
if (this.reason === PasswordResponses.INCORRECT_PASSWORD) {
promptString = this.l10n.get(
"password_invalid",
null,
"Invalid password. Please try again."
);
} else {
promptString = this.l10n.get(
"password_label",
null,
"Enter the password to open this PDF file."
);
}
const passwordIncorrect =
this.reason === PasswordResponses.INCORRECT_PASSWORD;

promptString.then(msg => {
this.label.textContent = msg;
});
});
if (!this._isViewerEmbedded || passwordIncorrect) {
this.input.focus();
}
this.label.textContent = await this.l10n.get(
`password_${passwordIncorrect ? "invalid" : "label"}`
);
}

close() {
Expand Down
Loading