Skip to content

Commit 4b49db7

Browse files
Merge pull request #13050 from Snuffleupagus/l10n-fallback
Collect all l10n fallback strings, used in the viewer, in one helper function (PR 12981 follow-up)
2 parents 8d203b3 + 038668b commit 4b49db7

17 files changed

+238
-272
lines changed

web/annotation_layer_builder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
import { AnnotationLayer } from "pdfjs-lib";
17-
import { NullL10n } from "./ui_utils.js";
17+
import { NullL10n } from "./l10n_utils.js";
1818
import { SimpleLinkService } from "./pdf_link_service.js";
1919

2020
/**

web/app.js

+13-35
Original file line numberDiff line numberDiff line change
@@ -260,28 +260,6 @@ const PDFViewerApplication = {
260260
_scriptingInstance: null,
261261
_mouseState: Object.create(null),
262262

263-
_localizeMessage(key, args = null) {
264-
const DEFAULT_L10N_STRINGS = {
265-
error_file: "File: {{file}}",
266-
error_line: "Line: {{line}}",
267-
error_message: "Message: {{message}}",
268-
error_stack: "Stack: {{stack}}",
269-
error_version_info: "PDF.js v{{version}} (build: {{build}})",
270-
invalid_file_error: "Invalid or corrupted PDF file.",
271-
loading_error: "An error occurred while loading the PDF.",
272-
missing_file_error: "Missing PDF file.",
273-
printing_not_ready: "Warning: The PDF is not fully loaded for printing.",
274-
printing_not_supported:
275-
"Warning: Printing is not fully supported by this browser.",
276-
rendering_error: "An error occurred while rendering the page.",
277-
unexpected_response_error: "Unexpected server response.",
278-
web_fonts_disabled:
279-
"Web fonts are disabled: unable to use embedded PDF fonts.",
280-
};
281-
282-
return this.l10n.get(key || "", args, DEFAULT_L10N_STRINGS[key]);
283-
},
284-
285263
// Called once when the document is loaded.
286264
async initialize(appConfig) {
287265
this.preferences = this.externalServices.createPreferences();
@@ -741,7 +719,7 @@ const PDFViewerApplication = {
741719
this.open(file, args);
742720
},
743721
onError: err => {
744-
this._localizeMessage("loading_error").then(msg => {
722+
this.l10n.get("loading_error").then(msg => {
745723
this._documentError(msg, err);
746724
});
747725
},
@@ -973,7 +951,7 @@ const PDFViewerApplication = {
973951
} else if (exception instanceof UnexpectedResponseException) {
974952
key = "unexpected_response_error";
975953
}
976-
return this._localizeMessage(key).then(msg => {
954+
return this.l10n.get(key).then(msg => {
977955
this._documentError(msg, { message: exception?.message });
978956
throw exception;
979957
});
@@ -1128,28 +1106,28 @@ const PDFViewerApplication = {
11281106
*/
11291107
_otherError(message, moreInfo = null) {
11301108
const moreInfoText = [
1131-
this._localizeMessage("error_version_info", {
1109+
this.l10n.get("error_version_info", {
11321110
version: version || "?",
11331111
build: build || "?",
11341112
}),
11351113
];
11361114
if (moreInfo) {
11371115
moreInfoText.push(
1138-
this._localizeMessage("error_message", { message: moreInfo.message })
1116+
this.l10n.get("error_message", { message: moreInfo.message })
11391117
);
11401118
if (moreInfo.stack) {
11411119
moreInfoText.push(
1142-
this._localizeMessage("error_stack", { stack: moreInfo.stack })
1120+
this.l10n.get("error_stack", { stack: moreInfo.stack })
11431121
);
11441122
} else {
11451123
if (moreInfo.filename) {
11461124
moreInfoText.push(
1147-
this._localizeMessage("error_file", { file: moreInfo.filename })
1125+
this.l10n.get("error_file", { file: moreInfo.filename })
11481126
);
11491127
}
11501128
if (moreInfo.lineNumber) {
11511129
moreInfoText.push(
1152-
this._localizeMessage("error_line", { line: moreInfo.lineNumber })
1130+
this.l10n.get("error_line", { line: moreInfo.lineNumber })
11531131
);
11541132
}
11551133
}
@@ -2021,7 +1999,7 @@ const PDFViewerApplication = {
20211999
}
20222000

20232001
if (!this.supportsPrinting) {
2024-
this._localizeMessage("printing_not_supported").then(msg => {
2002+
this.l10n.get("printing_not_supported").then(msg => {
20252003
this._otherError(msg);
20262004
});
20272005
return;
@@ -2030,7 +2008,7 @@ const PDFViewerApplication = {
20302008
// The beforePrint is a sync method and we need to know layout before
20312009
// returning from this method. Ensure that we can get sizes of the pages.
20322010
if (!this.pdfViewer.pageViewsReady) {
2033-
this._localizeMessage("printing_not_ready").then(msg => {
2011+
this.l10n.get("printing_not_ready").then(msg => {
20342012
// eslint-disable-next-line no-alert
20352013
window.alert(msg);
20362014
});
@@ -2354,7 +2332,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
23542332
throw new Error("file origin does not match viewer's");
23552333
}
23562334
} catch (ex) {
2357-
PDFViewerApplication._localizeMessage("loading_error").then(msg => {
2335+
PDFViewerApplication.l10n.get("loading_error").then(msg => {
23582336
PDFViewerApplication._documentError(msg, { message: ex?.message });
23592337
});
23602338
throw ex;
@@ -2465,7 +2443,7 @@ function webViewerInitialized() {
24652443

24662444
if (!PDFViewerApplication.supportsDocumentFonts) {
24672445
AppOptions.set("disableFontFace", true);
2468-
PDFViewerApplication._localizeMessage("web_fonts_disabled").then(msg => {
2446+
PDFViewerApplication.l10n.get("web_fonts_disabled").then(msg => {
24692447
console.warn(msg);
24702448
});
24712449
}
@@ -2497,7 +2475,7 @@ function webViewerInitialized() {
24972475
try {
24982476
webViewerOpenFileViaURL(file);
24992477
} catch (reason) {
2500-
PDFViewerApplication._localizeMessage("loading_error").then(msg => {
2478+
PDFViewerApplication.l10n.get("loading_error").then(msg => {
25012479
PDFViewerApplication._documentError(msg, reason);
25022480
});
25032481
}
@@ -2568,7 +2546,7 @@ function webViewerPageRendered({ pageNumber, timestamp, error }) {
25682546
}
25692547

25702548
if (error) {
2571-
PDFViewerApplication._localizeMessage("rendering_error").then(msg => {
2549+
PDFViewerApplication.l10n.get("rendering_error").then(msg => {
25722550
PDFViewerApplication._otherError(msg, error);
25732551
});
25742552
}

web/base_viewer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
isValidSpreadMode,
2626
MAX_AUTO_SCALE,
2727
moveToEndOfArray,
28-
NullL10n,
2928
PresentationModeState,
3029
RendererType,
3130
SCROLLBAR_PADDING,
@@ -39,6 +38,7 @@ import {
3938
} from "./ui_utils.js";
4039
import { PDFRenderingQueue, RenderingStates } from "./pdf_rendering_queue.js";
4140
import { AnnotationLayerBuilder } from "./annotation_layer_builder.js";
41+
import { NullL10n } from "./l10n_utils.js";
4242
import { PDFPageView } from "./pdf_page_view.js";
4343
import { SimpleLinkService } from "./pdf_link_service.js";
4444
import { TextLayerBuilder } from "./text_layer_builder.js";

web/firefoxcom.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
1818
import { isPdfFile, PDFDataRangeTransport, shadow } from "pdfjs-lib";
1919
import { BasePreferences } from "./preferences.js";
2020
import { DEFAULT_SCALE_VALUE } from "./ui_utils.js";
21+
import { getL10nFallback } from "./l10n_utils.js";
2122

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

203-
async get(property, args, fallback) {
204-
return this.mozL10n.get(property, args, fallback);
204+
async get(key, args = null, fallback = getL10nFallback(key, args)) {
205+
return this.mozL10n.get(key, args, fallback);
205206
}
206207

207208
async translate(element) {

web/genericl10n.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
import "../external/webL10n/l10n.js";
17+
import { getL10nFallback } from "./l10n_utils.js";
1718

1819
const webL10n = document.webL10n;
1920

@@ -37,9 +38,9 @@ class GenericL10n {
3738
return l10n.getDirection();
3839
}
3940

40-
async get(property, args, fallback) {
41+
async get(key, args = null, fallback = getL10nFallback(key, args)) {
4142
const l10n = await this._ready;
42-
return l10n.get(property, args, fallback);
43+
return l10n.get(key, args, fallback);
4344
}
4445

4546
async translate(element) {

web/l10n_utils.js

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/* Copyright 2021 Mozilla Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/**
17+
* A subset of the l10n strings in the `l10n/en-US/viewer.properties` file.
18+
*/
19+
const DEFAULT_L10N_STRINGS = {
20+
of_pages: "of {{pagesCount}}",
21+
page_of_pages: "({{pageNumber}} of {{pagesCount}})",
22+
23+
document_properties_kb: "{{size_kb}} KB ({{size_b}} bytes)",
24+
document_properties_mb: "{{size_mb}} MB ({{size_b}} bytes)",
25+
document_properties_date_string: "{{date}}, {{time}}",
26+
document_properties_page_size_unit_inches: "in",
27+
document_properties_page_size_unit_millimeters: "mm",
28+
document_properties_page_size_orientation_portrait: "portrait",
29+
document_properties_page_size_orientation_landscape: "landscape",
30+
document_properties_page_size_name_a3: "A3",
31+
document_properties_page_size_name_a4: "A4",
32+
document_properties_page_size_name_letter: "Letter",
33+
document_properties_page_size_name_legal: "Legal",
34+
document_properties_page_size_dimension_string:
35+
"{{width}} × {{height}} {{unit}} ({{orientation}})",
36+
document_properties_page_size_dimension_name_string:
37+
"{{width}} × {{height}} {{unit}} ({{name}}, {{orientation}})",
38+
document_properties_linearized_yes: "Yes",
39+
document_properties_linearized_no: "No",
40+
41+
print_progress_percent: "{{progress}}%",
42+
43+
"toggle_sidebar.title": "Toggle Sidebar",
44+
"toggle_sidebar_notification2.title":
45+
"Toggle Sidebar (document contains outline/attachments/layers)",
46+
47+
additional_layers: "Additional Layers",
48+
page_canvas: "Page {{page}}",
49+
thumb_page_title: "Page {{page}}",
50+
thumb_page_canvas: "Thumbnail of Page {{page}}",
51+
52+
find_reached_top: "Reached top of document, continued from bottom",
53+
find_reached_bottom: "Reached end of document, continued from top",
54+
"find_match_count[one]": "{{current}} of {{total}} match",
55+
"find_match_count[other]": "{{current}} of {{total}} matches",
56+
"find_match_count_limit[one]": "More than {{limit}} match",
57+
"find_match_count_limit[other]": "More than {{limit}} matches",
58+
find_not_found: "Phrase not found",
59+
60+
error_version_info: "PDF.js v{{version}} (build: {{build}})",
61+
error_message: "Message: {{message}}",
62+
error_stack: "Stack: {{stack}}",
63+
error_file: "File: {{file}}",
64+
error_line: "Line: {{line}}",
65+
rendering_error: "An error occurred while rendering the page.",
66+
67+
page_scale_width: "Page Width",
68+
page_scale_fit: "Page Fit",
69+
page_scale_auto: "Automatic Zoom",
70+
page_scale_actual: "Actual Size",
71+
page_scale_percent: "{{scale}}%",
72+
73+
loading_error: "An error occurred while loading the PDF.",
74+
invalid_file_error: "Invalid or corrupted PDF file.",
75+
missing_file_error: "Missing PDF file.",
76+
unexpected_response_error: "Unexpected server response.",
77+
78+
printing_not_supported:
79+
"Warning: Printing is not fully supported by this browser.",
80+
printing_not_ready: "Warning: The PDF is not fully loaded for printing.",
81+
web_fonts_disabled:
82+
"Web fonts are disabled: unable to use embedded PDF fonts.",
83+
};
84+
85+
function getL10nFallback(key, args) {
86+
switch (key) {
87+
case "find_match_count":
88+
key = `find_match_count[${args.total === 1 ? "one" : "other"}]`;
89+
break;
90+
case "find_match_count_limit":
91+
key = `find_match_count_limit[${args.limit === 1 ? "one" : "other"}]`;
92+
break;
93+
}
94+
return DEFAULT_L10N_STRINGS[key] || "";
95+
}
96+
97+
// Replaces {{arguments}} with their values.
98+
function formatL10nValue(text, args) {
99+
if (!args) {
100+
return text;
101+
}
102+
return text.replace(/\{\{\s*(\w+)\s*\}\}/g, (all, name) => {
103+
return name in args ? args[name] : "{{" + name + "}}";
104+
});
105+
}
106+
107+
/**
108+
* No-op implementation of the localization service.
109+
* @implements {IL10n}
110+
*/
111+
const NullL10n = {
112+
async getLanguage() {
113+
return "en-us";
114+
},
115+
116+
async getDirection() {
117+
return "ltr";
118+
},
119+
120+
async get(key, args = null, fallback = getL10nFallback(key, args)) {
121+
return formatL10nValue(fallback, args);
122+
},
123+
124+
async translate(element) {},
125+
};
126+
127+
export { getL10nFallback, NullL10n };

web/password_prompt.js

+10-26
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,18 @@ class PasswordPrompt {
6767
);
6868
}
6969

70-
open() {
71-
this.overlayManager.open(this.overlayName).then(() => {
72-
if (
73-
!this._isViewerEmbedded ||
74-
this.reason === PasswordResponses.INCORRECT_PASSWORD
75-
) {
76-
this.input.focus();
77-
}
70+
async open() {
71+
await this.overlayManager.open(this.overlayName);
7872

79-
let promptString;
80-
if (this.reason === PasswordResponses.INCORRECT_PASSWORD) {
81-
promptString = this.l10n.get(
82-
"password_invalid",
83-
null,
84-
"Invalid password. Please try again."
85-
);
86-
} else {
87-
promptString = this.l10n.get(
88-
"password_label",
89-
null,
90-
"Enter the password to open this PDF file."
91-
);
92-
}
73+
const passwordIncorrect =
74+
this.reason === PasswordResponses.INCORRECT_PASSWORD;
9375

94-
promptString.then(msg => {
95-
this.label.textContent = msg;
96-
});
97-
});
76+
if (!this._isViewerEmbedded || passwordIncorrect) {
77+
this.input.focus();
78+
}
79+
this.label.textContent = await this.l10n.get(
80+
`password_${passwordIncorrect ? "invalid" : "label"}`
81+
);
9882
}
9983

10084
close() {

0 commit comments

Comments
 (0)