Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

perf(web): avoid implicit i18n namespace loading (#42873)

Stephen Zhoub9f1ddaff49d
diff --git a/web/i18n/README.md b/web/i18n/README.md
index 57624528454..1773491b4c7 100644
--- a/web/i18n/README.md
+++ b/web/i18n/README.md
@@ -145,8 +145,8 @@ visited earlier in the session. Previously loaded bundles remain cached. There i
no route policy that resets `i18n.options.ns` on navigation.
Server metadata requests still use the request-scoped server instance and load
-exactly their requested namespace. Existing server consumers without a namespace
-retain their full-catalog behavior.
+exactly their requested namespace. Server translation consumers must declare their namespaces explicitly. Locale-only
+consumers subscribe to language changes without requesting a dictionary.
The build analyzer continues to report route usage and check unused keys. Its
route report is diagnostic and is not a runtime resource manifest or an allowlist.
diff --git a/web/i18n/__tests__/route-loading.spec.tsx b/web/i18n/__tests__/route-loading.spec.tsx
index 133ceb5be8b..a4dff499f21 100644
--- a/web/i18n/__tests__/route-loading.spec.tsx
+++ b/web/i18n/__tests__/route-loading.spec.tsx
@@ -9,6 +9,7 @@ import { I18nClientProvider } from '@/app/components/provider/i18n'
import { I18nServerProvider } from '@/app/components/provider/i18n-server'
import { AppToastHost } from '@/app/notifications/host'
import { changeLanguage } from '../client'
+import { useLocale } from '../lib.client'
vi.unmock('react-i18next')
const mocks = vi.hoisted(() => ({ pathname: '/signin', loadResource: vi.fn() }))
@@ -52,6 +53,21 @@ describe('on-demand translation loading', () => {
)
})
+ it('reads and updates the locale without loading any translation namespace', async () => {
+ function LocaleLabel() {
+ return <span>{useLocale()}</span>
+ }
+ render(
+ <I18nClientProvider locale="en-US" resource={{}}>
+ <LocaleLabel />
+ </I18nClientProvider>,
+ )
+ expect(await screen.findByText('en-US')).toBeVisible()
+ await act(() => changeLanguage('zh-Hans'))
+ expect(await screen.findByText('zh-Hans')).toBeVisible()
+ expect(mocks.loadResource).not.toHaveBeenCalled()
+ })
+
it('renders localized text and fallback without requesting missing unrelated namespaces', async () => {
render(
<I18nClientProvider locale="zh-Hans" resource={resources}>
diff --git a/web/i18n/__tests__/server.spec.ts b/web/i18n/__tests__/server.spec.ts
index 9603377f051..153158c6412 100644
--- a/web/i18n/__tests__/server.spec.ts
+++ b/web/i18n/__tests__/server.spec.ts
@@ -83,7 +83,7 @@ describe('server translations', () => {
it('keeps fallback and cross-namespace translations available', async () => {
const { getTranslation } = await import('../server')
- const { t } = await getTranslation('zh-Hans')
+ const { t } = await getTranslation('zh-Hans', 'common')
expect(t(($) => $['operation.save'], { ns: 'common' })).toBe('保存')
expect(t(($) => $['operation.cancel'], { ns: 'common' })).toBe('Cancel')
diff --git a/web/i18n/__tests__/translation-adapters.spec.ts b/web/i18n/__tests__/translation-adapters.spec.ts
index a308737e5f4..efef9aa908e 100644
--- a/web/i18n/__tests__/translation-adapters.spec.ts
+++ b/web/i18n/__tests__/translation-adapters.spec.ts
@@ -33,7 +33,7 @@ describe('translation adapter contracts', () => {
},
)
- it.each([{ ns: ['common'] }, { ns: ['common', 'plugin'] }, { ns: undefined }] as const)(
+ it.each([{ ns: ['common'] }, { ns: ['common', 'plugin'] }] as const)(
'loads the server namespace %s with the current locale',
async ({ ns }) => {
const { useTranslation } = await import('../lib.server')
diff --git a/web/i18n/lib.client.ts b/web/i18n/lib.client.ts
index fb077e829da..23515958962 100644
--- a/web/i18n/lib.client.ts
+++ b/web/i18n/lib.client.ts
@@ -12,6 +12,6 @@ export function useTranslation<
}
export function useLocale(): Locale {
- const { i18n } = useTranslationOriginal()
+ const { i18n } = useTranslationOriginal([])
return i18n.language as Locale
}
diff --git a/web/i18n/lib.server.ts b/web/i18n/lib.server.ts
index bb6f845bb84..c69782a42ec 100644
--- a/web/i18n/lib.server.ts
+++ b/web/i18n/lib.server.ts
@@ -2,16 +2,12 @@ import type { Namespace } from './resources'
import { use } from 'react'
import { getLocaleOnServer, getTranslation } from './server'
-async function getI18nConfig<
- const T extends readonly [Namespace, ...Namespace[]] | undefined = undefined,
->(ns?: T) {
+async function getI18nConfig<const T extends readonly [Namespace, ...Namespace[]]>(ns: T) {
const lang = await getLocaleOnServer()
return getTranslation(lang, ns)
}
-export function useTranslation<
- const T extends readonly [Namespace, ...Namespace[]] | undefined = undefined,
->(ns?: T) {
+export function useTranslation<const T extends readonly [Namespace, ...Namespace[]]>(ns: T) {
return use(getI18nConfig(ns))
}
diff --git a/web/i18n/server.ts b/web/i18n/server.ts
index f82332a9a9b..686b98b7695 100644
--- a/web/i18n/server.ts
+++ b/web/i18n/server.ts
@@ -10,7 +10,6 @@ import { LOCALE_COOKIE_NAME } from '@/config'
import { cookies, headers } from '@/next/headers'
import { loadI18nResource } from './load-resource'
import { canonicalizeLanguageTag, defaultLocale, supportedLocales } from './locale'
-import { namespaces } from './resources'
import { getInitOptions } from './settings'
const getOrCreateI18next = cache(async (lng: Locale) => {
@@ -31,10 +30,10 @@ const getOrCreateI18next = cache(async (lng: Locale) => {
export async function getTranslation<
const T extends Namespace | readonly [Namespace, ...Namespace[]],
->(lng: Locale, ns?: T) {
+>(lng: Locale, ns: T) {
const i18nextInstance = await getOrCreateI18next(lng)
- await i18nextInstance.loadNamespaces(typeof ns === 'string' ? [ns] : [...(ns ?? namespaces)])
+ await i18nextInstance.loadNamespaces(typeof ns === 'string' ? [ns] : [...ns])
return {
t: i18nextInstance.getFixedT<T>(lng, ns),