cleanupUnusedLocales.js 4.3 KB

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