try catch getIntlMessageFromHash

This commit is contained in:
Nuckyz 2024-11-03 16:18:33 -03:00
parent 4cf5b58f9b
commit e4aba774e0
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
4 changed files with 42 additions and 28 deletions

View file

@ -275,12 +275,16 @@ export default definePlugin({
},
makeGuildsBarGuildListFilter(isBetterFolders: boolean) {
return child => {
if (isBetterFolders) {
return child?.props?.["aria-label"] === getIntlMessage("SERVERS");
}
try {
return child => {
if (isBetterFolders) {
return child?.props?.["aria-label"] === getIntlMessage("SERVERS");
}
return true;
};
} catch {
return true;
};
}
},
makeGuildsBarTreeFilter(isBetterFolders: boolean) {

View file

@ -192,9 +192,9 @@ export default definePlugin({
replacement: [
// make the tag show the right text
{
match: /(switch\((\i)\){.+?)case (\i(?:\.\i)?)\.BOT:default:(\i)=.{0,40}#{intl::APP_TAG}"\]\)/,
replace: (_, origSwitch, variant, tags, displayedText) =>
`${origSwitch}default:{${displayedText} = $self.getTagText(${tags}[${variant}])}`
match: /(switch\((\i)\){.+?)case (\i(?:\.\i)?)\.BOT:default:(\i)=(.{0,40}#{intl::APP_TAG}"\]\))/,
replace: (_, origSwitch, variant, tags, displayedText, originalText) =>
`${origSwitch}default:{${displayedText} = $self.getTagText(${tags}[${variant}],${originalText})}`
},
// show OP tags correctly
{
@ -296,21 +296,25 @@ export default definePlugin({
isOPTag: (tag: number) => tag === Tag.Types.ORIGINAL_POSTER || tags.some(t => tag === Tag.Types[`${t.name}-OP`]),
getTagText(passedTagName: string) {
if (!passedTagName) return getIntlMessage("APP_TAG");
const [tagName, variant] = passedTagName.split("-");
const tag = tags.find(({ name }) => tagName === name);
if (!tag) return getIntlMessage("APP_TAG");
if (variant === "BOT" && tagName !== "WEBHOOK" && this.settings.store.dontShowForBots) return getIntlMessage("APP_TAG");
getTagText(passedTagName: string, originalText: string) {
try {
const [tagName, variant] = passedTagName.split("-");
if (!passedTagName) return getIntlMessage("APP_TAG");
const tag = tags.find(({ name }) => tagName === name);
if (!tag) return getIntlMessage("APP_TAG");
if (variant === "BOT" && tagName !== "WEBHOOK" && this.settings.store.dontShowForBots) return getIntlMessage("APP_TAG");
const tagText = settings.store.tagSettings?.[tag.name]?.text || tag.displayName;
switch (variant) {
case "OP":
return `${getIntlMessage("BOT_TAG_FORUM_ORIGINAL_POSTER")}${tagText}`;
case "BOT":
return `${getIntlMessage("APP_TAG")}${tagText}`;
default:
return tagText;
const tagText = settings.store.tagSettings?.[tag.name]?.text || tag.displayName;
switch (variant) {
case "OP":
return `${getIntlMessage("BOT_TAG_FORUM_ORIGINAL_POSTER")}${tagText}`;
case "BOT":
return `${getIntlMessage("APP_TAG")}${tagText}`;
default:
return tagText;
}
} catch {
return originalText;
}
},

View file

@ -73,8 +73,6 @@ function TypingIndicator({ channelId }: { channelId: string; }) {
const typingUsersArray = Object.keys(typingUsers).filter(id => id !== myId && !(RelationshipStore.isBlocked(id) && !settings.store.includeBlockedUsers));
let tooltipText: string;
// the new syntax is getIntlMessage("ONE_USER_TYPING", { a: getDisplayName(guildId, typingUsersArray[0]) });
switch (typingUsersArray.length) {
case 0: break;
case 1: {

View file

@ -24,24 +24,32 @@ import { Channel, Guild, Message, User } from "discord-types/general";
import { Except } from "type-fest";
import { runtimeHashMessageKey } from "./intlHash";
import { Logger } from "./Logger";
import { MediaModalItem, MediaModalProps, openMediaModal } from "./modal";
const IntlManagerLogger = new Logger("IntlManager");
/**
* Get an internationalized message from a non hashed key
* @param key The plain message key
* @param values The values to interpolate, if it's a rich message
*/
export function getIntlMessage(key: string, values?: Record<PropertyKey, any>): any {
return getIntlMessageFromHash(runtimeHashMessageKey(key), values);
return getIntlMessageFromHash(runtimeHashMessageKey(key), values, key);
}
/**
* Get an internationalized message from a hashed key
* @param key The hashed message key
* @param hashedKey The hashed message key
* @param values The values to interpolate, if it's a rich message
*/
export function getIntlMessageFromHash(key: string, values?: Record<PropertyKey, any>): any {
return values == null ? i18n.intl.string(i18n.t[key]) : i18n.intl.format(i18n.t[key], values);
export function getIntlMessageFromHash(hashedKey: string, values?: Record<PropertyKey, any>, originalKey?: string): any {
try {
return values == null ? i18n.intl.string(i18n.t[hashedKey]) : i18n.intl.format(i18n.t[hashedKey], values);
} catch (e) {
IntlManagerLogger.error(`Failed to get intl message for key: ${originalKey ?? hashedKey}`, e);
return originalKey ?? "";
}
}
/**