diff --git a/README.md b/README.md index 11f1eef..0f815f1 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,12 @@ const bot = new Bot(process.env.BOT_TOKEN as string) }); ``` +`ExtractLanguages` helps you extract languages types from i18n instance. + +```ts +type EnLocalizationKeys = keyof ExtractLanguages["en"]; +``` + ## [Fluent](https://projectfluent.org/) syntax This plugin provide internationalization for your bots with [Fluent](https://projectfluent.org/) syntax. diff --git a/package.json b/package.json index a51605a..fbdc793 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gramio/i18n", - "version": "1.0.0", + "version": "1.0.1", "description": "i18n plugin for GramIO with type-safety", "main": "dist/index.cjs", "module": "dist/index.js", diff --git a/src/index.ts b/src/index.ts index 8f7b3f6..43ef7d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,20 +1,13 @@ import type { ExtractArgsParams, ExtractItemValue, + I18nOptions, LanguagesMap, SoftString, } from "./types.js"; export * from "./types.js"; -export interface I18nOptions< - Languages extends LanguagesMap, - PrimaryLanguage extends keyof Languages, -> { - languages: Languages; - primaryLanguage: PrimaryLanguage; -} - export function defineI18n< Languages extends LanguagesMap, PrimaryLanguage extends keyof Languages, @@ -79,6 +72,10 @@ export function defineI18n< : ExtractArgsParams ): ExtractItemValue => t(language, key, ...args); }, + _: { + languages, + primaryLanguage, + }, // plugin: () => {}, }; } diff --git a/src/types.ts b/src/types.ts index 04fd536..5d8f063 100644 --- a/src/types.ts +++ b/src/types.ts @@ -40,3 +40,17 @@ export type ShouldFollowLanguageStrict = { }; export type SoftString = Strings | ({} & string); + +export interface I18nOptions< + Languages extends LanguagesMap, + PrimaryLanguage extends keyof Languages, +> { + languages: Languages; + primaryLanguage: PrimaryLanguage; +} + +export type ExtractLanguages = T extends { + _: { languages: infer Languages }; +} + ? Languages + : never; diff --git a/tests/index.test.ts b/tests/index.test.ts index a2d8a76..c090116 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from "bun:test"; import { Bot, format } from "gramio"; import { defineI18n } from "../src"; -import type { ShouldFollowLanguage } from "../src/types"; +import type { ExtractLanguages, ShouldFollowLanguage } from "../src/types"; const en = { greeting: (name: string) => format`Hello, ${name}!`, @@ -9,6 +9,16 @@ const en = { const ru: ShouldFollowLanguage = {}; +const i18n1 = defineI18n({ + languages: { + en, + ru, + }, + primaryLanguage: "en", +}); + +type A = ExtractLanguages["en"]; + describe("I18n", () => { it("Just t", () => { const i18n = defineI18n({