i18next-scanner.config.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. if (process.platform !== 'win32') {
  21. options.ns = /packages\/(.*?)\/src/g.exec(file.path)[1].replace('page-', 'app-');
  22. } else {
  23. options.ns = /packages\\(.*?)\\src/g.exec(file.path)[1].replace('page-', 'app-');
  24. }
  25. this.parser.set(key, options);
  26. };
  27. this.parser.parseFuncFromString(outputText, parserHandler);
  28. }
  29. done();
  30. }
  31. module.exports = {
  32. input: [
  33. 'packages/*/src/**/*.{ts,tsx}',
  34. // Use ! to filter out files or directories
  35. '!packages/*/src/**/*.spec.{ts,tsx}',
  36. '!packages/*/src/i18n/**',
  37. '!**/node_modules/**'
  38. ],
  39. options: {
  40. debug: true,
  41. defaultLng: 'en',
  42. func: {
  43. extensions: ['.tsx', '.ts'],
  44. list: ['t', 'i18next.t', 'i18n.t']
  45. },
  46. keySeparator: false, // key separator
  47. lngs: ['en'],
  48. ns: findPackages().map(({ dir }) => dir.replace('page-', 'app-')),
  49. nsSeparator: false, // namespace separator
  50. resource: {
  51. jsonIndent: 2,
  52. lineEnding: '\n',
  53. loadPath: 'packages/apps/public/locales/{{lng}}/{{ns}}.json',
  54. savePath: 'packages/apps/public/locales/{{lng}}/{{ns}}.json'
  55. },
  56. trans: {
  57. component: 'Trans'
  58. }
  59. },
  60. output: './',
  61. transform
  62. };