i18next-scanner.config.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const fs = require('node:fs')
  2. const path = require('node:path')
  3. const typescript = require('typescript')
  4. module.exports = {
  5. input: [
  6. 'frontend/js/**/*.{js,jsx,ts,tsx}',
  7. 'modules/*/frontend/js/**/*.{js,jsx,ts,tsx}',
  8. '!frontend/js/vendor/**',
  9. ],
  10. output: './',
  11. options: {
  12. sort: true,
  13. func: {
  14. list: ['t', 'phrase'],
  15. extensions: ['.js', '.jsx'],
  16. },
  17. trans: {
  18. component: 'Trans',
  19. i18nKey: 'i18nKey',
  20. defaultsKey: 'defaults',
  21. extensions: ['.js', '.jsx'],
  22. fallbackKey: false,
  23. },
  24. resource: {
  25. savePath: 'frontend/extracted-translations.json',
  26. jsonIndent: 2,
  27. lineEnding: '\n',
  28. },
  29. },
  30. // adapted from https://github.com/nucleartux/i18next-scanner-typescript/blob/master/src/index.js
  31. transform: function (file, enc, done) {
  32. const { base, ext } = path.parse(file.path)
  33. if (['.ts', '.tsx'].includes(ext) && !base.endsWith('.d.ts')) {
  34. const content = fs.readFileSync(file.path, enc)
  35. const { outputText } = typescript.transpileModule(content, {
  36. compilerOptions: { target: 'es2018', jsx: 'preserve' },
  37. fileName: base,
  38. })
  39. this.parser.parseTransFromString(outputText)
  40. this.parser.parseFuncFromString(outputText)
  41. }
  42. done()
  43. },
  44. }