i18next-scanner.config.js 1.3 KB

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