@gramio/i18n
Version:
i18n plugin for GramIO with type-safety
97 lines (93 loc) • 2.59 kB
TypeScript
import * as gramio from 'gramio';
import { Plugin } from 'gramio';
import { FluentBundle } from '@fluent/bundle';
/**
* Options for {@link getFluentClient}
*/
interface I18nFluentClientOptions {
/**
* Default locale
* @default "en"
*/
defaultLocale?: string;
/**
* The path to the folder with `*.ftl` files
* @default "locales"
*/
directory?: string;
}
/**
* Fluent client
*/
interface I18nFluentClient<Bundle extends FluentBundle = FluentBundle, Languages extends string = string> {
languages: {
/**
* Bundles
*/
bundles: Map<string, Bundle>;
/**
* Default locale
* @default "en"
*/
fallback: Languages;
/**
* All locales
*/
all: Languages[];
/**
* Current locale
*/
current: Languages;
/**
* Change the current locale
*/
change: (language: Languages) => void;
};
/**
* Format pattern
*/
t: Bundle["formatPattern"];
}
/**
* Get fluent client
*/
declare function getFluentClient<Bundle extends FluentBundle = FluentBundle, Languages extends string = string>(options?: I18nFluentClientOptions): I18nFluentClient<Bundle, Languages>;
/**
* This plugin provide internationalization for your bots with [Fluent](https://projectfluent.org/) syntax.
* @example
* ```ts
* import { Bot } from "gramio";
* import { i18n } from "@gramio/i18n";
*
* const bot = new Bot(process.env.BOT_TOKEN as string)
* .extend(i18n())
* .command("start", async (context) => {
* return context.send(
* context.t("shared-photos", {
* userName: "Anna",
* userGender: "female",
* photoCount: 3,
* })
* );
* })
* .onError(console.error)
* .onStart(console.log);
*
* bot.start();
* ```
*/
declare function i18n<Bundle extends FluentBundle = FluentBundle>(options?: I18nFluentClientOptions | I18nFluentClient<Bundle>): Plugin<{}, gramio.DeriveDefinitions & {
global: {
/** Object with localization utils and settings */
i18n: {
/** All languages */
locales: string[];
/** Current user locale */
locale: string;
/** Set locale to current user */
setLocale: (lang: string, strict?: boolean) => void;
};
t: Bundle["formatPattern"];
};
}>;
export { type I18nFluentClient, type I18nFluentClientOptions, getFluentClient, i18n };