chore: Clean up src/i18n.ts changes

This commit is contained in:
Daniel J. Geiger 2023-12-02 19:07:48 -06:00
parent 1dfadb4d26
commit ce595ff18c
4 changed files with 31 additions and 64 deletions

View File

@ -3,7 +3,7 @@ import { ExcalidrawElement, ExcalidrawTextElement, NonDeleted } from "../types";
import { getNonDeletedElements } from "../"; import { getNonDeletedElements } from "../";
import { getSelectedElements } from "../../scene"; import { getSelectedElements } from "../../scene";
import { AppState, ExcalidrawImperativeAPI, ToolType } from "../../types"; import { AppState, ExcalidrawImperativeAPI, ToolType } from "../../types";
import { registerAuxLangData } from "../../i18n"; import { LangLdr, registerCustomLangData } from "../../i18n";
import { import {
Action, Action,
@ -331,10 +331,7 @@ export type SubtypeCheckFn = (element: ExcalidrawElement) => boolean;
// Functions to prepare subtypes for use // Functions to prepare subtypes for use
export type SubtypePrepFn = ( export type SubtypePrepFn = (
addSubtypeAction: (action: Action) => void, addSubtypeAction: (action: Action) => void,
addLangData: ( addLangData: (fallbackLangData: {}, setLanguageAux: LangLdr) => void,
fallbackLangData: Object,
setLanguageAux: (langCode: string) => Promise<Object | undefined>,
) => void,
onSubtypeLoaded?: SubtypeLoadedCb, onSubtypeLoaded?: SubtypeLoadedCb,
) => { ) => {
actions: Action[]; actions: Action[];
@ -411,7 +408,7 @@ export const prepareSubtype = (
// Prepare the subtype // Prepare the subtype
const { actions, methods } = subtypePrepFn( const { actions, methods } = subtypePrepFn(
addSubtypeAction, addSubtypeAction,
registerAuxLangData, registerCustomLangData,
onSubtypeLoaded, onSubtypeLoaded,
); );

View File

@ -25,7 +25,7 @@ import { getElementAbsoluteCoords } from "../../../element/bounds";
import Scene from "../../../scene/Scene"; import Scene from "../../../scene/Scene";
// Imports for actions // Imports for actions
import { t, registerAuxLangData } from "../../../i18n"; import { LangLdr, registerCustomLangData, t } from "../../../i18n";
import { Action, makeCustomActionName } from "../../../actions/types"; import { Action, makeCustomActionName } from "../../../actions/types";
import { AppState } from "../../../types"; import { AppState } from "../../../types";
import { import {
@ -1611,19 +1611,12 @@ export const prepareMathSubtype = function (
methods.render = renderMathElement; methods.render = renderMathElement;
methods.renderSvg = renderSvgMathElement; methods.renderSvg = renderSvgMathElement;
methods.wrapText = wrapMathElement; methods.wrapText = wrapMathElement;
const getLangData = async (langCode: string): Promise<Object | undefined> => { const getLangData: LangLdr = (langCode) =>
try { import(
const condData = await import(
/* webpackChunkName: "locales/[request]" */ `./locales/${langCode}.json` /* webpackChunkName: "locales/[request]" */ `./locales/${langCode}.json`
); );
if (condData) {
return condData;
}
} catch (e) {}
return undefined;
};
addLangData(fallbackMathJaxLangData, getLangData); addLangData(fallbackMathJaxLangData, getLangData);
registerAuxLangData(fallbackMathJaxLangData, getLangData); registerCustomLangData(fallbackMathJaxLangData, getLangData);
const actions = createMathActions(); const actions = createMathActions();
actions.forEach((action) => addSubtypeAction(action)); actions.forEach((action) => addSubtypeAction(action));

View File

@ -87,20 +87,15 @@ if (import.meta.env.DEV) {
let currentLang: Language = defaultLang; let currentLang: Language = defaultLang;
let currentLangData = {}; let currentLangData = {};
const auxCurrentLangData = Array<Object>(); let fallbackCustomLangData = {};
const auxFallbackLangData = Array<Object>(); const langLoaders: LangLdr[] = [];
const auxSetLanguageFuncs = export type LangLdr = (langCode: string) => Promise<{}>;
Array<(langCode: string) => Promise<Object | undefined>>();
export const registerAuxLangData = ( export const registerCustomLangData = (fallbackLangData: {}, ldr: LangLdr) => {
fallbackLangData: Object, if (!langLoaders.includes(ldr)) {
setLanguageAux: (langCode: string) => Promise<Object | undefined>, fallbackCustomLangData = { ...fallbackLangData, ...fallbackCustomLangData };
) => { langLoaders.push(ldr);
if (auxFallbackLangData.includes(fallbackLangData)) {
return;
} }
auxFallbackLangData.push(fallbackLangData);
auxSetLanguageFuncs.push(setLanguageAux);
}; };
export const setLanguage = async (lang: Language) => { export const setLanguage = async (lang: Language) => {
@ -115,21 +110,18 @@ export const setLanguage = async (lang: Language) => {
currentLangData = await import( currentLangData = await import(
/* webpackChunkName: "locales/[request]" */ `./locales/${currentLang.code}.json` /* webpackChunkName: "locales/[request]" */ `./locales/${currentLang.code}.json`
); );
// Empty the auxCurrentLangData array
while (auxCurrentLangData.length > 0) {
auxCurrentLangData.pop();
}
// Fill the auxCurrentLangData array with each locale file found in auxLangDataRoots for this language
auxSetLanguageFuncs.forEach(async (setLanguageFn) => {
const condData = await setLanguageFn(currentLang.code);
if (condData) {
auxCurrentLangData.push(condData);
}
});
} catch (error: any) { } catch (error: any) {
console.error(`Failed to load language ${lang.code}:`, error.message); console.error(`Failed to load language ${lang.code}:`, error.message);
currentLangData = fallbackLangData; currentLangData = fallbackLangData;
} }
const auxData = langLoaders.map((fn) => fn(currentLang.code));
while (auxData.length > 0) {
try {
currentLangData = { ...(await auxData.pop()), ...currentLangData };
} catch (error: any) {
console.error(`Error loading ${lang.code} extra data:`, error.message);
}
}
} }
jotaiStore.set(editorLangCodeAtom, lang.code); jotaiStore.set(editorLangCodeAtom, lang.code);
@ -169,14 +161,8 @@ export const t = (
let translation = let translation =
findPartsForData(currentLangData, parts) || findPartsForData(currentLangData, parts) ||
findPartsForData(fallbackLangData, parts) || findPartsForData(fallbackLangData, parts) ||
findPartsForData(fallbackCustomLangData, parts) ||
fallback; fallback;
const auxData = Array<Object>().concat(
auxCurrentLangData,
auxFallbackLangData,
);
for (let i = 0; i < auxData.length; i++) {
translation = translation || findPartsForData(auxData[i], parts);
}
if (translation === undefined) { if (translation === undefined) {
const errorMessage = `Can't find translation for ${path}`; const errorMessage = `Can't find translation for ${path}`;
// in production, don't blow up the app on a missing translation key // in production, don't blow up the app on a missing translation key

View File

@ -27,7 +27,7 @@ import {
} from "../element/types"; } from "../element/types";
import { createIcon, iconFillColor } from "../components/icons"; import { createIcon, iconFillColor } from "../components/icons";
import { SubtypeButton } from "../components/Subtypes"; import { SubtypeButton } from "../components/Subtypes";
import { registerAuxLangData } from "../i18n"; import { LangLdr, registerCustomLangData } from "../i18n";
import { getFontString, getShortcutKey } from "../utils"; import { getFontString, getShortcutKey } from "../utils";
import * as textElementUtils from "../element/textElement"; import * as textElementUtils from "../element/textElement";
import { isTextElement } from "../element"; import { isTextElement } from "../element";
@ -46,17 +46,8 @@ const FONTSIZE = 20;
const DBFONTSIZE = 40; const DBFONTSIZE = 40;
const TRFONTSIZE = 60; const TRFONTSIZE = 60;
const getLangData = async (langCode: string): Promise<Object | undefined> => { const getLangData: LangLdr = (langCode) =>
try { import(`./helpers/locales/${langCode}.json`);
const condData = await import(
/* webpackChunkName: "locales/[request]" */ `./helpers/locales/${langCode}.json`
);
if (condData) {
return condData;
}
} catch (e) {}
return undefined;
};
const testSubtypeIcon = ({ theme }: { theme: Theme }) => const testSubtypeIcon = ({ theme }: { theme: Theme }) =>
createIcon( createIcon(
@ -155,7 +146,7 @@ const prepareTest1Subtype = function (
methods.clean = cleanTestElementUpdate; methods.clean = cleanTestElementUpdate;
addLangData(fallbackLangData, getLangData); addLangData(fallbackLangData, getLangData);
registerAuxLangData(fallbackLangData, getLangData); registerCustomLangData(fallbackLangData, getLangData);
const actions = [testAction, test1Button]; const actions = [testAction, test1Button];
actions.forEach((action) => addSubtypeAction(action)); actions.forEach((action) => addSubtypeAction(action));
@ -223,7 +214,7 @@ const prepareTest2Subtype = function (
} as SubtypeMethods; } as SubtypeMethods;
addLangData(fallbackLangData, getLangData); addLangData(fallbackLangData, getLangData);
registerAuxLangData(fallbackLangData, getLangData); registerCustomLangData(fallbackLangData, getLangData);
const actions = [test2Button]; const actions = [test2Button];
actions.forEach((action) => addSubtypeAction(action)); actions.forEach((action) => addSubtypeAction(action));
@ -241,7 +232,7 @@ const prepareTest3Subtype = function (
const methods = {} as SubtypeMethods; const methods = {} as SubtypeMethods;
addLangData(fallbackLangData, getLangData); addLangData(fallbackLangData, getLangData);
registerAuxLangData(fallbackLangData, getLangData); registerCustomLangData(fallbackLangData, getLangData);
const actions = [test3Button]; const actions = [test3Button];
actions.forEach((action) => addSubtypeAction(action)); actions.forEach((action) => addSubtypeAction(action));