|
| 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 }; |
0 commit comments