Add tags support to SC I18nApi (#32984)

* chore: update ew module to 1.13.0

* feat: implement tag support in I18nApi#translate

* fix: correct return type for translate

* test: translate World! in i18nApi test

* fix: again return type

* chore: update pnpm lock
This commit is contained in:
Florian Duros
2026-04-09 16:01:20 +01:00
committed by GitHub
parent b6b0b0009c
commit 3fd5718fcd
4 changed files with 45 additions and 18 deletions
@@ -6,6 +6,7 @@
*/
import { describe, it, expect } from "vitest";
import React from "react";
import { I18nApi } from "./I18nApi";
@@ -20,4 +21,21 @@ describe("I18nApi", () => {
expect(i18n.translate("hello.world" as TranslationKey)).toBe("Hello, World!");
});
it("can register a translation and use it with tags", () => {
const i18n = new I18nApi();
i18n.register({
["hello.world" as TranslationKey]: {
en: "Hello, <Bold>World!</Bold>",
},
});
expect(
i18n.translate("hello.world" as TranslationKey, {}, { Bold: (sub) => <strong>{sub}</strong> }),
).toStrictEqual(
<span>
Hello, <strong>World!</strong>
</span>,
);
});
});
@@ -5,7 +5,12 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
Please see LICENSE files in the repository root for full details.
*/
import { type I18nApi as II18nApi, type Variables, type Translations } from "@element-hq/element-web-module-api";
import {
type I18nApi as II18nApi,
type Variables,
type Translations,
type Tags,
} from "@element-hq/element-web-module-api";
import { humanizeTime } from "../utils/humanize";
import { _t, getLocale, registerTranslations } from "./i18n";
@@ -41,8 +46,12 @@ export class I18nApi implements II18nApi {
* Perform a translation, with optional variables
* @param key - The key to translate
* @param variables - Optional variables to interpolate into the translation
* @param tags - Optional tags to interpolate into the translation
*/
public translate(this: void, key: TranslationKey, variables?: Variables): string {
public translate(this: void, key: TranslationKey, variables?: Variables): string;
public translate(this: void, key: TranslationKey, variables: Variables | undefined, tags: Tags): React.ReactNode;
public translate(this: void, key: TranslationKey, variables?: Variables, tags?: Tags): React.ReactNode | string {
if (tags) return _t(key, variables, tags);
return _t(key, variables);
}