l10n_utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. * A subset of the l10n strings in the `l10n/en-US/viewer.properties` file.
  17. */
  18. const DEFAULT_L10N_STRINGS = {
  19. of_pages: "of {{pagesCount}}",
  20. page_of_pages: "({{pageNumber}} of {{pagesCount}})",
  21. document_properties_kb: "{{size_kb}} KB ({{size_b}} bytes)",
  22. document_properties_mb: "{{size_mb}} MB ({{size_b}} bytes)",
  23. document_properties_date_string: "{{date}}, {{time}}",
  24. document_properties_page_size_unit_inches: "in",
  25. document_properties_page_size_unit_millimeters: "mm",
  26. document_properties_page_size_orientation_portrait: "portrait",
  27. document_properties_page_size_orientation_landscape: "landscape",
  28. document_properties_page_size_name_a3: "A3",
  29. document_properties_page_size_name_a4: "A4",
  30. document_properties_page_size_name_letter: "Letter",
  31. document_properties_page_size_name_legal: "Legal",
  32. document_properties_page_size_dimension_string:
  33. "{{width}} × {{height}} {{unit}} ({{orientation}})",
  34. document_properties_page_size_dimension_name_string:
  35. "{{width}} × {{height}} {{unit}} ({{name}}, {{orientation}})",
  36. document_properties_linearized_yes: "Yes",
  37. document_properties_linearized_no: "No",
  38. additional_layers: "Additional Layers",
  39. page_landmark: "Page {{page}}",
  40. thumb_page_title: "Page {{page}}",
  41. thumb_page_canvas: "Thumbnail of Page {{page}}",
  42. find_reached_top: "Reached top of document, continued from bottom",
  43. find_reached_bottom: "Reached end of document, continued from top",
  44. "find_match_count[one]": "{{current}} of {{total}} match",
  45. "find_match_count[other]": "{{current}} of {{total}} matches",
  46. "find_match_count_limit[one]": "More than {{limit}} match",
  47. "find_match_count_limit[other]": "More than {{limit}} matches",
  48. find_not_found: "Phrase not found",
  49. page_scale_width: "Page Width",
  50. page_scale_fit: "Page Fit",
  51. page_scale_auto: "Automatic Zoom",
  52. page_scale_actual: "Actual Size",
  53. page_scale_percent: "{{scale}}%",
  54. loading: "Loading…",
  55. loading_error: "An error occurred while loading the PDF.",
  56. invalid_file_error: "Invalid or corrupted PDF file.",
  57. missing_file_error: "Missing PDF file.",
  58. unexpected_response_error: "Unexpected server response.",
  59. rendering_error: "An error occurred while rendering the page.",
  60. printing_not_supported:
  61. "Warning: Printing is not fully supported by this browser.",
  62. printing_not_ready: "Warning: The PDF is not fully loaded for printing.",
  63. web_fonts_disabled:
  64. "Web fonts are disabled: unable to use embedded PDF fonts.",
  65. free_text2_default_content: "Start typing…",
  66. editor_free_text2_aria_label: "Text Editor",
  67. editor_ink2_aria_label: "Draw Editor",
  68. editor_ink_canvas_aria_label: "User-created image",
  69. };
  70. if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
  71. DEFAULT_L10N_STRINGS.print_progress_percent = "{{progress}}%";
  72. }
  73. function getL10nFallback(key, args) {
  74. switch (key) {
  75. case "find_match_count":
  76. key = `find_match_count[${args.total === 1 ? "one" : "other"}]`;
  77. break;
  78. case "find_match_count_limit":
  79. key = `find_match_count_limit[${args.limit === 1 ? "one" : "other"}]`;
  80. break;
  81. }
  82. return DEFAULT_L10N_STRINGS[key] || "";
  83. }
  84. const PARTIAL_LANG_CODES = {
  85. en: "en-US",
  86. es: "es-ES",
  87. fy: "fy-NL",
  88. ga: "ga-IE",
  89. gu: "gu-IN",
  90. hi: "hi-IN",
  91. hy: "hy-AM",
  92. nb: "nb-NO",
  93. ne: "ne-NP",
  94. nn: "nn-NO",
  95. pa: "pa-IN",
  96. pt: "pt-PT",
  97. sv: "sv-SE",
  98. zh: "zh-CN",
  99. };
  100. // Try to support "incompletely" specified language codes (see issue 13689).
  101. function fixupLangCode(langCode) {
  102. return PARTIAL_LANG_CODES[langCode?.toLowerCase()] || langCode;
  103. }
  104. // Replaces {{arguments}} with their values.
  105. function formatL10nValue(text, args) {
  106. if (!args) {
  107. return text;
  108. }
  109. return text.replace(/\{\{\s*(\w+)\s*\}\}/g, (all, name) => {
  110. return name in args ? args[name] : "{{" + name + "}}";
  111. });
  112. }
  113. /**
  114. * No-op implementation of the localization service.
  115. * @implements {IL10n}
  116. */
  117. const NullL10n = {
  118. async getLanguage() {
  119. return "en-us";
  120. },
  121. async getDirection() {
  122. return "ltr";
  123. },
  124. async get(key, args = null, fallback = getL10nFallback(key, args)) {
  125. return formatL10nValue(fallback, args);
  126. },
  127. async translate(element) {},
  128. };
  129. export { fixupLangCode, getL10nFallback, NullL10n };