strings-import.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #! /usr/bin/env node
  2. "use strict";
  3. /* eslint-disable no-console */
  4. const fetch = require("node-fetch");
  5. /* globals cd, ls, mkdir, rm, ShellString */
  6. require("shelljs/global");
  7. const {CENTRAL_LOCALES, DEFAULT_LOCALE, LOCALES_SOURCE_DIRECTORY} = require("./locales");
  8. const L10N_CENTRAL = "https://hg.mozilla.org/l10n-central";
  9. const PROPERTIES_PATH = "raw-file/default/browser/chrome/browser/activity-stream/newtab.properties";
  10. const STRINGS_FILE = "strings.properties";
  11. // Get all the locales in l10n-central
  12. async function getLocales() {
  13. console.log(`Getting locales from ${L10N_CENTRAL}`);
  14. // Add sub repository locales that mozilla-central builds
  15. const locales = [];
  16. const unbuilt = [];
  17. const subrepos = await (await fetch(`${L10N_CENTRAL}?style=json`)).json();
  18. subrepos.entries.forEach(({name}) => {
  19. if (CENTRAL_LOCALES.includes(name)) {
  20. locales.push(name);
  21. } else {
  22. unbuilt.push(name);
  23. }
  24. });
  25. console.log(`Got ${locales.length} mozilla-central locales: ${locales}`);
  26. console.log(`Skipped ${unbuilt.length} unbuilt locales: ${unbuilt}`);
  27. return locales;
  28. }
  29. // Pick a different string if the desired one is missing
  30. const transvision = {};
  31. async function cherryPickString(locale) {
  32. const getTransvision = async string => {
  33. if (!transvision[string]) {
  34. const response = await fetch(`https://transvision.mozfr.org/api/v1/entity/gecko_strings/?id=${string}`); // eslint-disable-line fetch-options/no-fetch-credentials
  35. transvision[string] = response.ok ? await response.json() : {};
  36. }
  37. return transvision[string];
  38. };
  39. const expectedKey = "section_menu_action_add_search_engine";
  40. const expected = await getTransvision(`browser/chrome/browser/activity-stream/newtab.properties:${expectedKey}`);
  41. const target = await getTransvision("browser/chrome/browser/search.properties:searchAddFoundEngine2");
  42. return !expected[locale] && target[locale] ? `${expectedKey}=${target[locale]}\n` : "";
  43. }
  44. // Save the properties file to the locale's directory
  45. async function saveProperties(locale) {
  46. // Only save a file if the repository has the file
  47. const url = `${L10N_CENTRAL}/${locale}/${PROPERTIES_PATH}`;
  48. const response = await fetch(url); // eslint-disable-line fetch-options/no-fetch-credentials
  49. if (!response.ok) {
  50. // Indicate that this locale didn't save
  51. return locale;
  52. }
  53. // Save the file to the right place
  54. const text = await response.text();
  55. mkdir(locale);
  56. cd(locale);
  57. // For now, detect if a string is missing to use a different one instead
  58. ShellString(text + await cherryPickString(locale)).to(STRINGS_FILE);
  59. cd("..");
  60. // Indicate that we were successful in saving
  61. return "";
  62. }
  63. // Replace and update each locale's strings
  64. async function updateLocales() {
  65. console.log(`Switching to and deleting existing l10n tree under: ${LOCALES_SOURCE_DIRECTORY}`);
  66. cd(LOCALES_SOURCE_DIRECTORY);
  67. ls().forEach(dir => {
  68. // Keep the default/source locale as it might have newer strings
  69. if (dir !== DEFAULT_LOCALE) {
  70. rm("-r", dir);
  71. }
  72. });
  73. // Save the properties file for each locale one at a time to avoid too many
  74. // parallel connections (resulting in ECONNRESET / socket hang up)
  75. const missing = [];
  76. for (const locale of await getLocales()) {
  77. process.stdout.write(`${locale} `);
  78. if (await saveProperties(locale)) {
  79. missing.push(locale);
  80. }
  81. }
  82. console.log("");
  83. console.log(`Skipped ${missing.length} locales without strings: ${missing.sort()}`);
  84. console.log(`
  85. Please check the diffs, add/remove files, and then commit the result. Suggested commit message:
  86. chore(l10n): Update from l10n-central ${new Date()}`);
  87. }
  88. updateLocales().catch(console.error);