1
ahhtree OP 请问 v2 上的各位大佬,有没有遇见过这种问题?如何解决的?(不小心点击了发送...。💦)
|
2
ahhtree OP 相应 -> 响应
|
3
NessajCN 2023-11-15 08:44:47 +08:00
你 i18next 咋配置的,用的 pages 还是 routes,服务端能不能打印,https 是 local-ssl 的还是 nginx 代理
|
4
ahhtree OP @NessajCN i18n 主要是参考这篇文章配置的: https://locize.com/blog/next-app-dir-i18n/
|
6
NessajCN 2023-11-15 09:12:03 +08:00
那你发一下 `app/[lng]/layout.js` 和 `app/i18n/settings.js`
|
7
NessajCN 2023-11-15 09:13:28 +08:00
app/[lng]/page.js 也要
|
8
ahhtree OP layout:
<code> export async function generateStaticParams() { return Languages.map((lang) => ({ lang })); } export default async function RootLayout({ children, params: { lang }, }: { children: React.ReactNode; params: { lang: LanguageType }; }) { return ( <html lang={lang.includes("zh") ? "zh-CN" : "en-US"}> <body className={inter.className}> <main className="relative w-full h-screen flex flex-col"> <Header lang={lang} /> {/* <ContentWrapper childNodes={children} lang={lang} /> */} <div className="relative grow w-full flex flex-col items-center justify-center"> {children} </div> <Toaster /> <Footer lang={lang} /> </main> <InitRDKit /> </body> </html> ); } </code> settings: <code> export type LanguageType = "en" | "zh"; export const FallbackLng: LanguageType = "en"; export const Languages: LanguageType[] = [FallbackLng, "zh"]; export const CookieLocaleName = "i18next"; export const defaultNS = "home"; export function getOptions(lng: LanguageType = FallbackLng, ns = defaultNS) { return { debug: process.env.NODE_ENV === "development", supportedLngs: Languages, fallbackLng: FallbackLng, lng, fallbackNS: defaultNS, defaultNS, ns, }; } </code> i18n 服务端配置: <code> const initI18next = async (lng: LanguageType, ns: string) => { const i18nInstance = createInstance({}); await i18nInstance .use(initReactI18next) .use( resourcesToBackend( (language: string, namespace: string) => import(`./locales/${language}/${namespace}.json`) ) ) .init({ ...getOptions(lng, ns), detection: { order: ["path", "htmlTag", "navigator"], // lookupCookies: "", cookieOptions: { sameSite: "lax", path: `/${lng}`, }, lookupCookie: CookieLocaleName, caches: ["localStorage"], excludeCacheFor: ["cookie"], }, }); return i18nInstance; }; export async function serverTranslation( lng: LanguageType, ns: string, options = {} ) { const i18nextInstance = await initI18next(lng, ns); return { t: i18nextInstance.getFixedT(lng, Array.isArray(ns) ? ns[0] : ns), i18n: i18nextInstance, }; } </code> i18n 客户端 配置: <code> const runsOnServerSide = typeof window === "undefined"; const languageDetector = new LanguageDetector(); languageDetector.init({ useCookies: false }); // i18next .use(initReactI18next) .use(languageDetector) .use( resourcesToBackend( (language: string, namespace: string) => import(`./locales/${language}/${namespace}.json`) ) ) .init({ ...getOptions(), lng: undefined, // let detect the language on client side preload: runsOnServerSide ? Languages : [], detection: { order: ["path", "htmlTag", "navigator"], // lookupCookies: "", cookieOptions: { sameSite: "lax", }, caches: ['localStorage'], }, }); export function useTranslation( lng: string, ns: string, options?: UseTranslationOptions<any> ) { const [done, setDone] = useState(false); const [cookies, setCookie] = useCookies([CookieLocaleName]); const ret = useTranslationOrg(ns, options); const { i18n } = ret; if (runsOnServerSide && lng && i18n.resolvedLanguage !== lng) { i18n.changeLanguage(lng); } else { // debugger // eslint-disable-next-line react-hooks/rules-of-hooks const [activeLng, setActiveLng] = useState(i18n.resolvedLanguage); // eslint-disable-next-line react-hooks/rules-of-hooks useEffect(() => { if (activeLng !== lng) { setActiveLng(lng); i18n.changeLanguage(lng); } }, [activeLng, lng, i18n]); // eslint-disable-next-line react-hooks/rules-of-hooks useEffect(() => { if (cookies[CookieLocaleName] === lng) return; setCookie(CookieLocaleName, lng, { sameSite: "lax", path: `/${lng}` }); }, [lng, cookies, setCookie]); useEffect(() => { setDone(true); }, []); } return ret; } </code> |
9
NessajCN 2023-11-15 09:34:31 +08:00
你的入口页 page.js 怎么写的
|
10
ahhtree OP page:
export async function generateStaticParams() { return Languages.map((lang) => ({ lang })); } export default function Page({ params: { lang }, }: { params: { lang: LanguageType }; }) { const router = useRouter(); const [values, setValues] = useState(["", ""]); const setContentArray = useSearchContentArrayStore( (state) => state.setContentArray ); const handleSearch = useCallback(() => { setContentArray(values); router.push( `/${lang}/search?value1=${encodeURIComponent( values[0] )}&value2=${encodeURIComponent(values[1])}` ); }, [values, router, lang, setContentArray]); return ( <div> {/* <Bubbles /> */} <SearchBar isRow={false} values={values} setValues={setValues} handleSearch={handleSearch} /> </div> ); } |
11
clue 2023-11-15 10:03:26 +08:00
nextjs 难道没有日志?
|