fix: load i18n on server (#30171)

This commit is contained in:
Stephen Zhou 2025-12-25 19:13:59 +08:00 committed by GitHub
parent 9885e92854
commit 08d5eee993
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 4 deletions

View File

@ -1,7 +1,6 @@
import type { Locale } from '.' import type { Locale } from '.'
import type { KeyPrefix, Namespace } from './i18next-config' import type { Namespace } from './i18next-config'
import { match } from '@formatjs/intl-localematcher' import { match } from '@formatjs/intl-localematcher'
import { camelCase } from 'es-toolkit/compat'
import { createInstance } from 'i18next' import { createInstance } from 'i18next'
import resourcesToBackend from 'i18next-resources-to-backend' import resourcesToBackend from 'i18next-resources-to-backend'
import Negotiator from 'negotiator' import Negotiator from 'negotiator'
@ -23,10 +22,11 @@ const initI18next = async (lng: Locale, ns: Namespace) => {
return i18nInstance return i18nInstance
} }
export async function getTranslation(lng: Locale, ns: Namespace) { export async function getTranslation(lng: Locale, ns: Namespace, options: Record<string, any> = {}) {
const i18nextInstance = await initI18next(lng, ns) const i18nextInstance = await initI18next(lng, ns)
return { return {
t: i18nextInstance.getFixedT(lng, 'translation', camelCase(ns) as KeyPrefix), // @ts-expect-error types mismatch
t: i18nextInstance.getFixedT(lng, ns, options.keyPrefix),
i18n: i18nextInstance, i18n: i18nextInstance,
} }
} }