34 lines
641 B
TypeScript
34 lines
641 B
TypeScript
|
|
import i18n from "i18next"
|
||
|
|
import type { TOptions } from "i18next"
|
||
|
|
|
||
|
|
import { TxKeyPath } from "."
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Translates text.
|
||
|
|
* @param {TxKeyPath} key - The i18n key.
|
||
|
|
* @param {TOptions} options - The i18n options.
|
||
|
|
* @returns {string} - The translated text.
|
||
|
|
* @example
|
||
|
|
* Translations:
|
||
|
|
*
|
||
|
|
* ```en.ts
|
||
|
|
* {
|
||
|
|
* "hello": "Hello, {{name}}!"
|
||
|
|
* }
|
||
|
|
* ```
|
||
|
|
*
|
||
|
|
* Usage:
|
||
|
|
* ```ts
|
||
|
|
* import { translate } from "./i18n"
|
||
|
|
*
|
||
|
|
* translate("hello", { name: "world" })
|
||
|
|
* // => "Hello world!"
|
||
|
|
* ```
|
||
|
|
*/
|
||
|
|
export function translate(key: TxKeyPath, options?: TOptions): string {
|
||
|
|
if (i18n.isInitialized) {
|
||
|
|
return i18n.t(key, options)
|
||
|
|
}
|
||
|
|
return key
|
||
|
|
}
|