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

i18next-types-gen

v1.1.0

Published

Generates TS declaration file from i18next translation files.

Downloads

4

Readme

i18next-types-gen

Generates TS declaration file from i18next translation files.

Due to the issue, it is currently not possible to use TypeScript to infer types from translation files when you have many keys. This tool solves this problem by generating a declaration type file from translation files using a simple script.

⚠️ Take a look at this pull request.

Installation

npm install --save-dev i18next-types-gen

How it works

In general, it turns this:

{
  "key1": "value1",
  "key2": {
    "key3": "value2"
  },
  "key4": {
    "key5": {
      "key6": "value3"
    }
  }
}

into this:

export interface LocalesKeys {
  key1: string;
  "key2:key3": string;
  "key4:key5.key6": string;
}

JavaScript API

genLocalesType(options)

Generates a TypeScript interface from translation files.

Receives an object with the following properties:

  • sources: An array of paths to translation JSON files.
  • interfaceName [optional]: The name of the generated interface. Default: LocalesKeys.

Returns a string with the generated interface and the number of keys.

  • typeDefinition: The generated interface.
  • size: The number of keys.

Example:

import { genLocalesType } from "i18next-types-gen";

const { typeDefinition, size } = genLocalesType({
  sources: ["./locales/en.json", "./locales/de.json"],
  interfaceName: "MyCustomInterfaceName",
});

genLocalesTypeIntoFile(options)

Generates a TypeScript interface from translation files and writes it to a file.

Receives an object with the following properties:

  • sources: An array of paths to translation JSON files.
  • output: The path to the output file.
  • interfaceName [optional]: The name of the generated interface. Default: LocalesKeys.

Returns the number of keys.

  • size: The number of keys.

Example:

import { genLocalesTypeIntoFile } from "i18next-types-gen";

const { typeDefinition, size } = genLocalesTypeIntoFile({
  sources: ["./locales/en.json", "./locales/de.json"],
  output: "./locales/locales.d.ts",
  interfaceName: "MyCustomInterfaceName",
});

CLI

You can even use this tool as a CLI.

There is only one available command: run [options] <sources> <output>.

It requires tho arguments:

  • sources: the paths to the translation files separated by commas.
  • output: the path to the output file.

Additionally, there is one optional argument:

  • name <value> - interface name. Default: LocalesKeys.

For example:

npx i18next-types-gen locales/en.json,locales/de.json locales/locales.d.ts --name MyCustomInterfaceName

License

MIT