Begin implementing i18n
This commit is contained in:
parent
e863a6eaca
commit
d158dd69e9
3 changed files with 44 additions and 2 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { FluxDispatcher } from "@lib/metro/common";
|
||||
import { findByName } from "@lib/metro/filters";
|
||||
import { PrimitiveType } from "intl-messageformat";
|
||||
|
||||
|
|
@ -7,12 +8,51 @@ const IntlMessageFormat = findByName("MessageFormat") as typeof import("intl-mes
|
|||
|
||||
type I18nKey = keyof typeof langDefault;
|
||||
|
||||
let currentLocale: string | null;
|
||||
const _loadedLocale = new Set<string>();
|
||||
const _loadedStrings = {} as Record<string, typeof langDefault>;
|
||||
|
||||
export const Strings = new Proxy({}, {
|
||||
get: (_t, prop: keyof typeof langDefault) => {
|
||||
if (currentLocale && _loadedStrings[currentLocale]?.[prop]) {
|
||||
return _loadedStrings[currentLocale]?.[prop];
|
||||
}
|
||||
return langDefault[prop];
|
||||
}
|
||||
}) as Record<I18nKey, string>;
|
||||
|
||||
export function initFetchI18nStrings() {
|
||||
const cb = ({ locale }: { locale: string; }) => {
|
||||
const languageMap = {
|
||||
"es-ES": "es",
|
||||
"es-419": "es_419",
|
||||
"zh-TW": "zh-Hant",
|
||||
"zh-CN": "zh-Hans",
|
||||
"pt-PT": "pt",
|
||||
"sv-SE": "sv"
|
||||
} as Record<string, string>;
|
||||
|
||||
const resolvedLocale = languageMap[locale] ?? locale;
|
||||
if (resolvedLocale.startsWith("en-")) {
|
||||
currentLocale = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_loadedLocale.has(resolvedLocale)) {
|
||||
_loadedLocale.add(resolvedLocale);
|
||||
|
||||
fetch(`https://raw.githubusercontent.com/pyoncord/i18n/main/resources/${resolvedLocale}/bunny.json`)
|
||||
.then(r => r.json())
|
||||
.then(strings => _loadedStrings[resolvedLocale] = strings)
|
||||
.then(() => currentLocale = resolvedLocale)
|
||||
.catch(e => console.error(`An error occured while fetching strings for ${resolvedLocale}: ${e}`));
|
||||
}
|
||||
};
|
||||
|
||||
FluxDispatcher.subscribe("I18N_LOAD_SUCCESS", cb);
|
||||
return () => FluxDispatcher.unsubscribe("I18N_LOAD_SUCCESS", cb);
|
||||
}
|
||||
|
||||
type FormatStringRet<T> = T extends PrimitiveType ? string : string | T | (string | T)[];
|
||||
|
||||
export function formatString<T = void>(key: I18nKey, val: Record<string, T>): FormatStringRet<T> {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import initFixes from "@core/fixes";
|
||||
import { initFetchI18nStrings } from "@core/i18n";
|
||||
import { initCorePlugins } from "@core/plugins";
|
||||
import initSettings from "@core/ui/settings";
|
||||
import { _patchAssets } from "@lib/api/assets";
|
||||
|
|
@ -31,6 +32,7 @@ export default async () => {
|
|||
_patchAssets(),
|
||||
_patchCommands(),
|
||||
_patchChatBackground(),
|
||||
initFetchI18nStrings(),
|
||||
initSettings(),
|
||||
initFixes(),
|
||||
initSafeMode(),
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ export interface Dispatcher {
|
|||
isDispatching(): boolean;
|
||||
register(name: string, actionHandler: Record<string, (e: any) => void>, storeDidChange: (e: any) => boolean): string;
|
||||
setInterceptor(interceptor?: (payload: any) => void | boolean): void;
|
||||
subscribe(actionType: string, callback: (payload: any) => void): () => void;
|
||||
unsubscribe(actionType: string, callback: (payload: any) => void): () => void;
|
||||
subscribe(actionType: string, callback: (payload: any) => void): void;
|
||||
unsubscribe(actionType: string, callback: (payload: any) => void): void;
|
||||
wait(cb: () => void): void;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue