Created
April 14, 2022 11:03
-
-
Save predragnikolic/61917737c22c55cbeb74fdb18f7bb76f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This script *tries* to find unused tranlations. :) | |
Do not expect 100% perfect detection, but it should give good enough results. | |
This script can detect: | |
``` | |
t('some_translation_key') // can be detected | |
let x: TranslationKey = "some_translation_key" // can be detected, because of the use of `t()` | |
let x = "some_translation_key" as TranslationKey // can be detected, because of the use of TranslationKey | |
<Trans i18nKey="some_translation_key"` /> can be detected, because of the use of prop i18nKey | |
``` | |
This script cannot detect: | |
``` | |
{message: "some_translation_key"} // cannot be detected. | |
``` | |
A way to solve it is to use the `as TranslationKey` | |
``` | |
{message: "some_translation_key" as TranslationKey} // can be detected, because of the use of TranslationKey | |
``` | |
You can tweak the linePatternsThatMayContainTranslations to extend the detection of lines with translations. | |
You can twaek the ignoreTranslations to ignore some translations. | |
*/ | |
/** | |
* Regex patterns that should detect a translation key. | |
* Specify them here: | |
*/ | |
const linePatternsThatMayContainTranslations = [ | |
/.*t\(.*/, // should find `{t('some_key')}` or `i18n.t('some_key')` calls | |
/.*TranslationKey.*/, // should find `message: "some_key" as TranslationKey` or `let title: TranslationKey = "some_key"` | |
/.*i18nKey.*/ // should find `<Trans i18nKey="diversity_page.openness.effects"` | |
] | |
/** | |
* There are some translations that shuold be ignored. | |
* Specify them here: | |
*/ | |
const ignoreTranslations = [ | |
/^enums/, // dynamically generated | |
/^hello_processing\.error_code/, // dynamically generated | |
/^hello_processing\.error_code/, // dynamically generated | |
/^assessment_detail\..*\.title/, // dynamically generated | |
/^assessment_detail\..*\.low/, // dynamically generated | |
/^assessment_detail\..*\.mid/, // dynamically generated | |
/^assessment_detail\..*\.high/, // dynamically generated | |
/^hello_results_page.populations/, // dynamically generated | |
/^video_recording.color_scheme/, // dynamically generated | |
/^hellovima.error_codes/, // dynamically generated | |
/^video_recording.fields_type.enum/, // dynamically generated | |
] | |
// --- You should not be interested in the bellow, but you can take a look. | |
const path = require('path'); | |
var findInFiles = require("find-in-files") | |
const targetDir = path.join(__dirname, '..', 'src') | |
const targetFiles = "(.ts|.tsx|.js|.jsx)$" | |
const allTranslationKeys = Object.keys(require("../src/i18n/locales/en.json")) | |
const notIgnoredTranslationKeys = allTranslationKeys.filter(translationKey => !isIgnored(translationKey)) | |
/** | |
* Ignore thanslations that are dynamically generated like `t{`some_key.{dynamicValue}`}` | |
* @param {string} translationKey The value | |
* @return {boolean} { description_of_the_return_value } | |
*/ | |
function isIgnored(translationKey) { | |
return ignoreTranslations.some(ignoredTraslationRegex => ignoredTraslationRegex.test(translationKey)) | |
} | |
async function main() { | |
const linesThatMaybeContainTranslations = [] | |
for (const pattern of linePatternsThatMayContainTranslations) { | |
const results = await findInFiles.find(pattern, targetDir, targetFiles) | |
const linesWithTranslations = extractLinesFromResults(results) | |
linesThatMaybeContainTranslations.push(...linesWithTranslations) | |
} | |
const unusedKeys = notIgnoredTranslationKeys.filter(translationKey => { | |
return !linesThatMaybeContainTranslations.some(line => line.includes(translationKey)) | |
}) | |
console.log(`Found ${unusedKeys.length} unused keys:`) | |
console.log(unusedKeys) | |
} | |
main() | |
/** | |
* @param {Results} results The results | |
* @return {string[]} The used translations. | |
*/ | |
function extractLinesFromResults(results) { | |
/** @type {string[]} */ | |
let usedTranslations = [] | |
for (const fileName in results) { | |
const result = results[fileName] | |
usedTranslations = usedTranslations.concat(result.matches) | |
} | |
return usedTranslations | |
} |
Package.json
{ "scripts": { "find-unused-translations": "node ./scripts/findUnusedTranslations.js" }, "devDependencies": { "find-in-files": "^0.5.0", } }
yes i put this one in json file
but my question is how run wich one command
node
?
or
yarn run
or
npx?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you install the
find-in-files
dependency?