# python: t URL: https://generaltranslation.com/en-US/docs/python/api/t.mdx --- title: t description: API reference for the t translation function --- ## Overview The `t` function translates and interpolates an ICU MessageFormat string. It looks up the current locale, finds a cached translation by hash, and interpolates variables. If no translation is found, it falls back to the source string. ```python from gt_flask import t # or from gt_fastapi import t message = t("Hello, {name}!", name="World") ``` ## Reference ### Parameters ### GT options (passed via `**kwargs`) | Option | Type | Description | | ------ | ---- | ----------- | | `_context` | `str` | Additional context to disambiguate translations with the same source text. | | `_id` | `str` | Custom identifier for the translation entry. | | `_max_chars` | `int` | Maximum character length for the output. | ### Returns `str` — the translated and interpolated string. --- ## Examples ### Simple translation ```python t("Hello, world!") ``` ### With variables ```python t("Hello, {name}!", name="Alice") ``` ### With context ```python t("Bank", _context="financial institution") t("Bank", _context="river bank") ``` ### With f-strings and `declare_var` Python f-strings work naturally with `t` — just wrap your variables with [`declare_var`](/docs/python/api/declare-var): ```python from gt_flask import t, declare_var # Before i18n: message = f"{name} goes home" # After i18n — just wrap with t() and declare_var(): message = t(f"{declare_var(name, name='name')} goes home") ``` ### With character limit ```python t("This is a very long message that might need truncation", _max_chars=20) ``` --- ## Notes - `t` requires [`initialize_gt`](/docs/python/api/initialize-gt) to be called first. - If the current locale matches the default locale, no translation lookup is performed — only interpolation. - Variables are interpolated using ICU MessageFormat syntax (e.g., `{name}`, `{count, plural, one {# item} other {# items}}`).