Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of js-sdk roomNameGenerator to handle i18n for generated room names #9209

Merged
merged 8 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion src/MatrixClientPeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

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

export interface IMatrixClientCreds {
homeserverUrl: string;
Expand Down Expand Up @@ -278,6 +279,46 @@ class MatrixClientPegClass implements IMatrixClientPeg {
return matches[1];
}

private memberNamesToRoomName(names: string[], count: number): string {
const countWithoutMe = count - 1;
if (!names.length) {
return _t("Empty room");
}
if (names.length === 1 && countWithoutMe <= 1) {
return names[0];
}
if (names.length === 2 && countWithoutMe <= 2) {
return _t("%(user1)s and %(user2)s", {
user1: names[0],
user2: names[1],
});
}
return _t("%(user)s and %(count)s others", {
user: names[0],
count: countWithoutMe,
});
}

private inviteeNamesToRoomName(names: string[], count: number): string {
const countWithoutMe = count - 1;
if (!names.length) {
return _t("Empty room");
}
if (names.length === 1 && countWithoutMe <= 1) {
return names[0];
}
if (names.length === 2 && countWithoutMe <= 2) {
return _t("Inviting %(user1)s and %(user2)s", {
user1: names[0],
user2: names[1],
});
}
return _t("Inviting %(user)s and %(count)s others", {
user: names[0],
count: countWithoutMe,
});
}

private createClient(creds: IMatrixClientCreds): void {
const opts: ICreateClientOpts = {
baseUrl: creds.homeserverUrl,
Expand All @@ -300,6 +341,27 @@ class MatrixClientPegClass implements IMatrixClientPeg {
],
identityServer: new IdentityAuthClient(),
cryptoCallbacks: {},
roomNameGenerator: (_: string, state: RoomNameState) => {
switch (state.type) {
case RoomNameType.Generated:
switch (state.subtype) {
case "Inviting":
return this.inviteeNamesToRoomName(state.names, state.count);
default:
return this.memberNamesToRoomName(state.names, state.count);
}
case RoomNameType.EmptyRoom:
if (state.oldName) {
return _t("Empty room (was %(oldName)s)", {
oldName: state.oldName,
});
} else {
return _t("Empty room");
}
default:
return null;
}
},
};

// These are always installed regardless of the labs flag so that
Expand Down
9 changes: 8 additions & 1 deletion src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@
"Try again": "Try again",
"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.",
"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.",
"Empty room": "Empty room",
"%(user1)s and %(user2)s": "%(user1)s and %(user2)s",
"%(user)s and %(count)s others|other": "%(user)s and %(count)s others",
"%(user)s and %(count)s others|one": "%(user)s and 1 other",
"Inviting %(user1)s and %(user2)s": "Inviting %(user1)s and %(user2)s",
"Inviting %(user)s and %(count)s others|other": "Inviting %(user)s and %(count)s others",
"Inviting %(user)s and %(count)s others|one": "Inviting %(user)s and 1 other",
"Empty room (was %(oldName)s)": "Empty room (was %(oldName)s)",
"%(name)s is requesting verification": "%(name)s is requesting verification",
"%(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",
"%(brand)s was not given permission to send notifications - please try again": "%(brand)s was not given permission to send notifications - please try again",
Expand Down Expand Up @@ -1869,7 +1877,6 @@
"System Alerts": "System Alerts",
"Historical": "Historical",
"Suggested Rooms": "Suggested Rooms",
"Empty room": "Empty room",
"Add space": "Add space",
"You do not have permissions to add spaces to this space": "You do not have permissions to add spaces to this space",
"Join public room": "Join public room",
Expand Down