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

Commit 5aae974

Browse files
authored
Make use of js-sdk roomNameGenerator to handle i18n for generated room names (#9209)
* Make use of js-sdk roomNameGenerator to handle i18n for generated room names * DRY * Make tsc happier * Update MatrixClientPeg.ts
1 parent 27de00a commit 5aae974

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

src/MatrixClientPeg.ts

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ See the License for the specific language governing permissions and
1717
limitations under the License.
1818
*/
1919

20-
import { ICreateClientOpts, PendingEventOrdering } from 'matrix-js-sdk/src/matrix';
20+
import { ICreateClientOpts, PendingEventOrdering, RoomNameState, RoomNameType } from 'matrix-js-sdk/src/matrix';
2121
import { IStartClientOpts, MatrixClient } from 'matrix-js-sdk/src/client';
2222
import { MemoryStore } from 'matrix-js-sdk/src/store/memory';
2323
import * as utils from 'matrix-js-sdk/src/utils';
@@ -37,6 +37,7 @@ import IdentityAuthClient from './IdentityAuthClient';
3737
import { crossSigningCallbacks, tryToUnlockSecretStorageWithDehydrationKey } from './SecurityManager';
3838
import SecurityCustomisations from "./customisations/Security";
3939
import CryptoStoreTooNewDialog from "./components/views/dialogs/CryptoStoreTooNewDialog";
40+
import { _t } from "./languageHandler";
4041

4142
export interface IMatrixClientCreds {
4243
homeserverUrl: string;
@@ -278,6 +279,48 @@ class MatrixClientPegClass implements IMatrixClientPeg {
278279
return matches[1];
279280
}
280281

282+
private namesToRoomName(names: string[], count: number): string | undefined {
283+
const countWithoutMe = count - 1;
284+
if (!names.length) {
285+
return _t("Empty room");
286+
}
287+
if (names.length === 1 && countWithoutMe <= 1) {
288+
return names[0];
289+
}
290+
}
291+
292+
private memberNamesToRoomName(names: string[], count: number): string {
293+
const name = this.namesToRoomName(names, count);
294+
if (name) return name;
295+
296+
if (names.length === 2 && count === 2) {
297+
return _t("%(user1)s and %(user2)s", {
298+
user1: names[0],
299+
user2: names[1],
300+
});
301+
}
302+
return _t("%(user)s and %(count)s others", {
303+
user: names[0],
304+
count: count - 1,
305+
});
306+
}
307+
308+
private inviteeNamesToRoomName(names: string[], count: number): string {
309+
const name = this.namesToRoomName(names, count);
310+
if (name) return name;
311+
312+
if (names.length === 2 && count === 2) {
313+
return _t("Inviting %(user1)s and %(user2)s", {
314+
user1: names[0],
315+
user2: names[1],
316+
});
317+
}
318+
return _t("Inviting %(user)s and %(count)s others", {
319+
user: names[0],
320+
count: count - 1,
321+
});
322+
}
323+
281324
private createClient(creds: IMatrixClientCreds): void {
282325
const opts: ICreateClientOpts = {
283326
baseUrl: creds.homeserverUrl,
@@ -299,16 +342,34 @@ class MatrixClientPegClass implements IMatrixClientPeg {
299342
verificationMethods.RECIPROCATE_QR_CODE,
300343
],
301344
identityServer: new IdentityAuthClient(),
302-
cryptoCallbacks: {},
345+
// These are always installed regardless of the labs flag so that cross-signing features
346+
// can toggle on without reloading and also be accessed immediately after login.
347+
cryptoCallbacks: { ...crossSigningCallbacks },
348+
roomNameGenerator: (_: string, state: RoomNameState) => {
349+
switch (state.type) {
350+
case RoomNameType.Generated:
351+
switch (state.subtype) {
352+
case "Inviting":
353+
return this.inviteeNamesToRoomName(state.names, state.count);
354+
default:
355+
return this.memberNamesToRoomName(state.names, state.count);
356+
}
357+
case RoomNameType.EmptyRoom:
358+
if (state.oldName) {
359+
return _t("Empty room (was %(oldName)s)", {
360+
oldName: state.oldName,
361+
});
362+
} else {
363+
return _t("Empty room");
364+
}
365+
default:
366+
return null;
367+
}
368+
},
303369
};
304370

305-
// These are always installed regardless of the labs flag so that
306-
// cross-signing features can toggle on without reloading and also be
307-
// accessed immediately after login.
308-
Object.assign(opts.cryptoCallbacks, crossSigningCallbacks);
309371
if (SecurityCustomisations.getDehydrationKey) {
310-
opts.cryptoCallbacks.getDehydrationKey =
311-
SecurityCustomisations.getDehydrationKey;
372+
opts.cryptoCallbacks!.getDehydrationKey = SecurityCustomisations.getDehydrationKey;
312373
}
313374

314375
this.matrixClient = createMatrixClient(opts);
@@ -319,7 +380,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
319380

320381
this.matrixClient.setGuest(Boolean(creds.guest));
321382

322-
const notifTimelineSet = new EventTimelineSet(null, {
383+
const notifTimelineSet = new EventTimelineSet(undefined, {
323384
timelineSupport: true,
324385
pendingEvents: false,
325386
});

src/i18n/strings/en_EN.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@
9797
"Try again": "Try again",
9898
"Your homeserver was unreachable and was not able to log you in. Please try again. If this continues, please contact your homeserver administrator.": "Your homeserver was unreachable and was not able to log you in. Please try again. If this continues, please contact your homeserver administrator.",
9999
"Your homeserver rejected your log in attempt. This could be due to things just taking too long. Please try again. If this continues, please contact your homeserver administrator.": "Your homeserver rejected your log in attempt. This could be due to things just taking too long. Please try again. If this continues, please contact your homeserver administrator.",
100+
"Empty room": "Empty room",
101+
"%(user1)s and %(user2)s": "%(user1)s and %(user2)s",
102+
"%(user)s and %(count)s others|other": "%(user)s and %(count)s others",
103+
"%(user)s and %(count)s others|one": "%(user)s and 1 other",
104+
"Inviting %(user1)s and %(user2)s": "Inviting %(user1)s and %(user2)s",
105+
"Inviting %(user)s and %(count)s others|other": "Inviting %(user)s and %(count)s others",
106+
"Inviting %(user)s and %(count)s others|one": "Inviting %(user)s and 1 other",
107+
"Empty room (was %(oldName)s)": "Empty room (was %(oldName)s)",
100108
"%(name)s is requesting verification": "%(name)s is requesting verification",
101109
"%(brand)s does not have permission to send you notifications - please check your browser settings": "%(brand)s does not have permission to send you notifications - please check your browser settings",
102110
"%(brand)s was not given permission to send notifications - please try again": "%(brand)s was not given permission to send notifications - please try again",
@@ -1882,7 +1890,6 @@
18821890
"System Alerts": "System Alerts",
18831891
"Historical": "Historical",
18841892
"Suggested Rooms": "Suggested Rooms",
1885-
"Empty room": "Empty room",
18861893
"Add space": "Add space",
18871894
"You do not have permissions to add spaces to this space": "You do not have permissions to add spaces to this space",
18881895
"Join public room": "Join public room",

0 commit comments

Comments
 (0)