l10n_utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.fixupLangCode = fixupLangCode;
  27. exports.getL10nFallback = getL10nFallback;
  28. exports.NullL10n = void 0;
  29. const DEFAULT_L10N_STRINGS = {
  30. of_pages: "of {{pagesCount}}",
  31. page_of_pages: "({{pageNumber}} of {{pagesCount}})",
  32. document_properties_kb: "{{size_kb}} KB ({{size_b}} bytes)",
  33. document_properties_mb: "{{size_mb}} MB ({{size_b}} bytes)",
  34. document_properties_date_string: "{{date}}, {{time}}",
  35. document_properties_page_size_unit_inches: "in",
  36. document_properties_page_size_unit_millimeters: "mm",
  37. document_properties_page_size_orientation_portrait: "portrait",
  38. document_properties_page_size_orientation_landscape: "landscape",
  39. document_properties_page_size_name_a3: "A3",
  40. document_properties_page_size_name_a4: "A4",
  41. document_properties_page_size_name_letter: "Letter",
  42. document_properties_page_size_name_legal: "Legal",
  43. document_properties_page_size_dimension_string: "{{width}} × {{height}} {{unit}} ({{orientation}})",
  44. document_properties_page_size_dimension_name_string: "{{width}} × {{height}} {{unit}} ({{name}}, {{orientation}})",
  45. document_properties_linearized_yes: "Yes",
  46. document_properties_linearized_no: "No",
  47. print_progress_percent: "{{progress}}%",
  48. "toggle_sidebar.title": "Toggle Sidebar",
  49. "toggle_sidebar_notification2.title": "Toggle Sidebar (document contains outline/attachments/layers)",
  50. additional_layers: "Additional Layers",
  51. page_landmark: "Page {{page}}",
  52. thumb_page_title: "Page {{page}}",
  53. thumb_page_canvas: "Thumbnail of Page {{page}}",
  54. find_reached_top: "Reached top of document, continued from bottom",
  55. find_reached_bottom: "Reached end of document, continued from top",
  56. "find_match_count[one]": "{{current}} of {{total}} match",
  57. "find_match_count[other]": "{{current}} of {{total}} matches",
  58. "find_match_count_limit[one]": "More than {{limit}} match",
  59. "find_match_count_limit[other]": "More than {{limit}} matches",
  60. find_not_found: "Phrase not found",
  61. error_version_info: "PDF.js v{{version}} (build: {{build}})",
  62. error_message: "Message: {{message}}",
  63. error_stack: "Stack: {{stack}}",
  64. error_file: "File: {{file}}",
  65. error_line: "Line: {{line}}",
  66. rendering_error: "An error occurred while rendering the page.",
  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. loading: "Loading…",
  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. printing_not_supported: "Warning: Printing is not fully supported by this browser.",
  78. printing_not_ready: "Warning: The PDF is not fully loaded for printing.",
  79. web_fonts_disabled: "Web fonts are disabled: unable to use embedded PDF fonts."
  80. };
  81. function getL10nFallback(key, args) {
  82. switch (key) {
  83. case "find_match_count":
  84. key = `find_match_count[${args.total === 1 ? "one" : "other"}]`;
  85. break;
  86. case "find_match_count_limit":
  87. key = `find_match_count_limit[${args.limit === 1 ? "one" : "other"}]`;
  88. break;
  89. }
  90. return DEFAULT_L10N_STRINGS[key] || "";
  91. }
  92. const PARTIAL_LANG_CODES = {
  93. en: "en-US",
  94. es: "es-ES",
  95. fy: "fy-NL",
  96. ga: "ga-IE",
  97. gu: "gu-IN",
  98. hi: "hi-IN",
  99. hy: "hy-AM",
  100. nb: "nb-NO",
  101. ne: "ne-NP",
  102. nn: "nn-NO",
  103. pa: "pa-IN",
  104. pt: "pt-PT",
  105. sv: "sv-SE",
  106. zh: "zh-CN"
  107. };
  108. function fixupLangCode(langCode) {
  109. return PARTIAL_LANG_CODES[langCode?.toLowerCase()] || langCode;
  110. }
  111. function formatL10nValue(text, args) {
  112. if (!args) {
  113. return text;
  114. }
  115. return text.replace(/\{\{\s*(\w+)\s*\}\}/g, (all, name) => {
  116. return name in args ? args[name] : "{{" + name + "}}";
  117. });
  118. }
  119. const NullL10n = {
  120. async getLanguage() {
  121. return "en-us";
  122. },
  123. async getDirection() {
  124. return "ltr";
  125. },
  126. async get(key, args = null, fallback = getL10nFallback(key, args)) {
  127. return formatL10nValue(fallback, args);
  128. },
  129. async translate(element) {}
  130. };
  131. exports.NullL10n = NullL10n;