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-type-safe

v1.0.16

Published

A TypeScript library that provides type safety for i18n (internationalization) and i18next in React projects. Ensure that your translations and placeholders are type-checked, allowing you to catch translation-related bugs at compile-time.

Downloads

13

Readme

🛡️ i18n-type-safe


  • This is a TypeScript🔷 setup that will provides type safety for i18n (internationalization) in React ⚛️ projects utilizing ⚡️Vite оr ^ Expo.
  • Ensure that your translations and placeholders are type-safe, allowing you to catch translation-related bugs at compile-time.
  • Bulletproof you translations with i18n-type-safe! Don't let translation errors undermine your app's quality!

🌍 Translation File Protection

Protects Against Wrong/Missing Keys in Translation Files Say goodbye to the frustration of missing or incorrect translation keys. This setup ensures that all keys are valid and accounted for.

Prevents Wrong/Missing {{Placeholders}} in Translation Files Never worry about placeholders causing issues. The setup validates that all placeholders are correctly used in your translation strings.

🚀 In-App Protection

Protects Against Wrong Keys When Using the t() Function in Your App When you use the t() function in your app, the setup makes sure that you're referencing valid translation keys.

Prevents Wrong {{Placeholders}} When Using the t() Function in Your App Likewise, the setup ensures that your placeholders are used correctly within the t() function.

Autocomplete for Translation Keys and Placeholders Enjoy the convenience of autocomplete when working with translation keys and placeholders in both your translation files and your app.


🔍 Examples

  • Translation File Protection

// locales/en.ts
import { Locale } from '@/@types/locals'

export const en: Locale = {
  welcome: 'Welcome, {{username}}!', ✅ Correct!
  //wellcome: 'Welcome, {{usename}}!', ❌ wrong key or placeholder are detected
}
  • In-App Protection

// app/index.tsx
 import { useTranslation } from 'react-i18next'
 //... component code
 const { t } = useTranslation();
 //... render
 <p>{t('welcome', { username: 'John' })}</p> ✅ Correct!
// <p>{t('wellcome', { usename: 'John' })}</p> ❌ Wrong key or placeholder are detected

🛠️ Getting Started

Step 1: Install

Before starting with the setup, install the required the packages. Run the following command in your project's 📂 - root/ directory:

npm i i18n-type-safe

Also add if not included in your project

npm i i18next react-i18next

Step 2: Define Translation Types

  • Create 📂/@types/locals.ts

1.1 Define all types for your translations. 1.2 Utilize i18n-type-safe Generic to ensure type safety for placeholders in the translations.

import { Locale } from 'i18n-type-safe'

// 1.1
export interface MyInterface {
  welcome: '{{username}}'
  password: string
  // ... add other translation keys as needed
}

// 1.2
export type TypeSafeLocal = Locale<MyInterface>

Step 3: Extend the i18next module with custom type options

  • Create 📂/@types/i18next.d.ts
import { MyInterface } from './locals'

declare module 'i18next' {
  interface CustomTypeOptions {
    defaultNS: 'en', // default language
    resources: {
      en: MyInterface,
  //  de: MyInterface, 
    }
  }
}

Step 4: Create Translations

  • Create 📂/locales/en.ts

Implement the new TypeSafeLocal type in your translation files.

import { TypeSafeLocal } from '../@types/locals'

export const en: TypeSafeLocal = {
  welcome: 'Welcome, {{username}}!', // ✅ Correct!
  //wellcome: 'Welcome, {{usename}}!', ❌ Wrong key OR/AND placeholder detected!
  password: 'Password', // ✅ Correct!
  //pasword: 'Password', // ❌ Wrong OR missing key detected!
}

Step 5: Initialize and configure In-App usage

  • Create 📂/i18n.tsx
import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'

import {en} from './locales/en'
// import {de} from './locales/de' // German

const resources = {
  en: {
    translation: en,
  },
  // de: {
  //   translation: de,
  // }
}

i18n
.use(initReactI18next)
.init({
  compatibilityJSON: 'v3',
  lng: 'en', // (default lang)
  resources,
  interpolation: {
    escapeValue: false,
  },
})

export default i18n
  • Optionally, create a dynamic type for all your language keys, to be used in your project
export type Lang = keyof typeof resource

Step 6: Provide instance

  • In your project root file index.tsx or App.tsx
import '../i18n'

Step 7: Setup TS File Scope

  • Adjust the tsconfig.json file to ensure TypeScript includes the correct files for your i18n-type-safe implementation
"include": ["**/*.ts", "**/*.tsx"],

🎉 Congratulations, that's it — you are all set! 🎉

Enjoy a wonderful Developer Experience with 'i18n-type-safe', featuring autocomplete and error detection!


📜 License

Licensed under the GPL-3.0 license. This library is free and open-source!