Skip to content

Commit

Permalink
fix: unable extract translation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhtrinh committed Jan 23, 2024
1 parent 6f22795 commit d65c26d
Showing 1 changed file with 6 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,10 @@
const {exec} = require('child_process')
const fs = require('fs')
const path = require('path')

const packagePath = path.join(process.cwd(), 'package.json')
const pkgJSON = JSON.parse(fs.readFileSync(packagePath))

const getAllFilesByExtensions = (dirPath, arrayOfFiles = [], extensions = []) => {
const files = fs.readdirSync(dirPath, {withFileTypes: true})

files.forEach(function (file) {
const filePath = path.join(dirPath, file.name)
if (file.isDirectory()) {
arrayOfFiles = getAllFilesByExtensions(filePath, arrayOfFiles, extensions)
} else if (extensions.length === 0 || extensions.includes(path.extname(filePath))) {
arrayOfFiles.push(filePath)
}
})

return arrayOfFiles
}

function extract(locale) {
// `extends` is a reserved word (`class A extends B {}`)
const {extends: extendsPkg, overridesDir} = pkgJSON.ccExtensibility || {}
Expand All @@ -41,32 +27,21 @@ function extract(locale) {
`--out-file translations/${locale}.json`,
'--id-interpolation-pattern [sha512:contenthash:base64:6]'
].join(' ')

exec(command, (err) => {
if (err) {
console.error(err)
}
})
} else {
const overridesPath = path.join(process.cwd(), overridesDir)
// get all the files in extended app
const files = getAllFilesByExtensions(
path.join(overridesPath, 'app'),
[],
['.js', '.jsx', '.ts', '.tsx']
)
// get the file names that are overridden in base template
const overriddenFiles = files
.map((path) => path.replace(overridesDir, `node_modules/${extendsPkg}`))
.filter((file) => fs.existsSync(file))
const extractCommand = [
'formatjs extract',
'"./node_modules/${extendsPkg}/app/**/*.{js,jsx,ts,tsx}"',
'"${overridesDir}/app/**/*.{js,jsx,ts,tsx}"',
`"./node_modules/${extendsPkg}/app/**/*.{js,jsx,ts,tsx}"`,
`"${overridesDir}/app/**/*.{js,jsx,ts,tsx}"`,
`--out-file translations/${locale}.json`,
'--id-interpolation-pattern [sha512:contenthash:base64:6]',
'--ignore',
...overriddenFiles.map((file) => `'${file}'`)
'--id-interpolation-pattern [sha512:contenthash:base64:6]'
].join(' ')

exec(extractCommand, (err) => {
if (err) {
console.error(err)
Expand Down

0 comments on commit d65c26d

Please sign in to comment.