mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update i18nStore
(#141)
This commit is contained in:
@@ -1,46 +0,0 @@
|
|||||||
import convertResourceToDataURL from "./convertResourceToDataURL";
|
|
||||||
|
|
||||||
const getFontsStyleElement = async (element: HTMLElement) => {
|
|
||||||
const styleSheets = element.ownerDocument.styleSheets;
|
|
||||||
const fontFamilyStyles: CSSStyleDeclaration[] = [];
|
|
||||||
|
|
||||||
for (const sheet of styleSheets) {
|
|
||||||
for (const rule of sheet.cssRules) {
|
|
||||||
if (rule.constructor.name === "CSSFontFaceRule") {
|
|
||||||
fontFamilyStyles.push((rule as CSSFontFaceRule).style);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const styleElement = document.createElement("style");
|
|
||||||
|
|
||||||
for (const f of fontFamilyStyles) {
|
|
||||||
const fontFamily = f.getPropertyValue("font-family");
|
|
||||||
const fontWeight = f.getPropertyValue("font-weight");
|
|
||||||
const src = f.getPropertyValue("src");
|
|
||||||
const resourceUrls = src.split(",").map((s) => {
|
|
||||||
return s.replace(/url\("?(.+?)"?\)/, "$1");
|
|
||||||
});
|
|
||||||
const base64Urls: string[] = [];
|
|
||||||
|
|
||||||
for (const url of resourceUrls) {
|
|
||||||
try {
|
|
||||||
const base64Url = await convertResourceToDataURL(url);
|
|
||||||
base64Urls.push(`url("${base64Url}")`);
|
|
||||||
} catch (error) {
|
|
||||||
// do nth
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
styleElement.innerHTML += `
|
|
||||||
@font-face {
|
|
||||||
font-family: "${fontFamily}";
|
|
||||||
src: ${base64Urls.join(",")};
|
|
||||||
font-weight: ${fontWeight};
|
|
||||||
}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return styleElement;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getFontsStyleElement;
|
|
52
web/src/labs/i18n/createI18nStore.tsx
Normal file
52
web/src/labs/i18n/createI18nStore.tsx
Normal file
@@ -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;
|
@@ -1,53 +1,4 @@
|
|||||||
type I18nState = Readonly<{
|
import createI18nStore from "./createI18nStore";
|
||||||
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,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const defaultI18nState = {
|
const defaultI18nState = {
|
||||||
locale: "en",
|
locale: "en",
|
||||||
|
@@ -25,7 +25,8 @@ const useI18n = () => {
|
|||||||
|
|
||||||
const translate = (key: string) => {
|
const translate = (key: string) => {
|
||||||
try {
|
try {
|
||||||
return resources[locale][key] as string;
|
const value = resources[locale][key] as string;
|
||||||
|
return value;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user