Skip to content

Commit

Permalink
feat: add ExtractLanguages helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Oct 9, 2024
1 parent dd760ef commit db8f13a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof i18n>["en"];
```

## [Fluent](https://projectfluent.org/) syntax

This plugin provide internationalization for your bots with [Fluent](https://projectfluent.org/) syntax.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 5 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -79,6 +72,10 @@ export function defineI18n<
: ExtractArgsParams<Item>
): ExtractItemValue<Item, FallbackItem> => t(language, key, ...args);
},
_: {
languages,
primaryLanguage,
},
// plugin: () => {},
};
}
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ export type ShouldFollowLanguageStrict<Language extends LanguageMap> = {
};

export type SoftString<Strings> = Strings | ({} & string);

export interface I18nOptions<
Languages extends LanguagesMap,
PrimaryLanguage extends keyof Languages,
> {
languages: Languages;
primaryLanguage: PrimaryLanguage;
}

export type ExtractLanguages<T> = T extends {
_: { languages: infer Languages };
}
? Languages
: never;
12 changes: 11 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
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}!`,
};

const ru: ShouldFollowLanguage<typeof en> = {};

const i18n1 = defineI18n({
languages: {
en,
ru,
},
primaryLanguage: "en",
});

type A = ExtractLanguages<typeof i18n1>["en"];

describe("I18n", () => {
it("Just t", () => {
const i18n = defineI18n({
Expand Down

0 comments on commit db8f13a

Please sign in to comment.