international-types
v0.8.1
Published
Type-safe internationalization (i18n) utility types
Downloads
82,801
Maintainers
Readme
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
})