cleanupUnusedLocales.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const fs = require('fs')
  2. const Path = require('path')
  3. const { execSync } = require('child_process')
  4. const EN_JSON = Path.join(__dirname, '../../locales/en.json')
  5. const CHECK = process.argv.includes('--check')
  6. const SYNC_NON_EN = process.argv.includes('--sync-non-en')
  7. const COUNT_SUFFIXES = [
  8. '_plural',
  9. '_zero',
  10. '_one',
  11. '_two',
  12. '_few',
  13. '_many',
  14. '_other',
  15. ]
  16. async function main() {
  17. const locales = JSON.parse(await fs.promises.readFile(EN_JSON, 'utf-8'))
  18. const src = execSync(
  19. // - find all the app source files in web
  20. // - exclude data files
  21. // - exclude list of locales used in frontend
  22. // - exclude locales files
  23. // - exclude public assets
  24. // - exclude third-party dependencies
  25. // - exclude scripts
  26. // - exclude tests
  27. // - read all the source files
  28. `
  29. find . -type f \
  30. -not -path './cypress/results/*' \
  31. -not -path './data/*' \
  32. -not -path './frontend/extracted-translations.json' \
  33. -not -path './locales/*' \
  34. -not -path './public/*' \
  35. -not -path '*/node_modules/*' \
  36. -not -path '*/scripts/*' \
  37. -not -path '*/tests/*' \
  38. -exec cat {} +
  39. `,
  40. {
  41. // run from services/web directory
  42. cwd: Path.join(__dirname, '../../'),
  43. // 1GB
  44. maxBuffer: 1024 * 1024 * 1024,
  45. // Docs: https://nodejs.org/docs/latest-v16.x/api/child_process.html#child_process_options_stdio
  46. // Entries are [stdin, stdout, stderr]
  47. stdio: ['ignore', 'pipe', 'inherit'],
  48. }
  49. ).toString()
  50. const found = new Set([
  51. // Month names
  52. 'january',
  53. 'february',
  54. 'march',
  55. 'april',
  56. 'may',
  57. 'june',
  58. 'july',
  59. 'august',
  60. 'september',
  61. 'october',
  62. 'november',
  63. 'december',
  64. // Notifications created in third-party-datastore
  65. 'dropbox_email_not_verified',
  66. 'dropbox_unlinked_because_access_denied',
  67. 'dropbox_unlinked_because_full',
  68. // Actually used without the spurious space.
  69. // TODO: fix the space and upload the changed locales
  70. 'the_file_supplied_is_of_an_unsupported_type ',
  71. ])
  72. const matcher = new RegExp(
  73. `\\b(${Object.keys(locales)
  74. // Sort by length in descending order to match long, compound keys with
  75. // special characters (space or -) before short ones.
  76. // Examples:
  77. // - `\b(x|x-and-y)\b` will match `t('x-and-y')` as 'x'.
  78. // This is leaving 'x-and-y' as seemingly unused. Doh!
  79. // - `\b(x-and-y|x)\b` will match `t('x-and-y')` as 'x-and-y'. Yay!
  80. .sort((a, b) => (a.length < b.length ? 1 : -1))
  81. .join('|')})\\b`,
  82. 'g'
  83. )
  84. let m
  85. while ((m = matcher.exec(src))) {
  86. found.add(m[0])
  87. for (const suffix of COUNT_SUFFIXES) {
  88. found.add(m[0] + suffix)
  89. }
  90. }
  91. const unusedKeys = []
  92. for (const key of Object.keys(locales)) {
  93. if (!found.has(key)) {
  94. unusedKeys.push(key)
  95. }
  96. }
  97. if (SYNC_NON_EN) {
  98. if (CHECK) {
  99. throw new Error('--check is incompatible with --sync-non-en')
  100. }
  101. const LOCALES = Path.join(__dirname, '../../locales')
  102. for (const name of await fs.promises.readdir(LOCALES)) {
  103. if (name === 'README.md') continue
  104. if (name === 'en.json') continue
  105. const path = Path.join(LOCALES, name)
  106. const locales = JSON.parse(await fs.promises.readFile(path, 'utf-8'))
  107. for (const key of Object.keys(locales)) {
  108. if (!found.has(key)) {
  109. delete locales[key]
  110. }
  111. }
  112. const sorted =
  113. JSON.stringify(locales, Object.keys(locales).sort(), 2) + '\n'
  114. await fs.promises.writeFile(path, sorted)
  115. }
  116. }
  117. if (unusedKeys.length === 0) {
  118. return
  119. }
  120. console.warn('---')
  121. console.warn(
  122. `Found ${unusedKeys.length} unused translations keys:\n${unusedKeys
  123. .map(s => ` - '${s}'`)
  124. .join('\n')}`
  125. )
  126. console.warn('---')
  127. if (CHECK) {
  128. console.warn('---')
  129. console.warn(
  130. 'Try running:\n\n',
  131. ' web$ make cleanup_unused_locales',
  132. '\n'
  133. )
  134. console.warn('---')
  135. throw new Error('found unused translations keys')
  136. }
  137. console.log('Deleting unused translations keys')
  138. for (const key of unusedKeys) {
  139. delete locales[key]
  140. }
  141. const sorted = JSON.stringify(locales, Object.keys(locales).sort(), 2) + '\n'
  142. await fs.promises.writeFile(EN_JSON, sorted)
  143. }
  144. main().catch(error => {
  145. console.error(error)
  146. process.exit(1)
  147. })