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

i18n-file-tooling

v1.1.0

Published

For small projects where Weblate or Crowdin are overkill.

Downloads

46

Readme

i18n-file-tooling

For small projects where Weblate or Crowdin are overkill.

A command-line tool for updating and merging internationalization (i18n) translation files. Take a base language file that you work on (e.g. en.json) and synchronize its keys with other language files (e.g. de.json, fr.json). This tool will add missing keys, remove unused keys, and preserve existing translations.

Features

  • Sync Translation Keys: Automatically adds missing keys to translation files.
  • Preserve Existing Translations: Keeps existing translations intact when updating.
  • Remove Unused Keys: Cleans up translation files by removing keys that no longer exist in the base file.
  • Include Comments: Adds comments with the original text above each key for better context.
  • Watch Mode: Monitors the base file for changes and updates translation files automatically.
  • Support for TS/JS Files: Works with TypeScript (.ts) and JavaScript (.js) translation files in addition to JSON.
  • Customizable Code Style: Allows customization of code style preferences like quote types, trailing commas, and semicolons.

Installation

Install the package globally using npm:

npm install -g i18n-file-tooling

Usage

i18n-update --path <translations_folder> --from <base_language> --to <target_languages> [options]

Options

  • --path <path>: Path to the folder containing translation files. Defaults to the current directory (.).
  • --from <language>: Base language code (e.g., en). Defaults to en.
  • --to <languages>: Comma-separated list of target language codes (e.g., "de,fr,es").
  • --watch: Watch for changes in the base file and update translations automatically.
  • --ts: Use TypeScript (.ts) files instead of JSON.
  • --js: Use JavaScript (.js) files instead of JSON.
  • --single-quotes: Use single quotes in TS/JS mode.
  • --double-quotes: Use double quotes in TS/JS mode.
  • --trailing-comma / --no-trailing-comma: Include or omit trailing commas in objects.
  • --semicolon / --no-semicolon: Include or omit semicolons at the end of files.

Code Style Options (For TS/JS Mode)

The tool can auto-detect code style preferences from the base file. However, you can override these settings using the following options:

  • Quote Type:
    • --single-quotes: Use single quotes (') in strings.
    • --double-quotes: Use double quotes (") in strings.
  • Trailing Comma:
    • --trailing-comma: Include a trailing comma after the last property in objects.
    • --no-trailing-comma: Do not include a trailing comma.
  • Semicolon:
    • --semicolon: Include a semicolon at the end of the file.
    • --no-semicolon: Do not include a semicolon at the end.

Examples

Using JSON Files

Scenario

You have translation files in JSON format. Your base language is English (en.json), and you have translations for German (de.json) and French (fr.json). You want to ensure all translation files are up-to-date with the base file.

Base English File (en.json)
{
  "greeting": "Hello, world!",
  "farewell": "Goodbye!",
  "menu": {
    "home": "Home",
    "profile": "Profile",
    "settings": "Settings"
  }
}
Existing German Translation (de.json)
{
  // Hello, world!
  greeting: "Hallo, Welt!",
  // The key "farewell" is missing
  menu: {
    // Home
    home: "Startseite",
    // Profile
    profile: "Profil",
    // The key "settings" is missing
  },
}
Running the Tool

To update the German translation file based on the English base file, run:

i18n-update --path ./translations --from en --to de
Updated German Translation (de.json)
{
  // Hello, world!
  greeting: "Hallo, Welt!",
  // Goodbye!
  farewell: "",
  menu: {
    // Home
    home: "Startseite",
    // Profile
    profile: "Profil",
    // Settings
    settings: "",
  },
}

Using TypeScript Files

Scenario

You prefer to use TypeScript files for your translations.

Base English File (en.ts)
export default {
  // Welcome to our application!
  greeting: "Welcome!",
  // Please sign in to continue.
  signInPrompt: "Please sign in.",
  menu: {
    // Dashboard
    dashboard: "Dashboard",
    // Reports
    reports: "Reports",
    // Logout
    logout: "Logout",
  },
};
Existing French Translation (fr.ts)
export default {
  // Welcome to our application!
  greeting: "Bienvenue!",
  // The key "signInPrompt" is missing
  menu: {
    // Dashboard
    dashboard: "Tableau de bord",
    // Reports
    reports: "Rapports",
    // The key "logout" is missing
  },
};
Running the Tool with Code Style Options

To update the French translation file with your code style preferences, run:

i18n-update --path ./translations --from en --to fr --ts
Updated French Translation (fr.ts)
export default {
  // Welcome to our application!
  greeting: "Bienvenue!",
  // Please sign in to continue.
  signInPrompt: "",
  menu: {
    // Dashboard
    dashboard: "Tableau de bord",
    // Reports
    reports: "Rapports",
    // Logout
    logout: "",
  },
};
What Changed?
  • Added Missing Keys: The keys "signInPrompt" and "menu.logout" were added with empty string values.
  • Preserved Existing Translations: Existing translations were kept intact.
  • Auto-Detected Code Style: Note that the tool auto-detected the code style from the base file (double quotes, trailing commas, and semicolon at the end).

Watch Mode

To automatically update translations whenever the base file changes, use the --watch option:

i18n-update --path ./translations --from en --to "de,fr" --watch