fetchDescriptions.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { ensureDirSync, removeSync, writeFileSync } from "fs-extra";
  2. import graphqlRequest from "./src/util/functions/graphql";
  3. let langList = [];
  4. graphqlRequest(`
  5. query {
  6. langFiles(project: "extension") {
  7. lang
  8. }
  9. }
  10. `).then(res => res.data.langFiles.forEach(lang => {
  11. langList.push(lang.lang);
  12. })).finally(() => {
  13. removeSync("src/_locales");
  14. Promise.all(
  15. langList.map(async langCode => {
  16. return [
  17. langCode,
  18. await translationsInLanguage(langCode, "extension.description.short")
  19. ];
  20. })
  21. ).then(data => {
  22. data.map(description => {
  23. switch (description[0]) {
  24. case "pt":
  25. description[0] = "pt_PT";
  26. break;
  27. case "ar_SA":
  28. description[0] = "ar";
  29. break;
  30. case "cs_CZ":
  31. description[0] = "cs";
  32. break;
  33. case "da_DK":
  34. description[0] = "da";
  35. break;
  36. case "he_IL":
  37. description[0] = "he";
  38. break;
  39. case "ja_JP":
  40. description[0] = "ja";
  41. break;
  42. case "ko_KR":
  43. description[0] = "ko";
  44. break;
  45. case "sl_SI":
  46. description[0] = "sl";
  47. break;
  48. case "sv_SE":
  49. description[0] = "sv";
  50. break;
  51. case "uk_UA":
  52. description[0] = "uk";
  53. break;
  54. case "bs_BA":
  55. description[0] = "bs";
  56. break;
  57. }
  58. if (!description[1]) return;
  59. ensureDirSync(`src/_locales/${description[0]}`);
  60. writeFileSync(
  61. `src/_locales/${description[0]}/messages.json`,
  62. JSON.stringify({
  63. description: {
  64. message: description[1]
  65. }
  66. })
  67. );
  68. });
  69. });
  70. });
  71. // Obtain extension strings, whole project or specific string (if given) fallbacks to english
  72. async function translationsInLanguage(langCode: string, string?: string): Promise<object|string> {
  73. const FALLBACK_LOCALE = "en";
  74. const langFiles = (await graphqlRequest(`
  75. query {
  76. langFiles(project: "extension", lang: "${langCode}") {
  77. translations
  78. }
  79. }
  80. `)).data.langFiles;
  81. if (langFiles.length === 0) {
  82. if (langCode !== FALLBACK_LOCALE) {
  83. return !string
  84. ? await translationsInLanguage(FALLBACK_LOCALE)
  85. : await translationsInLanguage(FALLBACK_LOCALE, string);
  86. } else {
  87. return string ?? {};
  88. }
  89. }
  90. const translations = langFiles[0].translations;
  91. if (!string) {
  92. return translations;
  93. }
  94. if (translations[string]) {
  95. return translations[string];
  96. } else if (langCode !== FALLBACK_LOCALE) {
  97. return await translationsInLanguage(FALLBACK_LOCALE, string);
  98. }
  99. return string;
  100. }