i18next-scanner.config.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2017-2020 @polkadot/apps authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. const fs = require('fs');
  5. const path = require('path');
  6. const typescript = require('typescript');
  7. const findPackages = require('./scripts/findPackages');
  8. function transform (file, enc, done) {
  9. const { ext } = path.parse(file.path);
  10. if (ext === '.tsx') {
  11. const content = fs.readFileSync(file.path, enc);
  12. const { outputText } = typescript.transpileModule(content, {
  13. compilerOptions: {
  14. target: 'es2018'
  15. },
  16. fileName: path.basename(file.path)
  17. });
  18. const parserHandler = (key, options) => {
  19. options.defaultValue = key;
  20. options.ns = /packages\/(.*?)\/src/g.exec(file.path)[1].replace('page-', 'app-');
  21. this.parser.set(key, options);
  22. };
  23. this.parser.parseFuncFromString(outputText, parserHandler);
  24. }
  25. done();
  26. }
  27. module.exports = {
  28. input: [
  29. 'packages/*/src/**/*.{ts,tsx}',
  30. // Use ! to filter out files or directories
  31. '!packages/*/src/**/*.spec.{ts,tsx}',
  32. '!packages/*/src/i18n/**',
  33. '!**/node_modules/**'
  34. ],
  35. options: {
  36. debug: true,
  37. defaultLng: 'en',
  38. defaultNs: 'ui',
  39. func: {
  40. extensions: ['.tsx', '.ts'],
  41. list: ['t', 'i18next.t', 'i18n.t']
  42. },
  43. keySeparator: false, // key separator
  44. lngs: ['en'],
  45. ns: findPackages().map(({ dir }) => dir.replace('page-', 'app-')),
  46. nsSeparator: false, // namespace separator
  47. resource: {
  48. jsonIndent: 2,
  49. lineEnding: '\n',
  50. loadPath: 'packages/apps/public/locales/{{lng}}/{{ns}}.json',
  51. savePath: 'packages/apps/public/locales/{{lng}}/{{ns}}.json'
  52. },
  53. trans: {
  54. component: 'Trans'
  55. }
  56. },
  57. output: './',
  58. transform
  59. };