locales.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright 2012 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. "use strict";
  16. const fs = require("fs");
  17. const https = require("https");
  18. const path = require("path");
  19. // Fetches all languages that have an *active* translation in mozilla-central.
  20. // This is used in gulpfile.js for the `importl10n` command.
  21. const DEFAULT_LOCALE = "en-US";
  22. const EXCLUDE_LANG_CODES = ["ca-valencia", "ja-JP-mac"];
  23. function normalizeText(s) {
  24. return s.replace(/\r\n?/g, "\n").replace(/\uFEFF/g, "");
  25. }
  26. function downloadLanguageCodes() {
  27. console.log("Downloading language codes...\n");
  28. const ALL_LOCALES =
  29. "https://hg.mozilla.org/mozilla-central/raw-file/tip/browser/locales/all-locales";
  30. return new Promise(function (resolve) {
  31. https.get(ALL_LOCALES, function (response) {
  32. if (response.statusCode === 200) {
  33. let content = "";
  34. response.setEncoding("utf8");
  35. response.on("data", function (chunk) {
  36. content += chunk;
  37. });
  38. response.on("end", function () {
  39. content = content.trim(); // Remove any leading/trailing white-space.
  40. const langCodes = normalizeText(content).split("\n");
  41. // Remove all locales that we don't want to download below.
  42. for (const langCode of [DEFAULT_LOCALE, ...EXCLUDE_LANG_CODES]) {
  43. const i = langCodes.indexOf(langCode);
  44. if (i > -1) {
  45. langCodes.splice(i, 1);
  46. }
  47. }
  48. resolve(langCodes);
  49. });
  50. } else {
  51. resolve([]);
  52. }
  53. });
  54. });
  55. }
  56. function downloadLanguageFiles(root, langCode) {
  57. console.log("Downloading " + langCode + "...");
  58. // Constants for constructing the URLs. Translations are taken from the
  59. // Nightly channel as those are the most recent ones.
  60. const MOZ_CENTRAL_ROOT = "https://hg.mozilla.org/l10n-central/";
  61. const MOZ_CENTRAL_PDFJS_DIR = "/raw-file/default/browser/pdfviewer/";
  62. // Defines which files to download for each language.
  63. const files = ["viewer.properties"];
  64. let downloadsLeft = files.length;
  65. const outputDir = path.join(root, langCode);
  66. if (!fs.existsSync(outputDir)) {
  67. fs.mkdirSync(outputDir);
  68. }
  69. return new Promise(function (resolve) {
  70. // Download the necessary files for this language.
  71. files.forEach(function (fileName) {
  72. const outputPath = path.join(outputDir, fileName);
  73. const url =
  74. MOZ_CENTRAL_ROOT + langCode + MOZ_CENTRAL_PDFJS_DIR + fileName;
  75. https.get(url, function (response) {
  76. // Not all files exist for each language. Files without translations
  77. // have been removed (https://bugzilla.mozilla.org/show_bug.cgi?id=1443175).
  78. if (response.statusCode === 200) {
  79. let content = "";
  80. response.setEncoding("utf8");
  81. response.on("data", function (chunk) {
  82. content += chunk;
  83. });
  84. response.on("end", function () {
  85. fs.writeFileSync(outputPath, normalizeText(content), "utf8");
  86. if (--downloadsLeft === 0) {
  87. resolve();
  88. }
  89. });
  90. } else {
  91. if (--downloadsLeft === 0) {
  92. resolve();
  93. }
  94. }
  95. });
  96. });
  97. });
  98. }
  99. async function downloadL10n(root, callback) {
  100. const langCodes = await downloadLanguageCodes();
  101. for (const langCode of langCodes) {
  102. if (!langCode) {
  103. continue;
  104. }
  105. await downloadLanguageFiles(root, langCode);
  106. }
  107. const removeCodes = [];
  108. for (const entry of fs.readdirSync(root)) {
  109. const dirPath = path.join(root, entry),
  110. stat = fs.lstatSync(dirPath);
  111. if (
  112. stat.isDirectory() &&
  113. entry !== DEFAULT_LOCALE &&
  114. !langCodes.includes(entry)
  115. ) {
  116. removeCodes.push(entry);
  117. }
  118. }
  119. if (removeCodes.length) {
  120. console.log(
  121. "\nConsider removing the following unmaintained locales:\n" +
  122. removeCodes.join(", ") +
  123. "\n"
  124. );
  125. }
  126. if (callback) {
  127. callback();
  128. }
  129. }
  130. exports.downloadL10n = downloadL10n;