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

@spaced-out/eslint-plugin-i18n

v1.0.7

Published

### ESLint Rules for Translation and Internationalization: This repository introduces two ESLint rules designed to enforce best practices for internationalization (i18n) in your codebase. These rules ensure that static labels are replaced with translation

Downloads

489

Readme

eslint-plugin-i18n

ESLint Rules for Translation and Internationalization:

This repository introduces two ESLint rules designed to enforce best practices for internationalization (i18n) in your codebase. These rules ensure that static labels are replaced with translation functions and that all translation keys are defined in the corresponding translation files.

Rules overview

1. no-static-labels

Description:

This rule identifies and prevents the use of static labels (hardcoded strings) in JSX code. It enforces wrapping all static labels with a translation function (e.g., t('key', 'defaultValue')).

Key Features:

  • Detects static strings in JSX elements, attributes, and variables.
  • Identifies template literals, binary expressions, logical expressions, and conditional expressions containing static strings.
  • Supports dynamic attribute checks, including menu, options, tooltip, and more.
  • Auto-fix functionality to wrap detected static labels in a translation function.

Why Use It?

Ensures that all user-facing text is localized, improving the scalability and maintainability of your application for internationalization.

2. missing-translation

Description:

This rule checks if the translation keys used in your t function are present in your translation JSON files (e.g., en.json, fr.json). It warns developers about missing keys, ensuring translation files remain consistent with the codebase.

Key Features:

  • Verifies translation keys passed to the t function.
  • Supports t as a standalone function and as a method (e.g., this.t()).
  • Helps maintain completeness of translation files.
  • Warns developers if keys are missing from the source of truth JSON file (en.json).

Why Use It?

Ensures all user-facing text is properly localized and prevents runtime issues due to missing translations.

Installation

1. Install ESLint and the plugin:

npm install eslint eslint-plugin-i18n --save-dev

2. Add the rules to your .eslintrc.js configuration:

module.exports = {
  extends: [
    // other configurations...
  ],
  plugins: ['i18n'],
  rules: {
    'i18n/no-static-labels': 'error',
    'i18n/missing-translation': 'warn',
  },
};

3. Configure your translation files:

Place your translation files (e.g., en.json, fr.json) in the translations folder inside your i18n directory. The rules will automatically pick up these files to validate translation keys.

How It Works

Rule: no-static-labels

The rule inspects your code for hardcoded static strings. For example:

Input:

<div>Hello, World!</div>
const message = "Welcome!";

Output: The rule suggests wrapping the strings in a translation function:

<div>{t('Hello_World', 'Hello, World!')}</div>
const message = t('Welcome', 'Welcome!');

Supported Contexts:

  • JSX Text (<div>Static Text</div>)
  • JSX Attributes (<button label="Click me" />)
  • JavaScript Variables (const label = "Static text";)
  • Template Literals, Binary Expressions, Logical Expressions, Conditional Expressions, and Object Properties.

Rule: missing-translation

The rule checks translation keys used in the t function. If a key is missing in the JSON files, a warning is displayed:

Input:

const message = t('NonExistentKey', 'Default Message');

Warning Message:

Translation key "NonExistentKey" is missing in the config translations JSON file.

Supported Function Usages:

  • t('key', 'defaultValue')

Fix Suggestions and Workflow

Auto-Fix for no-static-labels

  • The no-static-labels rule supports auto-fix functionality. Run ESLint with the --fix flag to automatically wrap static labels in the t function.
npx eslint . --fix

Handling Missing Translations

  1. Run the linter to identify missing keys:
npx eslint .
  1. Use a script to generate the missing keys in your translation files. For example:
yarn generate-translations

This script should:

  • Parse all translation keys from the codebase.
  • Compare the keys with existing translation JSON files.
  • Append missing keys with default values.