The core translation functionality of i18next, accessed through the
t()The main translation function that retrieves translated strings with support for interpolation, pluralization, and context.
/**
* Main translation function
* @param key - Translation key or array of keys (fallback chain)
* @param options - Translation options including interpolation data
* @returns Translated string
*/
function t(key: string | string[], options?: TOptions): string;
interface TOptions {
/** Default value if translation key is missing */
defaultValue?: string;
/** Override language for this translation */
lng?: string;
/** Override language hierarchy for this translation */
lngs?: readonly string[];
/** Override namespace for this translation */
ns?: string | readonly string[];
/** Interpolation data object */
replace?: { [key: string]: any };
/** Count for pluralization */
count?: number;
/** Context for contextual translations */
context?: string;
/** Skip interpolation processing */
skipInterpolation?: boolean;
/** Return object instead of string */
returnObjects?: boolean;
/** Join array values with separator */
joinArrays?: string;
/** Post-processing to apply */
postProcess?: string | string[];
/** Key prefix to prepend */
keyPrefix?: string;
[key: string]: any;
}Usage Examples:
import i18next from "i18next";
// Basic translation
i18next.t("welcome"); // "Welcome"
// With interpolation
i18next.t("hello", { name: "John" }); // "Hello John!"
// With fallback keys
i18next.t(["greeting.formal", "greeting.default"]); // Uses first available key
// Override language
i18next.t("welcome", { lng: "de" }); // "Willkommen"
// With default value
i18next.t("missing.key", { defaultValue: "Default text" }); // "Default text"Checks if a translation key exists without returning the translation.
/**
* Check if translation key exists
* @param key - Translation key or array of keys
* @param options - Options for key resolution
* @returns True if key exists
*/
function exists(key: string | string[], options?: TOptions): boolean;Usage Examples:
// Check if key exists
if (i18next.exists("welcome")) {
console.log(i18next.t("welcome"));
}
// Check with options
if (i18next.exists("hello", { lng: "de" })) {
console.log(i18next.t("hello", { lng: "de", name: "World" }));
}Creates a translation function bound to specific language, namespace, or key prefix.
/**
* Get translation function bound to specific parameters
* @param lng - Language or array of languages
* @param ns - Namespace or array of namespaces
* @param keyPrefix - Key prefix to prepend to all keys
* @returns Bound translation function
*/
function getFixedT<Ns extends Namespace | null = DefaultNamespace>(
lng?: string | readonly string[],
ns?: Ns,
keyPrefix?: string
): TFunction;Usage Examples:
// Fixed to German language
const tGerman = i18next.getFixedT("de");
tGerman("welcome"); // German translation of "welcome"
// Fixed to specific namespace
const tCommon = i18next.getFixedT(null, "common");
tCommon("button.save"); // From common namespace
// Fixed with key prefix
const tButtons = i18next.getFixedT(null, null, "buttons");
tButtons("save"); // Translates "buttons.save"
// Combined usage
const tGermanButtons = i18next.getFixedT("de", "common", "buttons");
tGermanButtons("save"); // German translation of "common:buttons.save"Dynamic value replacement in translation strings using double curly braces.
// Translation resource
{
"welcome": "Welcome {{name}}!",
"stats": "You have {{count}} {{type}}",
"formatted": "Price: {{price, currency}}"
}
// Usage
i18next.t("welcome", { name: "Alice" }); // "Welcome Alice!"
i18next.t("stats", { count: 5, type: "messages" }); // "You have 5 messages"
i18next.t("formatted", { price: 29.99 }); // "Price: $29.99" (with formatter)Automatic plural form selection based on count values.
// Translation resource
{
"item": "{{count}} item",
"item_plural": "{{count}} items",
"item_zero": "No items"
}
// Usage
i18next.t("item", { count: 0 }); // "No items"
i18next.t("item", { count: 1 }); // "1 item"
i18next.t("item", { count: 5 }); // "5 items"Contextual translations for different situations.
// Translation resource
{
"friend": "A friend",
"friend_male": "A boyfriend",
"friend_female": "A girlfriend"
}
// Usage
i18next.t("friend"); // "A friend"
i18next.t("friend", { context: "male" }); // "A boyfriend"
i18next.t("friend", { context: "female" }); // "A girlfriend"Reference other translation keys within translations.
// Translation resource
{
"name": "John",
"greeting": "Hello $t(name)!",
"welcome": "$t(greeting) Welcome to our app."
}
// Usage
i18next.t("welcome"); // "Hello John! Welcome to our app."Handle complex data structures in translations.
// Translation resource
{
"menu": ["Home", "About", "Contact"],
"user": {
"name": "John Doe",
"role": "Admin"
}
}
// Usage with returnObjects
i18next.t("menu", { returnObjects: true }); // ["Home", "About", "Contact"]
i18next.t("user", { returnObjects: true }); // { name: "John Doe", role: "Admin" }
// Usage with joinArrays
i18next.t("menu", { joinArrays: " | " }); // "Home | About | Contact"Type-safe translation key selection using proxy-based selectors.
/**
* Extract translation key from selector function
* @param selector - Selector function using proxy object
* @returns Translation key string
*/
function keyFromSelector<S = Record<string, any>, T = string>(
selector: ($: S) => T
): T;Usage Examples:
import { keyFromSelector, t } from "i18next";
// Define your resource structure type
interface Resources {
common: {
buttons: {
save: string;
cancel: string;
};
};
}
// Use selector to get type-safe keys
const saveButtonKey = keyFromSelector<Resources>(($) => $.common.buttons.save);
// saveButtonKey is "common:buttons.save"
// Use with translation
const saveText = t(saveButtonKey); // Type-safe translationTranslation function handles missing keys gracefully and can be configured for different fallback behaviors.
// Configure missing key behavior
await i18next.init({
saveMissing: true, // Save missing keys to backend
missingKeyHandler: (lng, ns, key, fallbackValue) => {
console.warn(`Missing translation: ${key} in ${lng}:${ns}`);
},
parseMissingKeyHandler: (key) => {
// Transform missing key for display
return key.replace(/([A-Z])/g, ' $1').trim();
}
});
// Missing keys with fallback
i18next.t("missing.key", { defaultValue: "Fallback text" }); // "Fallback text"
// Missing keys return key when no fallback
i18next.t("missing.key"); // "missing.key"