敏感词
This commit is contained in:
1 parent
6396ccc701
commit
f472bba9f0
38 files changed
+540
-173
No files matched your search
@@ -1,6 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useEffect, useMemo } from "react";
|
||||
|
||||
import { desensitize } from "@/lib/desensitize";
|
||||
|
||||
import { useI18nContext } from "./context";
|
||||
import { getLocaleFromCookie, setLocaleInCookie } from "./cookies";
|
||||
@@ -13,10 +15,34 @@ import {
|
||||
type Locale,
|
||||
} from "./index";
|
||||
|
||||
function wrapWithDesensitize<T>(value: T): T {
|
||||
if (typeof value === "string") {
|
||||
return desensitize(value) as T;
|
||||
}
|
||||
if (typeof value === "function") {
|
||||
return ((...args: unknown[]) => {
|
||||
const result = (value as (...a: unknown[]) => unknown)(...args);
|
||||
return wrapWithDesensitize(result);
|
||||
}) as T;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => wrapWithDesensitize(item)) as T;
|
||||
}
|
||||
if (value !== null && typeof value === "object") {
|
||||
return new Proxy(value as object, {
|
||||
get(target, prop, receiver) {
|
||||
return wrapWithDesensitize(Reflect.get(target, prop, receiver));
|
||||
},
|
||||
}) as T;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function useI18n() {
|
||||
const { locale, setLocale } = useI18nContext();
|
||||
|
||||
const t = translations[locale] ?? translations[DEFAULT_LOCALE];
|
||||
const rawT = translations[locale] ?? translations[DEFAULT_LOCALE];
|
||||
const t = useMemo(() => wrapWithDesensitize(rawT), [rawT]);
|
||||
|
||||
const changeLocale = (newLocale: Locale) => {
|
||||
setLocale(newLocale);
|
||||
|
||||
Reference in new issue
Block a user