npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

translation-json-cleaner

v1.0.1

Published

A CLI tool to find and remove unused translation keys

Downloads

21

Readme

Translation JSON Cleaner

A command-line tool for cleaning up translation JSON files by removing unused keys and empty objects. This tool helps to keep your translation files organized and efficient by ensuring that only the necessary keys are present.

Usage Example

After installing the package, you can run the tool from the command line. Below is a step-by-step example of how to use the translation JSON cleaner.

Step 1: Install the Package

If you haven't already installed the package, you can do so using npm:

npm install -g translation-json-cleaner

or use it with npm scripts:

npx translation-json-cleaner

Step 2: Prepare Your Files

Assume you have the following project structure:

my-project/
├── src/
│   ├── index.js
│   └── components/
│       ├── Button.js
│       └── Header.js
├── translations/
│   └── en.json
└── package.json

Your translations/en.json file might look like this:

{
  "greeting": "Hello",
  "farewell": "Goodbye",
  "nested": {
    "welcome": "Welcome to our application!",
    "thankYou": "Thank you for using our app!"
  }
}

Step 3: Run the Cleaner

Navigate to your project directory and run the translation JSON cleaner:

translation-json-cleaner

or use it with npm scripts:

npx translation-json-cleaner

Step 4: Provide the Required Inputs

The tool will prompt you for the following information:

  1. Translation File Path: Enter the path to your JSON translation file. For example:
./translations/en.json
  1. Project Path: Enter the root path of your project where the keys will be checked. For example:
./
  1. File Extensions: Enter a comma-separated list of file extensions to scan for translation keys. For example:
js,ts,jsx

Step 5: Review the Results

The tool will scan your project files for keys used in your source code and compare them to the keys in your translation JSON file. It will list any unused keys found and ask whether you want to remove them.

Step 6: Confirm Removal

If there are unused keys, you will see a prompt like this:

Unused keys found:
- farewell

Do you want to remove the unused keys and any empty objects? (yes/no)

Type yes to confirm removal. The unused keys will be deleted, and any empty objects will also be removed from your translation file. After running the tool, your translations/en.json file might look like this:

{
  "greeting": "Hello",
  "nested": {
    "welcome": "Welcome to our application!",
    "thankYou": "Thank you for using our app!"
  }
}

Table of Contents

Installation

You can install the package globally or as a dependency in your project.

Globally

To install the CLI tool globally, run:

npm install -g translation-json-cleaner

As a Dependency

To add it to your project, use:

npm install translation-json-cleaner -g

Usage

After installation, you can run the tool from the command line:

translation-json-cleaner

It's recommended run this code from the root of your project to ensure that all translation files are cleaned up.

Prompts

When you run the tool, you will be prompted to provide the following information:

  1. Translation File Path: The path to your JSON translation file (e.g., ./path/to/translations.json).
  2. Project Path: The root path of your project where the keys will be checked (e.g., ./).
  3. File Extensions: A comma-separated list of file extensions to scan for translation keys (e.g., js,ts,jsx). You can leave this blank to check all file types.

Key Removal

The tool will:

  • Scan your project for keys used in your source files.
  • List any unused keys found in the translation file.
  • Ask if you want to remove the unused keys and any empty objects left behind.

Features

  • Removes unused translation keys: Cleans up your translation JSON files by removing keys that are not referenced in your project.
  • Removes empty objects: Cleans up any empty objects left after unused keys are removed.
  • Supports nested translation keys: Handles complex translation structures with nested keys.
  • Configurable: Allows you to specify the file extensions to scan for translation keys.

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any enhancements or bug fixes.

Acknowledgments

  • Thanks to chalk for colorizing console output.
  • Thanks to glob for file pattern matching.
  • Thanks to inquirer for prompting user input.