Files
Folo/apps/renderer/src/i18n.ts
T
3a85febb6e refactor(monorepo): migrate to monorepo (#1059)
* init

Signed-off-by: Innei <i@innei.in>

* chore: utils import

Signed-off-by: Innei <tukon479@gmail.com>

* remove stable router

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: components import

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

* minify

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* fix: test

Signed-off-by: Innei <tukon479@gmail.com>

* chore: ui component extract

Signed-off-by: Innei <tukon479@gmail.com>

* fix: import

Signed-off-by: Innei <tukon479@gmail.com>

* re-render

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: remove hooks

Signed-off-by: Innei <tukon479@gmail.com>

* fix

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* setting

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: remove icons

Signed-off-by: Innei <i@innei.in>

* typings

Signed-off-by: Innei <i@innei.in>

* feat: add horizontal scroll functionality to TimelineTabs (#1081)

Co-authored-by: xhuajin <xhuajin@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>

* fix: Wrong initial position of tab animation (#1087)

Co-authored-by: hamster1963 <hamster1963@users.noreply.github.com>

* fix: disable auto toggle readability in web

* fix: margin condition in EntryListHeader (#1103)

* chore(i18n): add daily reward system translation (#1102)

Co-authored-by: lc4t <lc4t@users.noreply.github.com>

* fix: deps

Signed-off-by: Innei <i@innei.in>

* fix: type

Signed-off-by: Innei <i@innei.in>

* update

Signed-off-by: Innei <i@innei.in>

* fix

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
Signed-off-by: Innei <tukon479@gmail.com>
Co-authored-by: Innei <Innei@users.noreply.github.com>
Co-authored-by: Huajin <114134574+xhuajin@users.noreply.github.com>
Co-authored-by: xhuajin <xhuajin@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: 仓鼠 <71394853+hamster1963@users.noreply.github.com>
Co-authored-by: hamster1963 <hamster1963@users.noreply.github.com>
Co-authored-by: Konv Suu <2583695112@qq.com>
Co-authored-by: lc4t <11334449+lc4t@users.noreply.github.com>
Co-authored-by: lc4t <lc4t@users.noreply.github.com>
2024-10-24 14:37:43 +08:00

96 lines
2.5 KiB
TypeScript

import { Chain } from "@follow/utils/chain"
import { EventBus } from "@follow/utils/event-bus"
import { getStorageNS } from "@follow/utils/ns"
import i18next from "i18next"
import { atom } from "jotai"
import { initReactI18next } from "react-i18next"
import { defaultNS, ns } from "./@types/constants"
import { defaultResources } from "./@types/default-resource"
import { getGeneralSettings } from "./atoms/settings/general"
import { isDev } from "./constants"
import { jotaiStore } from "./lib/jotai"
export const i18nAtom = atom(i18next)
export const langChain = new Chain()
export class LocaleCache {
static shared = new LocaleCache()
private getKey(lang: string) {
return getStorageNS(`locale-${lang}`)
}
get(lang: string) {
const key = this.getKey(lang)
const cache = localStorage.getItem(key)
if (!cache) return null
return JSON.parse(cache)
}
set(lang: string) {
const key = this.getKey(lang)
const mergedResources = {} as any
for (const nsKey of ns) {
const nsResources = i18next.getResourceBundle(lang, nsKey)
mergedResources[nsKey] = nsResources
}
localStorage.setItem(key, JSON.stringify(mergedResources))
}
}
export const fallbackLanguage = "en"
export const initI18n = async () => {
const i18next = jotaiStore.get(i18nAtom)
const lang = getGeneralSettings().language
const mergedResources = {
...defaultResources,
}
let cache = null as any
if (!isDev) {
cache = LocaleCache.shared.get(lang)
if (cache) {
mergedResources[lang] = cache
}
}
await i18next.use(initReactI18next).init({
ns,
lng: cache ? lang : fallbackLanguage,
fallbackLng: fallbackLanguage,
defaultNS,
debug: import.meta.env.DEV,
resources: mergedResources,
})
}
if (import.meta.hot) {
import.meta.hot.on(
"i18n-update",
async ({ file, content }: { file: string; content: string }) => {
const resources = JSON.parse(content)
const i18next = jotaiStore.get(i18nAtom)
const nsName = file.match(/locales\/(.+?)\//)?.[1]
if (!nsName) return
const lang = file.split("/").pop()?.replace(".json", "")
if (!lang) return
i18next.addResourceBundle(lang, nsName, resources, true, true)
console.info("reload", lang, nsName)
await i18next.reloadResources(lang, nsName)
import.meta.env.DEV && EventBus.dispatch("I18N_UPDATE", "")
},
)
}
declare module "@follow/utils/event-bus" {
interface CustomEvent {
I18N_UPDATE: string
}
}