fix more warnings, store transformed text in LRUCache of 1000
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
import { ReactNode, useEffect, useState } from "react";
|
||||
import { IntlProvider as ReactIntlProvider } from "react-intl";
|
||||
|
||||
import { DefaultLocale, useLocale } from "@/Components/IntlProvider/IntlProviderUtils";
|
||||
import { DefaultLocale } from "@/Components/IntlProvider/IntlProviderUtils";
|
||||
import {useLocale} from "@/Components/IntlProvider/useLocale";
|
||||
import enMessages from "@/translations/en.json";
|
||||
|
||||
const getMessages = (locale: string) => {
|
||||
|
@ -1,37 +1,5 @@
|
||||
import { ExternalStore } from "@snort/shared";
|
||||
import { useSyncExternalStore } from "react";
|
||||
|
||||
import useLogin from "@/Hooks/useLogin";
|
||||
|
||||
export const DefaultLocale = "en-US";
|
||||
|
||||
class LangStore extends ExternalStore<string | null> {
|
||||
setLang(s: string) {
|
||||
localStorage.setItem("lang", s);
|
||||
this.notifyChange();
|
||||
}
|
||||
|
||||
takeSnapshot() {
|
||||
return localStorage.getItem("lang");
|
||||
}
|
||||
}
|
||||
|
||||
const LangOverride = new LangStore();
|
||||
|
||||
export function useLocale() {
|
||||
const { language } = useLogin(s => ({ language: s.appData.item.preferences.language }));
|
||||
const loggedOutLang = useSyncExternalStore(
|
||||
c => LangOverride.hook(c),
|
||||
() => LangOverride.snapshot(),
|
||||
);
|
||||
const locale = language ?? loggedOutLang ?? getLocale();
|
||||
return {
|
||||
locale,
|
||||
lang: locale.toLowerCase().split(/[_-]+/)[0],
|
||||
setOverride: (s: string) => LangOverride.setLang(s),
|
||||
};
|
||||
}
|
||||
|
||||
export const getLocale = () => {
|
||||
return (navigator.languages && navigator.languages[0]) ?? navigator.language ?? DefaultLocale;
|
||||
};
|
||||
|
14
packages/app/src/Components/IntlProvider/langStore.tsx
Normal file
14
packages/app/src/Components/IntlProvider/langStore.tsx
Normal file
@ -0,0 +1,14 @@
|
||||
import {ExternalStore} from "@snort/shared";
|
||||
|
||||
class LangStore extends ExternalStore<string | null> {
|
||||
setLang(s: string) {
|
||||
localStorage.setItem("lang", s);
|
||||
this.notifyChange();
|
||||
}
|
||||
|
||||
takeSnapshot() {
|
||||
return localStorage.getItem("lang");
|
||||
}
|
||||
}
|
||||
|
||||
export const LangOverride = new LangStore();
|
19
packages/app/src/Components/IntlProvider/useLocale.tsx
Normal file
19
packages/app/src/Components/IntlProvider/useLocale.tsx
Normal file
@ -0,0 +1,19 @@
|
||||
import {useSyncExternalStore} from "react";
|
||||
|
||||
import {getLocale} from "@/Components/IntlProvider/IntlProviderUtils";
|
||||
import {LangOverride} from "@/Components/IntlProvider/langStore";
|
||||
import useLogin from "@/Hooks/useLogin";
|
||||
|
||||
export function useLocale() {
|
||||
const {language} = useLogin(s => ({language: s.appData.item.preferences.language}));
|
||||
const loggedOutLang = useSyncExternalStore(
|
||||
c => LangOverride.hook(c),
|
||||
() => LangOverride.snapshot(),
|
||||
);
|
||||
const locale = language ?? loggedOutLang ?? getLocale();
|
||||
return {
|
||||
locale,
|
||||
lang: locale.toLowerCase().split(/[_-]+/)[0],
|
||||
setOverride: (s: string) => LangOverride.setLang(s),
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user