0.16.0 • Published 7 months ago

i18n-unused v0.16.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

i18n-unused

npm npm

The static analyze tool for finding, marking and removing unused and missing i18n translations in your JavaScript project.

Installation

With npm:

npm install --save-dev i18n-unused

With yarn:

yarn add --dev i18n-unused

Configuration

Add config i18n-unused.config.js to your root folder:

/** @type {import('i18n-unused').RunOptions} */
module.exports = {
  localesPath: 'src/locales',
  srcPath: 'src',
};

For ES-Modules (esm) use i18n-unused.config.cjs. You can also use the .json with no support for callbacks.

Configuration options

Option nameDescriptionRequiredTypeDefault value
localesPathpath to search for localesyesstring-
localesExtensionsallowed file extensions for localesnostring[]if not set localeNameResolver: 'json'
localeNameResolverfile name resolver for localesnoRegExp, (name: string) => boolean-
customCheckerfunction to check if a key is used, if so the key should be removed from translationsKeysnofn, (matchedKeys: Set\<string>, translationsKeys: string[] => void)A defined validations will be applied
localeFileParserresolve locale imports, for example if you use named imports from locales files, just wrap it to your own resolverno(module) => modulefn, return module.default or module
localeFileLoaderload the locale file manually (e.g. for using your own parser)no(filePath) => object-
srcPathpath to search for translationsnostring'' (same as run folder)
srcExtensionsallowed file extensions for translationsnostring[]'js', 'ts', 'jsx', 'tsx', 'vue'
ignorePathsignored paths, eg: ['src/ignored-folder'], should start similarly srcPathnostring[]-
translationKeyMatchermatcher to search for translation keys in filesnoRegExpRegExp, match $_, $t, t, $tc, tc and i18nKey
excludeKeydoesn't process translations that include passed key(s), for example if you set excludeKey: '.props.', script will ignore Button.props.value.nostring, string[]-
ignoreCommentsIgnore code comments in src files.nobooleanfalse
markerspecial string to mark unused translations, it'll added via mark-unusednostring'UNUSED'
gitCheckshow git state change treenobooleanfalse
contextuse i18n context, (eg: plurals)nobooleantrue
flatTranslationsuse flat translations, (eg: Flat JSON)nobooleanfalse
translationSeparatorseparator for translations using in codenostring'.'
translationContextSeparatorseparator for i18n context (see context option)nostring'_'
translationContextMatchermatcher to search for context endingsnoRegExpRegExp, match zero, one, two, few, many, other, male, female, 0, 1, 2, 3, 4, 5, plural, 11 and 100
missedTranslationParserparser for ejecting value from translationKeyMatcher matchesnoRegExp, (v: string) => stringRegExp, match value inside rounded brackets
localeJsonStringifyIndentjson indent value for writing json file, either a number of spaces, or a string to indent with. (i.e. 2, 4, \t)nostring , number2

Usage

Get help:

i18n-unused -h

Display unused translations:

i18n-unused display-unused

Display unused translations for mashpie/i18n-node:

i18n-unused display-unused --translation-key-matcher '/(?:[$ .](__))\(.*?\)/gi'

Mark unused translations via [UNUSED] or marker from config (works only with json for now):

i18n-unused mark-unused

Remove unused translations (works only with json for now):

i18n-unused remove-unused

Sync translations (works only with json for now):

i18n-unused sync <source> <target>

Display missed translations:

i18n-unused display-missed

Usage in code

collectUnusedTranslations

If you use tool in code, you can run async function collectUnusedTranslations:

import { collectUnusedTranslations } from 'i18n-unused';

const handleTranslations = async () => {
  const unusedTranslations = await collectUnusedTranslations(
    localesPaths, // paths to locale files
    srcFilesPaths, // paths to src files
    {
      localeFileParser: (module) => module, // optional, resolver for module
      excludeTranslationKey: ['.props.'], // optional, special string or sting[] to exclude flat translations
    },
  );
};

It'll return to you follow collect:

{
  translations: [
    {
      localePath: 'locale_file_path',
      keys: ['unused_key'],
      count: 1,
    },
  ],
  totalCount: 1,
}

collectMissedTranslations

If you use tool in code, you can run async function collectMissedTranslations:

import { collectMissedTranslations } from 'i18n-unused';

const handleTranslations = async () => {
  const missedTranslations = await collectMissedTranslations(
    localesPaths, // paths to locale files
    srcFilesPaths, // paths to src files
    {
      localeFileParser: (module) => module, // optional, resolver for module
      excludeTranslationKey: ['.props.'], // optional, special string or sting[] to exclude flat translations
      translationKeyMatcher: /(?:[$ .](_|t|tc))\(.*?\)/ig, // optional, match translation keys in files
    },
  );
};

You'll get the following collection:

{
  translations: [
    {
      filePath: 'src_file_path',
      staticKeys: ['missed_key'], // keys without ${} syntax
      dynamicKeys: ['missed_key'], // keys with ${} syntax
      staticCount: 1,
      dynamicCount: 1,
    },
  ],
  totalStaticCount: 1,
  totalDynamicCount: 1,
}

generateFilesPaths

Available as async function generateFilesPaths:

import { generateFilesPaths } from 'i18n-unused';

const handleFilesPaths = async () => {
  // return array of full paths to files
  const filesPaths = await generateFilesPaths(
    srcPath, // path where search files, example: 'src/locales'
    {
      srcExtensions, // allowed file extensions, example: ['js', 'ts']
      fileNameResolver, // resolver for file name, see more info about 'localeNameResolver' option
    },
  );
};

Action results

Next actions return unusedTranslations:

  • displayUnusedTranslations
  • removeUnusedTranslations
  • markUnusedTranslations

Next actions return missedTranslations:

  • displayMissedTranslations

What else?

If the tool helped you, please rate it on github, thx. I'll be glad to your PRs =)

License

MIT License. Maxim Vishnevsky

0.16.0

7 months ago

0.15.0

8 months ago

0.14.0

10 months ago

0.11.0

2 years ago

0.12.0

2 years ago

0.11.1

2 years ago

0.13.0

1 year ago

0.10.0

2 years ago

0.9.0

2 years ago

0.8.0

2 years ago

0.7.2

2 years ago

0.7.1

2 years ago

0.7.0

2 years ago

0.5.2

2 years ago

0.6.0

2 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.0

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.1.2

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.0

3 years ago

0.0.8

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago