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 🙏

© 2026 – Pkg Stats / Ryan Hefner

rn-translation-gen

v1.5.6

Published

This library automatically generates strongly typed translation keys from JSON files for React Native and TypeScript projects. It ensures compile-time safety, preventing errors caused by incorrect translation keys.

Readme

Translation Key Generator for TypeScript

This library automatically generates strongly typed translation keys from JSON files for React Native and TypeScript projects.
It ensures compile-time safety, preventing errors caused by incorrect translation keys.

Additionally, this tool is designed as a build-time utility, meaning it should be installed as a devDependency rather than a regular dependency. This prevents unnecessary bloating of the production bundle, as the generated types are only required during development.


🚀 Features

Generates TypeScript types (translations.types.d.ts) for all translation keys.
Creates a TRANSLATION_KEYS object (translations.types.ts) for type-safe key usage.
Ensures compile-time errors if an incorrect key is used.
Works with deeply nested translation JSON structures.
Customizable input and output paths for translation files.
Supports configuration via both JSON (rn-translation-gen.json) and YAML (rn-translation-gen.yml) files.
Flexible output modes: Single-file (default) or dual-file mode using --output-mode.
Optionally exclude a top-level key (e.g., "translation") and unwrap its children using --exclude-key.
Control eslint quote disabling in generated files using --disable-eslint-quotes flag.
Verify types without generating files using --noEmit flag (perfect for CI/CD pipelines).
Optional Prettier formatting with --format flag for 80-character line wrapping and code style compliance.
Built-in help documentation with --help and -h flags.Runtime validator helper isTranslationKey emitted by default (disable with --no-validator).


📦 Installation

Since this is a build-time tool, install it as a devDependency:

npm install rn-translation-gen --save-dev

or using Yarn:

yarn add rn-translation-gen --dev

⚙️ Usage

Quick Start with Config File

Create rn-translation-gen.json in your project root:

{
  "input": "./src/locales",
  "output": "./src/generated/translations",
  "excludeKey": "translation"
}

Then add to your package.json:

{
  "scripts": {
    "translations:generate": "rn-translation-gen",
    "translations:check": "rn-translation-gen --noEmit"
  }
}

Run:

npm run translations:generate
npm run translations:check

All Available Flags

  • --input (required): Path to translation JSON files directory
  • --output (required): Path to output directory for generated files
  • --exclude-key (optional): Exclude and unwrap a top-level key (e.g., "translation")
  • --output-mode (optional): single (default) or dual mode
  • --format (optional): Format with Prettier (default: false)
  • --disable-eslint-quotes (optional): Add eslint-disable comments
  • --no-validator (optional): Skip emitting runtime validator helper (default: emitted)
  • --noEmit (optional): Type check only, don't generate files
  • --help, -h (optional): Show help

Config File Options

Both JSON and YAML config files support all flags:

{
  "input": "./src/locales",
  "output": "./src/generated/translations",
  "excludeKey": "translation",
  "format": true,
  "outputMode": "dual",
  "disableEslintQuotes": false,
  "noValidator": false,
  "noEmit": false
}

📌 Example

Let’s say you’re building a Multi-Screen App with screens like Chat, Profile, and Error Handling.

Translation file (en.json):

{
  "screens": {
    "Chat": {
      "title": "Chats",
      "actions": {
        "send": "Send",
        "delete": "Delete"
      }
    },
    "Profile": {
      "title": "Profile",
      "actions": {
        "edit": "Edit Profile",
        "logout": "Logout"
      }
    }
  },
  "messages": {
    "notifications": {
      "newMessage": "You have a new message!",
      "userOnline": "{username} is now online."
    },
    "errors": {
      "network": "Network error. Please try again.",
      "unauthorized": "Unauthorized access."
    }
  }
}

Generated output:

Type Definitions (translations.d.ts):

export type TranslationKey =
  | "screens.Chat.title"
  | "screens.Chat.actions.send"
  | "screens.Chat.actions.delete"
  | "screens.Profile.title"
  | "screens.Profile.actions.edit"
  | "screens.Profile.actions.logout"
  | "messages.notifications.newMessage"
  | "messages.notifications.userOnline"
  | "messages.errors.network"
  | "messages.errors.unauthorized";

Key Constants (translations.ts):

export const TRANSLATION_KEYS = {
  screens: {
    Chat: {
      title: "screens.Chat.title",
      actions: {
        send: "screens.Chat.actions.send",
        delete: "screens.Chat.actions.delete",
      },
    },
    Profile: {
      title: "screens.Profile.title",
      actions: {
        edit: "screens.Profile.actions.edit",
        logout: "screens.Profile.actions.logout",
      },
    },
  },
  messages: {
    notifications: {
      newMessage: "messages.notifications.newMessage",
      userOnline: "messages.notifications.userOnline",
    },
    errors: {
      network: "messages.errors.network",
      unauthorized: "messages.errors.unauthorized",
    },
  },
};

Usage in a React component:

import React from "react";
import { Text, View, Button } from "react-native";
import { TRANSLATION_KEYS } from "./translations";
import { TranslationKey } from "./translations";

const Notification = ({ messageKey }: { messageKey: TranslationKey }) => {
  return <Text>{messageKey}</Text>; // Displays the translation key
};

const ChatScreen = () => {
  const titleKey: TranslationKey = TRANSLATION_KEYS.screens.Chat.title;
  const sendKey: TranslationKey = TRANSLATION_KEYS.screens.Chat.actions.send;

  return (
    <View>
      <Text>{titleKey}</Text>
      <Button title={sendKey} onPress={() => {}} />
      <Notification messageKey={TRANSLATION_KEYS.messages.errors.network} />
    </View>
  );
};

export default ChatScreen;

📜 Notes

  • Ensure your translation files (en.json, ar.json, etc.) are valid JSON format.
  • Prerequisite: This tool requires jq, a lightweight JSON processor. Install it if not already available:
    • Mac: brew install jq
    • Linux: sudo apt install jq
    • Windows: choco install jq

✅ Enjoy seamless translations! Feel free to contribute or report issues. 🚀