-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load custom i18n strings for plugins
- Loading branch information
1 parent
5ad36fe
commit dd89808
Showing
5 changed files
with
62 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,58 @@ | ||
import type { I18n } from "@common"; | ||
import { loadAllMessagesInLocale } from "@discord/intl"; | ||
import type { loadAllMessagesInLocale as LoadAllMessagesInLocale } from "@discord/intl"; | ||
import { waitForProps } from "@webpack"; | ||
import { DEFAULT_LOCALE } from "src/constants"; | ||
import messages from "../../../i18n/en-US.messages"; | ||
import type * as definitions from "../../../i18n/en-US.messages"; | ||
|
||
export let locale: string | undefined; | ||
export const t = messages; | ||
export let t: typeof definitions.default; | ||
export let messagesLoader: typeof definitions.messagesLoader; | ||
|
||
export async function load(): Promise<void> { | ||
const { intl } = await waitForProps<I18n>("getAvailableLocales", "intl"); | ||
|
||
// ! HACK: This is a workaround until ignition issues are fixed. | ||
// We need to delay the import of the messages for intl to be loaded and use that module instead of @discord/intl directly. | ||
const { default: messages, messagesLoader: loader } = await import( | ||
"../../../i18n/en-US.messages" | ||
); | ||
t = messages; | ||
messagesLoader = loader; | ||
|
||
locale = intl.currentLocale || intl.defaultLocale || DEFAULT_LOCALE; | ||
|
||
intl.onLocaleChange((newLocale) => { | ||
locale = newLocale; | ||
addRepluggedStrings(); | ||
void addRepluggedStrings(); | ||
}); | ||
void addRepluggedStrings(); | ||
} | ||
|
||
export function addRepluggedStrings(): void { | ||
export async function addRepluggedStrings(): Promise<void> { | ||
const { loadAllMessagesInLocale } = await waitForProps<{ | ||
loadAllMessagesInLocale: typeof LoadAllMessagesInLocale; | ||
}>("loadAllMessagesInLocale"); | ||
|
||
if (locale) { | ||
void loadAllMessagesInLocale(locale); | ||
} | ||
} | ||
|
||
/** | ||
* Load custom strings into Replugged's translations, typically for plugins. | ||
*/ | ||
export function loadAllStrings(translations: Record<string, object>): void { | ||
Object.keys(translations).forEach(async (locale) => { | ||
const { default: originalStrings } = await messagesLoader.localeImportMap[locale](); | ||
const strings = translations[locale]; | ||
const messages = { ...strings, ...originalStrings }; | ||
|
||
if (locale in messagesLoader.messages) messagesLoader.messages[locale] = messages; | ||
messagesLoader.localeImportMap[locale] = () => | ||
new Promise((resolve) => { | ||
resolve({ default: messages }); | ||
}); | ||
messagesLoader.messageKeys = [...messagesLoader.messageKeys, ...Object.keys(strings)]; | ||
}); | ||
void addRepluggedStrings(); | ||
} |