-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce international-types (#20)
* feat: introduce international-types * chore(docs): mention international-types in the README * chore(docs): add international-types README * refactor: import type instead of import * chore(docs): replace relative to absolute links * fix: correct files * chore: bump to 0.1.1
- Loading branch information
Showing
14 changed files
with
201 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<p align="center"> | ||
<picture> | ||
<source media="(prefers-color-scheme: dark)" srcset="./assets/logo-white.png"> | ||
<source media="(prefers-color-scheme: light)" srcset="./assets/logo-black.png" /> | ||
<img alt="" height="100px" src="./assets/logo-white.png"> | ||
</picture> | ||
<br /> | ||
Type-safe internationalization (i18n) utility types | ||
</p> | ||
|
||
--- | ||
|
||
- [Features](#features) | ||
- [Usage](#usage) | ||
- [Type-safe keys](#type-safe-keys) | ||
- [License](#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](https://github.com/QuiiBz/next-international)! | ||
## Usage | ||
|
||
```bash | ||
pnpm install international-types | ||
``` | ||
|
||
### Type-safe keys | ||
|
||
```ts | ||
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 | ||
|
||
```ts | ||
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) { | ||
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 | ||
|
||
```ts | ||
import type { LocaleKeys, LocaleValue } from 'international-types' | ||
|
||
type Locale = { | ||
param: 'This is a {value}' | ||
'hello.people': 'Hello {name}! You are {age} years old.' | ||
} | ||
|
||
function t< | ||
Key extends LocaleKeys<Locale, undefined>, | ||
Value extends LocaleValue = ScopedValue<Locale, undefined, Key>, | ||
>(key: Key, param: ParamsObject<Value>) { | ||
// ... | ||
} | ||
|
||
t('param', { | ||
value: '' | ||
// value is required | ||
}) | ||
|
||
t('hello.people', { | ||
name: '', | ||
age: '' | ||
// name and age are required | ||
}) | ||
``` | ||
|
||
## License | ||
|
||
[MIT](./LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export type LocaleValue = string | number; | ||
export type BaseLocale = Record<string, LocaleValue>; | ||
|
||
export type LocaleKeys< | ||
Locale extends BaseLocale, | ||
Scope extends Scopes<Locale> | undefined, | ||
Key extends string = Extract<keyof Locale, string>, | ||
> = Scope extends undefined ? Key : Key extends `${Scope}.${infer Test}` ? Test : never; | ||
|
||
export type Params<Value extends LocaleValue> = Value extends '' | ||
? [] | ||
: // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
Value extends `${infer Head}{${infer Param}}${infer Tail}` | ||
? [Param, ...Params<Tail>] | ||
: []; | ||
|
||
export type ParamsObject<Value extends LocaleValue> = Record<Params<Value>[number], LocaleValue>; | ||
|
||
export type ExtractScopes< | ||
Value extends string, | ||
Prev extends string | undefined = undefined, | ||
> = Value extends `${infer Head}.${infer Tail}` | ||
? [ | ||
Prev extends string ? `${Prev}.${Head}` : Head, | ||
...ExtractScopes<Tail, Prev extends string ? `${Prev}.${Head}` : Head>, | ||
] | ||
: []; | ||
|
||
export type Scopes<Locale extends BaseLocale> = ExtractScopes<Extract<keyof Locale, string>>[number]; | ||
|
||
export type ScopedValue< | ||
Locale extends BaseLocale, | ||
Scope extends Scopes<Locale> | undefined, | ||
Key extends LocaleKeys<Locale, Scope>, | ||
> = Scope extends undefined ? Locale[Key] : Locale[`${Scope}.${Key}`]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "international-types", | ||
"version": "0.1.1", | ||
"description": "Type-safe internationalization (i18n) utility types", | ||
"types": "dist/index.d.ts", | ||
"keywords": [ | ||
"i18n", | ||
"types", | ||
"typescript", | ||
"translate", | ||
"internationalization" | ||
], | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/QuiiBz/next-international.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/QuiiBz/next-international/issues" | ||
}, | ||
"homepage": "https://github.com/QuiiBz/next-international#readme", | ||
"scripts": { | ||
"build": "tsc --declaration --emitDeclarationOnly --outDir dist index.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/next-international/src/i18n/create-get-locale-static-props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { createI18n } from './i18n/create-i18n'; | ||
/* c8 ignore next */ | ||
export { BaseLocale, LocaleValue } from './types'; | ||
export type { BaseLocale, LocaleValue } from 'international-types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,8 @@ | ||
export type LocaleValue = string | number; | ||
export type BaseLocale = Record<string, LocaleValue>; | ||
|
||
export type LocaleKeys< | ||
Locale extends BaseLocale, | ||
Scope extends Scopes<Locale> | undefined, | ||
Key extends string = Extract<keyof Locale, string>, | ||
> = Scope extends undefined ? Key : Key extends `${Scope}.${infer Test}` ? Test : never; | ||
import type { BaseLocale } from 'international-types'; | ||
|
||
export type Locales = Record<string, () => Promise<any>>; | ||
|
||
export type LocaleContext<Locale extends BaseLocale> = { | ||
localeContent: Locale; | ||
fallbackLocale?: Locale; | ||
}; | ||
|
||
export type Params<Value extends LocaleValue> = Value extends '' | ||
? [] | ||
: // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
Value extends `${infer Head}{${infer Param}}${infer Tail}` | ||
? [Param, ...Params<Tail>] | ||
: []; | ||
|
||
export type ParamsObject<Value extends LocaleValue> = Record<Params<Value>[number], LocaleValue>; | ||
|
||
export type ExtractScopes< | ||
Value extends string, | ||
Prev extends string | undefined = undefined, | ||
> = Value extends `${infer Head}.${infer Tail}` | ||
? [ | ||
Prev extends string ? `${Prev}.${Head}` : Head, | ||
...ExtractScopes<Tail, Prev extends string ? `${Prev}.${Head}` : Head>, | ||
] | ||
: []; | ||
|
||
export type Scopes<Locale extends BaseLocale> = ExtractScopes<Extract<keyof Locale, string>>[number]; | ||
|
||
export type ScopedValue< | ||
Locale extends BaseLocale, | ||
Scope extends Scopes<Locale> | undefined, | ||
Key extends LocaleKeys<Locale, Scope>, | ||
> = Scope extends undefined ? Locale[Key] : Locale[`${Scope}.${Key}`]; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.