cleanupUnusedLocales.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 'dropbox_unlinked_because_suspended',
  72. // Actually used without the spurious space.
  73. // TODO: fix the space and upload the changed locales
  74. 'the_file_supplied_is_of_an_unsupported_type ',
  75. ])
  76. const matcher = new RegExp(
  77. `\\b(${Object.keys(locales)
  78. // Sort by length in descending order to match long, compound keys with
  79. // special characters (space or -) before short ones.
  80. // Examples:
  81. // - `\b(x|x-and-y)\b` will match `t('x-and-y')` as 'x'.
  82. // This is leaving 'x-and-y' as seemingly unused. Doh!
  83. // - `\b(x-and-y|x)\b` will match `t('x-and-y')` as 'x-and-y'. Yay!
  84. .sort((a, b) => (a.length < b.length ? 1 : -1))
  85. .join('|')})\\b`,
  86. 'g'
  87. )
  88. let m
  89. while ((m = matcher.exec(src))) {
  90. found.add(m[0])
  91. for (const suffix of COUNT_SUFFIXES) {
  92. found.add(m[0] + suffix)
  93. }
  94. }
  95. const unusedKeys = []
  96. for (const key of Object.keys(locales)) {
  97. if (!found.has(key)) {
  98. unusedKeys.push(key)
  99. }
  100. }
  101. if (SYNC_NON_EN) {
  102. if (CHECK) {
  103. throw new Error('--check is incompatible with --sync-non-en')
  104. }
  105. const LOCALES = Path.join(__dirname, '../../locales')
  106. for (const name of await fs.promises.readdir(LOCALES)) {
  107. if (name === 'README.md') continue
  108. if (name === 'en.json') continue
  109. const path = Path.join(LOCALES, name)
  110. const locales = loadLocale(name.replace('.json', ''))
  111. for (const key of Object.keys(locales)) {
  112. if (!found.has(key)) {
  113. delete locales[key]
  114. }
  115. }
  116. const sorted =
  117. JSON.stringify(locales, Object.keys(locales).sort(), 2) + '\n'
  118. await fs.promises.writeFile(path, sorted)
  119. }
  120. }
  121. if (unusedKeys.length === 0) {
  122. return
  123. }
  124. console.warn('---')
  125. console.warn(
  126. `Found ${unusedKeys.length} unused translations keys:\n${unusedKeys
  127. .map(s => ` - '${s}'`)
  128. .join('\n')}`
  129. )
  130. console.warn('---')
  131. if (CHECK) {
  132. console.warn('---')
  133. console.warn(
  134. 'Try running:\n\n',
  135. ' web$ make cleanup_unused_locales',
  136. '\n'
  137. )
  138. console.warn('---')
  139. throw new Error('found unused translations keys')
  140. }
  141. console.log('Deleting unused translations keys')
  142. for (const key of unusedKeys) {
  143. delete locales[key]
  144. }
  145. const sorted = JSON.stringify(locales, Object.keys(locales).sort(), 2) + '\n'
  146. await fs.promises.writeFile(EN_JSON, sorted)
  147. }
  148. try {
  149. await main()
  150. } catch (error) {
  151. console.error(error)
  152. process.exit(1)
  153. }