0.8.1 • Published 2 years ago

international-types v0.8.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

0.8.1

2 years ago

0.8.0

2 years ago

0.6.2

2 years ago

0.5.3

2 years ago

0.5.0

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.5.2

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago