Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 0 additions & 46 deletions web/src/labs/html2image/getFontsStyleElement.ts

This file was deleted.

52 changes: 52 additions & 0 deletions web/src/labs/i18n/createI18nStore.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
type I18nState = Readonly<{
locale: string;
}>;

type Listener = (ns: I18nState, ps?: I18nState) => void;

const createI18nStore = (preloadedState: I18nState) => {
const listeners: Listener[] = [];
let currentState = preloadedState;

const getState = () => {
return currentState;
};

const setState = (state: Partial<I18nState>) => {
const nextState = {
...currentState,
...state,
};
const prevState = currentState;
currentState = nextState;

for (const cb of listeners) {
cb(currentState, prevState);
}
};

const subscribe = (listener: Listener) => {
let isSubscribed = true;
listeners.push(listener);

const unsubscribe = () => {
if (!isSubscribed) {
return;
}

const index = listeners.indexOf(listener);
listeners.splice(index, 1);
isSubscribed = false;
};

return unsubscribe;
};

return {
getState,
setState,
subscribe,
};
};

export default createI18nStore;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

51 changes: 1 addition & 50 deletions web/src/labs/i18n/i18nStore.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,4 @@
type I18nState = Readonly<{
locale: string;
}>;

type Listener = (ns: I18nState, ps?: I18nState) => void;

const createI18nStore = (preloadedState: I18nState) => {
const listeners: Listener[] = [];
let currentState = preloadedState;

const getState = () => {
return currentState;
};

const setState = (state: Partial<I18nState>) => {
const nextState = {
...currentState,
...state,
};
const prevState = currentState;
currentState = nextState;

for (const cb of listeners) {
cb(currentState, prevState);
}
};

const subscribe = (listener: Listener) => {
let isSubscribed = true;
listeners.push(listener);

const unsubscribe = () => {
if (!isSubscribed) {
return;
}

const index = listeners.indexOf(listener);
listeners.splice(index, 1);
isSubscribed = false;
};

return unsubscribe;
};

return {
getState,
setState,
subscribe,
};
};
import createI18nStore from "./createI18nStore";

const defaultI18nState = {
locale: "en",
Expand Down
3 changes: 2 additions & 1 deletion web/src/labs/i18n/useI18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const useI18n = () => {

const translate = (key: string) => {
try {
return resources[locale][key] as string;
const value = resources[locale][key] as string;
return value;
} catch (error) {
return key;
}
Expand Down