Skip to content

Commit

Permalink
script enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Lysak committed Sep 11, 2024
1 parent daebaa5 commit 2a14c1d
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions scripts/scan-locales.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,47 @@ const baseLocale = getLocalePaths(BASE_LOCALE)

const { keys: translationKeys, namespaces } = getLocaleData(baseLocale)

// Path to your source code
const sourceCodePath = './src/**/*.{js,jsx,ts,tsx}' // adjust for relevant file types
const translationKeysWoNs = translationKeys.map((key) =>
key.replace(new RegExp(`^(${namespaces.join('|')}).`), ''),
)

// Search for translation keys in the source code
function searchForTranslationKeysInCode(pattern) {
function searchForTranslationKeysInCode() {
const regex = /t[cs]?\(['"`]([a-zA-Z0-9_.]+)['"`]\s*,?\s*{?/g
// = /t\(['"`]([a-zA-Z0-9_.]+)['"`]\s*,?\s*{?/g
// /t\(['"`]([a-zA-Z0-9_.]+)['"`]\)/g // regex to match t('key')
const files = glob.sync(pattern)
const files = [
...glob.sync(`./src/**/*.{js,jsx,ts,tsx}`),
...glob.sync(`./src/*.{js,jsx,ts,tsx}`),
]

const foundKeys = new Set()

files.forEach((file) => {
const content = fs.readFileSync(file, 'utf-8')
let match

while ((match = regex.exec(content)) !== null) {
foundKeys.add(match[1]) // Add the matched key
}

const keys = translationKeysWoNs.filter((key) => new RegExp(`['"\`]${key}['"\`]`).test(content))

for (const key of keys) {
foundKeys.add(key)
}
})

return foundKeys
}

// Find unused translation keys
function findUnusedKeys() {
const usedKeys = searchForTranslationKeysInCode(sourceCodePath)
const usedKeys = searchForTranslationKeysInCode()

const unusedKeys = []

const regex = new RegExp(`^(${namespaces.join('|')}).`)

for (const key of translationKeys.map((key) => key.replace(regex, ''))) {
for (const key of translationKeysWoNs) {
if (!usedKeys.has(key)) {
unusedKeys.push(key)
}
Expand Down

0 comments on commit 2a14c1d

Please sign in to comment.