Files
RN_Template/RN_TEMPLATE/app/i18n/translate.ts

34 lines
641 B
TypeScript
Raw Permalink Normal View History

2026-02-05 13:16:05 +08:00
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
}