Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

international-types

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

international-types

Type-safe internationalization (i18n) utility types

  • 0.8.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source


Type-safe internationalization (i18n) utility types


  • Features
  • Usage
  • License

Features

  • Autocompletion: For locale keys, scopes and params!
  • Extensible: Designed to be used with any library

Note: Using Next.js? Check out next-international!

Usage

pnpm install international-types

Type-safe keys

import type { LocaleKeys } from 'international-types'

type Locale = {
  hello: 'Hello'
  'hello.world': 'Hello World!'
}

function t<Key extends LocaleKeys<Locale, undefined>>(key: Key) {
  // ...
}

t('')
// hello | hello.world

Type-safe scopes with keys

import type { LocaleKeys, Scopes } from 'international-types'

type Locale = {
  hello: 'Hello'
  'scope.nested.demo': 'Nested scope'
  'scope.nested.another.demo': 'Another nested scope'
}

function scopedT<Scope extends Scopes<Locale>>(scope: Scope) {
  return function t<Key extends LocaleKeys<Locale, Scope>>(key: Key) {
    // ...
  }
}

const t = scopedT('')
// scope | scope.nested

t('')
// For scope: nested.demo | nested.another.demo
// For scope.nested: demo | another.demo

Type-safe params

import type { LocaleKeys, BaseLocale, Scopes, ScopedValue, CreateParams, ParamsObject } from 'international-types'

type Locale = {
  param: 'This is a {value}'
  'hello.people': 'Hello {name}! You are {age} years old.'
}

function scopedT<Locale extends BaseLocale, Scope extends Scopes<Locale> | undefined>(scope?: Scope) {
  return function t<Key extends LocaleKeys<Locale, Scope>, Value extends ScopedValue<Locale, Scope, Key>>(
    key: Key,
    ...params: CreateParams<ParamsObject<Value>, Locale, Scope, Key, Value>
  ) {
    // ...
  }
}

const t = scopedT<Locale, undefined>();

t('param', {
  value: ''
  // value is required
})

t('hello.people', {
  name: '',
  age: ''
  // name and age are required
})

License

MIT

Keywords

FAQs

Package last updated on 12 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc