feat: top level hax

This commit is contained in:
Lewis Crichton 2024-06-09 21:47:03 +01:00
parent a4d4d981e0
commit ef028edc0d
No known key found for this signature in database
3 changed files with 28 additions and 17 deletions

View file

@ -33,7 +33,6 @@ import { Margins } from "@utils/margins";
import { classes, isObjectEmpty } from "@utils/misc"; import { classes, isObjectEmpty } from "@utils/misc";
import { openModalLazy } from "@utils/modal"; import { openModalLazy } from "@utils/modal";
import { useAwaiter } from "@utils/react"; import { useAwaiter } from "@utils/react";
import { lowercaseify } from "@utils/text";
import { $t } from "@utils/translation"; import { $t } from "@utils/translation";
import { Plugin } from "@utils/types"; import { Plugin } from "@utils/types";
import { findByPropsLazy } from "@webpack"; import { findByPropsLazy } from "@webpack";
@ -154,7 +153,7 @@ export function PluginCard({ plugin, disabled, onRestartNeeded, onMouseEnter, on
return ( return (
<AddonCard <AddonCard
name={plugin.name} name={plugin.name}
description={$t(`${lowercaseify(plugin.name)}.description`)} description={plugin.description}
isNew={isNew} isNew={isNew}
enabled={isEnabled()} enabled={isEnabled()}
setEnabled={toggleEnabled} setEnabled={toggleEnabled}

View file

@ -37,8 +37,6 @@ export const wordsToPascal = (words: string[]) =>
export const wordsToTitle = (words: string[]) => export const wordsToTitle = (words: string[]) =>
words.map(w => w[0].toUpperCase() + w.slice(1)).join(" "); words.map(w => w[0].toUpperCase() + w.slice(1)).join(" ");
export const lowercaseify = (text: string) => text[0].toLowerCase() + text.slice(1);
const units = ["years", "months", "weeks", "days", "hours", "minutes", "seconds"] as const; const units = ["years", "months", "weeks", "days", "hours", "minutes", "seconds"] as const;
type Units = typeof units[number]; type Units = typeof units[number];

View file

@ -107,6 +107,7 @@ function _t(key: string, bundle: any): Translation {
* @returns A translated string. * @returns A translated string.
*/ */
export function $t(key: string, variables?: Record<string, any>): string { export function $t(key: string, variables?: Record<string, any>): string {
const getter = (): string => {
const translation = _t(key, loadedLocale); const translation = _t(key, loadedLocale);
if (typeof translation !== "string") { if (typeof translation !== "string") {
@ -127,6 +128,19 @@ export function $t(key: string, variables?: Record<string, any>): string {
if (!variables) return translation as string; if (!variables) return translation as string;
return format(translation as string, variables); return format(translation as string, variables);
};
// top level support hax (thank you vee!!)
// tl;dr: this lets you use $t at the top level in objects by simulating a string, a la:
// {
// description: $t("clientTheme.description")
// }
// and any future accesses of the description prop will result in an up to date translation
return {
__proto__: String.prototype,
valueOf: getter,
toString: getter
} as unknown as string;
} }
interface TranslateProps { interface TranslateProps {