l10n_utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.NullL10n = void 0;
  27. exports.fixupLangCode = fixupLangCode;
  28. exports.getL10nFallback = getL10nFallback;
  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. additional_layers: "Additional Layers",
  48. page_landmark: "Page {{page}}",
  49. thumb_page_title: "Page {{page}}",
  50. thumb_page_canvas: "Thumbnail of Page {{page}}",
  51. find_reached_top: "Reached top of document, continued from bottom",
  52. find_reached_bottom: "Reached end of document, continued from top",
  53. "find_match_count[one]": "{{current}} of {{total}} match",
  54. "find_match_count[other]": "{{current}} of {{total}} matches",
  55. "find_match_count_limit[one]": "More than {{limit}} match",
  56. "find_match_count_limit[other]": "More than {{limit}} matches",
  57. find_not_found: "Phrase not found",
  58. page_scale_width: "Page Width",
  59. page_scale_fit: "Page Fit",
  60. page_scale_auto: "Automatic Zoom",
  61. page_scale_actual: "Actual Size",
  62. page_scale_percent: "{{scale}}%",
  63. loading: "Loading…",
  64. loading_error: "An error occurred while loading the PDF.",
  65. invalid_file_error: "Invalid or corrupted PDF file.",
  66. missing_file_error: "Missing PDF file.",
  67. unexpected_response_error: "Unexpected server response.",
  68. rendering_error: "An error occurred while rendering the page.",
  69. printing_not_supported: "Warning: Printing is not fully supported by this browser.",
  70. printing_not_ready: "Warning: The PDF is not fully loaded for printing.",
  71. web_fonts_disabled: "Web fonts are disabled: unable to use embedded PDF fonts.",
  72. free_text2_default_content: "Start typing…",
  73. editor_free_text2_aria_label: "Text Editor",
  74. editor_ink2_aria_label: "Draw Editor",
  75. editor_ink_canvas_aria_label: "User-created image"
  76. };
  77. {
  78. DEFAULT_L10N_STRINGS.print_progress_percent = "{{progress}}%";
  79. }
  80. function getL10nFallback(key, args) {
  81. switch (key) {
  82. case "find_match_count":
  83. key = `find_match_count[${args.total === 1 ? "one" : "other"}]`;
  84. break;
  85. case "find_match_count_limit":
  86. key = `find_match_count_limit[${args.limit === 1 ? "one" : "other"}]`;
  87. break;
  88. }
  89. return DEFAULT_L10N_STRINGS[key] || "";
  90. }
  91. const PARTIAL_LANG_CODES = {
  92. en: "en-US",
  93. es: "es-ES",
  94. fy: "fy-NL",
  95. ga: "ga-IE",
  96. gu: "gu-IN",
  97. hi: "hi-IN",
  98. hy: "hy-AM",
  99. nb: "nb-NO",
  100. ne: "ne-NP",
  101. nn: "nn-NO",
  102. pa: "pa-IN",
  103. pt: "pt-PT",
  104. sv: "sv-SE",
  105. zh: "zh-CN"
  106. };
  107. function fixupLangCode(langCode) {
  108. return PARTIAL_LANG_CODES[langCode?.toLowerCase()] || langCode;
  109. }
  110. function formatL10nValue(text, args) {
  111. if (!args) {
  112. return text;
  113. }
  114. return text.replace(/\{\{\s*(\w+)\s*\}\}/g, (all, name) => {
  115. return name in args ? args[name] : "{{" + name + "}}";
  116. });
  117. }
  118. const NullL10n = {
  119. async getLanguage() {
  120. return "en-us";
  121. },
  122. async getDirection() {
  123. return "ltr";
  124. },
  125. async get(key, args = null, fallback = getL10nFallback(key, args)) {
  126. return formatL10nValue(fallback, args);
  127. },
  128. async translate(element) {}
  129. };
  130. exports.NullL10n = NullL10n;