Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 24481c9

Browse files
committed
Fix [object Object] in Widget Permissions
Due to two-step translations we could sometimes end up in a case where instead of a string we'd get a React Node wrapper, which tried being translated for the second time. This commit solves this problem by verifying if we have passed both Tags and Variables. Fixes element-hq/element-web#18384
1 parent c7560a2 commit 24481c9

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/languageHandler.tsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import PlatformPeg from "./PlatformPeg";
2828
import webpackLangJsonUrl from "$webapp/i18n/languages.json";
2929
import { SettingLevel } from "./settings/SettingLevel";
3030
import { retry } from "./utils/promise";
31+
import { isEmpty, isUndefined } from 'lodash';
3132

3233
const i18nFolder = 'i18n/';
3334

@@ -188,22 +189,35 @@ export function substitute(text: string, variables: IVariables, tags: Tags): str
188189
export function substitute(text: string, variables?: IVariables, tags?: Tags): string | React.ReactNode {
189190
let result: React.ReactNode | string = text;
190191

191-
if (variables !== undefined) {
192-
const regexpMapping: IVariables = {};
192+
const hasVariables = !isUndefined(variables);
193+
const hasTags = !isUndefined(tags);
194+
const mappingVariables: IVariables = {};
195+
/**
196+
* All three arguments is a very niche feature in this application, and needs special treatment.
197+
* In case where we have both variables and tags we need to run the replacements in one go
198+
* otherwise the second replacement may get React.ReactNode as the entry point which entirely
199+
* breaks the substitution and returns a simple [object Object] string.
200+
* As of writing this comment, the only files using all three arguments are:
201+
* AppPermission.tsx
202+
* NewRoomIntro.tsx
203+
* CapabilityText.tsx
204+
*/
205+
if (hasVariables) {
193206
for (const variable in variables) {
194-
regexpMapping[`%\\(${variable}\\)s`] = variables[variable];
207+
mappingVariables[`%\\(${variable}\\)s`] = variables[variable];
208+
}
209+
// Ignore the first substitution if we have tags in the same call
210+
if (!hasTags) {
211+
result = replaceByRegexes(result as string, mappingVariables);
195212
}
196-
result = replaceByRegexes(result as string, regexpMapping);
197213
}
198-
199-
if (tags !== undefined) {
200-
const regexpMapping: Tags = {};
214+
if (hasTags) {
215+
const regexpMapping: Tags | IVariables = hasVariables ? mappingVariables : {};
201216
for (const tag in tags) {
202217
regexpMapping[`(<${tag}>(.*?)<\\/${tag}>|<${tag}>|<${tag}\\s*\\/>)`] = tags[tag];
203218
}
204219
result = replaceByRegexes(result as string, regexpMapping);
205220
}
206-
207221
return result;
208222
}
209223

0 commit comments

Comments
 (0)