Skip to content

Commit

Permalink
Begin implementing i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Mar 8, 2024
1 parent e863a6e commit d158dd6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
40 changes: 40 additions & 0 deletions src/core/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FluxDispatcher } from "@lib/metro/common";
import { findByName } from "@lib/metro/filters";
import { PrimitiveType } from "intl-messageformat";

Expand All @@ -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> {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -31,6 +32,7 @@ export default async () => {
_patchAssets(),
_patchCommands(),
_patchChatBackground(),
initFetchI18nStrings(),
initSettings(),
initFixes(),
initSafeMode(),
Expand Down
4 changes: 2 additions & 2 deletions src/lib/metro/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit d158dd6

Please sign in to comment.