This page explains createTranslationFunction and getTranslations which can be used to internationalize Forge functions. To add internationalization support for your app's frontend, see the useTranslation hook for UI Kit apps or the Forge bridge i18n function for Custom UI apps.
The i18n
APIs allow you to access the translation resources configured in Translations so that your app can adapt based on a user’s language and locale. There
are two functions available:
createTranslationFunction()
allows you to generate a translation function, referred to as the t
function, bound to a specified locale code. This function performs translations by replacing
the requested translation key with its respective localized string based on the translation files specified
in the app manifest.
1 2const createTranslationFunction = ( locale: ForgeSupportedLocaleCode ): Promise<TranslationFunction> type TranslationFunction = (i18nKey: string, defaultValue?: string) => string;
t
function, which
performs translations by replacing the translation key with the translation value. It uses the
assigned locale code and fallback settings, if required, to determine the appropriate translation.
defaultValue
if provided.
If the key is not found and no defaultValue
is provided, it returns the translation key itself
to help identify which translations are missing.1 2import api, { i18n } from "@forge/api"; const resolver = new Resolver(); resolver.define("getText", async (req) => { const t = await i18n.createTranslationFunction('en-US'); return JSON.stringify({ text: t("page.content"), }); }); export const handler = resolver.getDefinitions();
getTranslations()
retrieves the content of a translation file for a specified locale
code.
1 2const getTranslations = ( locale: ForgeSupportedLocaleCode, options: GetTranslationsOptions = { fallback: true } ): Promise<GetTranslationsResult> interface GetTranslationsOptions { fallback: boolean; } interface GetTranslationsResult { locale: ForgeSupportedLocaleCode; translations: TranslationResourceContent | null; } interface TranslationResourceContent { [key: string]: string | TranslationResourceContent; }
locale: The locale code of the translation resource returned within the payload. If the
fallback
option is enabled and the translation resource for the requested locale is unavailable,
the returned locale might differ from the requested one as the function retrieves the translation
resource from the corresponding fallback locale instead.
translations: The parsed content of the translation file for the specified locale. If the
translation file and fallback can't be found for the given locale, the function returns null
. The
getTranslations()
function retrieves the complete content of the specified translation file. In
this process, the fallback
setting is not implemented at the specific translation key level.
Therefore, if the intended translation key is absent from the designated translation file, you might
need to obtain the required content for that key by requesting an alternative translation file.
1 2import api, { i18n } from "@forge/api"; const resolver = new Resolver(); resolver.define("getText", async (req) => { const { translations, locale } = await i18n.getTranslations("en-US"); return JSON.stringify({ translations, locale, }); }); export const handler = resolver.getDefinitions();
A user's locale
information is not directly available to Forge functions. This is because:
There are several ways for Forge functions to access a user's locale information. This includes:
For Confluence, you can use the getCurrentUser API to get the locale information for the current user.
1 2const getConfluenceLocale = async () => { const response = await api .asUser() .requestConfluence(route`/wiki/rest/api/user/current`, { headers: { Accept: "application/json", }, }); const confluence = await response.json(); return confluence.locale; };
For Jira, you can use the getLocale API to get the locale information for the current user.
1 2const getJiraLocale = async () => { const response = await api .asUser() .requestJira(route`/rest/api/3/mypreferences/locale`, { headers: { Accept: "application/json", }, }); const jira = await response.json(); return jira.locale; };
Rate this page: