i18n-extract.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env node
  2. import sh from 'shelljs'
  3. import fs from 'node:fs/promises'
  4. import * as vars from './vars.mjs'
  5. import log from 'npmlog'
  6. import { GettextExtractor, JsExtractors, HtmlExtractors } from 'gettext-extractor'
  7. let extractor = new GettextExtractor()
  8. const tempOutput = 'locale/app.new.pot'
  9. const pot = 'locale/app.pot'
  10. const tempHtml = 'locale/tmp-html'
  11. ;(async () => {
  12. sh.mkdir('-p', tempHtml)
  13. for (const plugin of vars.builtinPlugins) {
  14. log.info('compile-pug', plugin)
  15. sh.exec(`yarn pug --doctype html -s --pretty -O '{require: function(){}}' -o ${tempHtml}/${plugin} ${plugin}`, { fatal: true })
  16. }
  17. log.info('extract-ts')
  18. extractor.createJsParser([
  19. JsExtractors.callExpression('this.translate.instant', {
  20. arguments: { text: 0 },
  21. }),
  22. JsExtractors.callExpression('translate.instant', {
  23. arguments: { text: 0 },
  24. }),
  25. JsExtractors.callExpression('_', {
  26. arguments: { text: 0 },
  27. }),
  28. ]).parseFilesGlob('./tabby-*/src/**/*.ts')
  29. log.info('extract-pug')
  30. const options = {
  31. attributes: {
  32. context: 'translatecontext',
  33. },
  34. }
  35. extractor.createHtmlParser([
  36. HtmlExtractors.elementContent('translate, [translate=""]', options),
  37. HtmlExtractors.elementAttribute('[translate*=" "]', 'translate', options),
  38. ]).parseFilesGlob(`${tempHtml}/**/*.html`)
  39. extractor.savePotFile(tempOutput)
  40. extractor.printStats()
  41. sh.rm('-r', tempHtml)
  42. sh.exec(`msgcat -s ${tempOutput} > ${pot}`, { fatal: true })
  43. await fs.rename(tempOutput, pot)
  44. })()