Skip to content

Commit ae843dd

Browse files
committed
feat: implement tag support in I18nApi#translate
1 parent 78678fb commit ae843dd

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

packages/shared-components/src/core/i18n/I18nApi.test.ts renamed to packages/shared-components/src/core/i18n/I18nApi.test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { describe, it, expect } from "vitest";
9+
import React from "react";
910

1011
import { I18nApi } from "./I18nApi";
1112

@@ -20,4 +21,19 @@ describe("I18nApi", () => {
2021

2122
expect(i18n.translate("hello.world" as TranslationKey)).toBe("Hello, World!");
2223
});
24+
25+
it("can register a translation and use it with tags", () => {
26+
const i18n = new I18nApi();
27+
i18n.register({
28+
["hello.world" as TranslationKey]: {
29+
en: "Hello, <world />",
30+
},
31+
});
32+
33+
expect(i18n.translate("hello.world" as TranslationKey, {}, { world: <strong>World!</strong> })).toStrictEqual(
34+
<span>
35+
Hello, <strong>World!</strong>
36+
</span>,
37+
);
38+
});
2339
});

packages/shared-components/src/core/i18n/I18nApi.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
55
Please see LICENSE files in the repository root for full details.
66
*/
77

8-
import { type I18nApi as II18nApi, type Variables, type Translations } from "@element-hq/element-web-module-api";
8+
import {
9+
type I18nApi as II18nApi,
10+
type Variables,
11+
type Translations,
12+
type Tags,
13+
} from "@element-hq/element-web-module-api";
914

1015
import { humanizeTime } from "../utils/humanize";
1116
import { _t, getLocale, registerTranslations } from "./i18n";
@@ -41,8 +46,12 @@ export class I18nApi implements II18nApi {
4146
* Perform a translation, with optional variables
4247
* @param key - The key to translate
4348
* @param variables - Optional variables to interpolate into the translation
49+
* @param tags - Optional tags to interpolate into the translation
4450
*/
45-
public translate(this: void, key: TranslationKey, variables?: Variables): string {
51+
public translate(this: void, key: TranslationKey, variables?: Variables): string;
52+
public translate(this: void, key: TranslationKey, variables: Variables | undefined, tags: Tags): React.ReactNode;
53+
public translate(this: void, key: TranslationKey, variables?: Variables, tags?: Tags): string {
54+
if (tags) return _t(key, variables, tags) as string;
4655
return _t(key, variables);
4756
}
4857

0 commit comments

Comments
 (0)