i18next-scanner.config.cjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * i18next-scanner 配置文件
  3. * 用于扫描代码中的翻译 key 并自动更新 locale JSON 文件
  4. *
  5. * 使用方法: pnpm run i18n
  6. */
  7. const typescriptTransform = require('i18next-scanner-typescript')
  8. module.exports = {
  9. input: [
  10. 'src/**/*.{js,jsx,ts,tsx,vue}',
  11. // 排除不需要扫描的目录
  12. '!src/locales/**',
  13. '!**/node_modules/**'
  14. ],
  15. output: './',
  16. options: {
  17. debug: false,
  18. removeUnusedKeys: true,
  19. sort: true,
  20. func: {
  21. // 扫描的函数名
  22. list: ['t', '$t', 'i18n.t'],
  23. extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue']
  24. },
  25. trans: {
  26. // Trans 组件 (如果使用)
  27. component: 'Trans',
  28. i18nKey: 'i18nKey',
  29. defaultsKey: 'defaults',
  30. extensions: ['.js', '.jsx', '.tsx', '.vue'],
  31. fallbackKey: false
  32. },
  33. // 支持的语言
  34. lngs: ['en', 'zh-cn'],
  35. ns: ['translation'],
  36. defaultLng: 'zh-cn',
  37. defaultNs: 'translation',
  38. // 默认值策略:中文使用 key 本身,英文留空待翻译
  39. defaultValue: (lng, ns, key) => {
  40. if (lng === 'zh-cn') {
  41. return key
  42. }
  43. return ''
  44. },
  45. resource: {
  46. loadPath: 'src/locales/{{lng}}.json',
  47. savePath: 'src/locales/{{lng}}.json',
  48. jsonIndent: 2,
  49. lineEnding: '\n'
  50. },
  51. nsSeparator: false, // 禁用 namespace 分隔符
  52. keySeparator: false, // 禁用 key 分隔符,支持扁平化 key
  53. interpolation: {
  54. prefix: '{{',
  55. suffix: '}}'
  56. }
  57. },
  58. // TypeScript 和 Vue 文件解析支持
  59. transform: typescriptTransform({
  60. extensions: ['.ts', '.tsx', '.vue'],
  61. tsOptions: {
  62. target: 'esnext'
  63. }
  64. })
  65. }