Skip to content

Commit 5a40f75

Browse files
committed
fix(webview): skip the dead en lazy glob and load ru in the long-locale story
1 parent 09d0180 commit 5a40f75

4 files changed

Lines changed: 16 additions & 30 deletions

File tree

‎webview-ui/playwright/gallery/stories.tsx‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,8 @@ export const stories: Record<string, Story> = {
420420
return <UISettingsStory />
421421
},
422422
"ui-settings-long-locale": async () => {
423-
const [{ UISettingsStory }, { default: i18next }] = await Promise.all([
424-
import("@/components/settings/__tests__/UISettings.visual.fixture"),
425-
import("@/i18n/setup"),
426-
])
427-
await i18next.changeLanguage("ru")
428-
return <UISettingsStory />
423+
const { UISettingsStory } = await import("@/components/settings/__tests__/UISettings.visual.fixture")
424+
return <UISettingsStory initialState={{ language: "ru" }} />
429425
},
430426
welcome: async () => {
431427
const [{ AppProviders }, { WelcomeLanding }] = await Promise.all([

‎webview-ui/src/components/settings/__tests__/UISettings.visual.fixture.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface UIState {
1414
autoCloseZooOpenedNewFiles?: boolean
1515
}
1616

17-
export function UISettingsStory() {
17+
export function UISettingsStory({ initialState }: Pick<React.ComponentProps<typeof AppProviders>, "initialState">) {
1818
const [state, setState] = useState<UIState>({
1919
reasoningBlockCollapsed: true,
2020
enterBehavior: "send",
@@ -42,7 +42,7 @@ export function UISettingsStory() {
4242
}
4343

4444
return (
45-
<AppProviders>
45+
<AppProviders initialState={initialState}>
4646
<div
4747
data-testid="ui-settings-story"
4848
className="w-[488px] max-w-full rounded-lg border border-vscode-panel-border bg-vscode-editor-background p-4 text-vscode-editor-foreground">

‎webview-ui/src/i18n/__tests__/setup.rejection.spec.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import i18next, { FALLBACK_LANGUAGE, loadLanguage } from "../setup"
1+
import i18next, { loadLanguage } from "../setup"
22

33
// A dynamic import that rejects is what a 404 or a chunk missing from the
44
// deployed manifest looks like at runtime. One namespace is enough: `loadLanguage`
@@ -38,6 +38,5 @@ describe("i18n setup locale load failure", () => {
3838
// VS Code webview) can still reject the second import from that cache
3939
// without re-requesting the chunk.
4040
expect(warn).toHaveBeenCalledTimes(2)
41-
expect(i18next.language).toBe(FALLBACK_LANGUAGE)
4241
})
4342
})

‎webview-ui/src/i18n/setup.ts‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type LocaleModule = { default?: LocaleResources } & LocaleResources
2020
const LOCALE_PATH_PATTERN = /\.\/locales\/([^/]+)\/([^/]+)\.json$/
2121

2222
const eagerLocaleFiles = import.meta.glob<LocaleModule>("./locales/en/*.json", { eager: true })
23-
const lazyLocaleFiles = import.meta.glob<LocaleModule>("./locales/**/*.json")
23+
const lazyLocaleFiles = import.meta.glob<LocaleModule>(["./locales/**/*.json", "!./locales/en/*.json"])
2424

2525
const pendingLanguages = new Map<string, Promise<void>>()
2626
const loadedLanguages = new Set<string>([FALLBACK_LANGUAGE])
@@ -67,27 +67,19 @@ export function loadLanguage(language: string): Promise<void> {
6767
return pending
6868
}
6969

70-
const loaders = Object.entries(lazyLocaleFiles)
71-
.filter(([path]) => parseLocalePath(path)?.language === language)
72-
.map(([path, load]) => load().then((module) => ({ path, module })))
70+
const loaders = Object.entries(lazyLocaleFiles).flatMap(([path, load]) => {
71+
const parsed = parseLocalePath(path)
7372

74-
// Unknown language: leave the fallback bundles in place so i18next resolves
75-
// every key through `fallbackLng`.
76-
if (loaders.length === 0) {
77-
loadedLanguages.add(language)
78-
return Promise.resolve()
79-
}
73+
return parsed?.language === language ? [load().then((module) => ({ namespace: parsed.namespace, module }))] : []
74+
})
8075

76+
// An unknown language has no loaders, so `Promise.all` resolves at once and
77+
// the fallback bundles stay in place: i18next resolves every key through
78+
// `fallbackLng`.
8179
const promise = Promise.all(loaders)
8280
.then((modules) => {
83-
modules.forEach(({ path, module }) => {
84-
const parsed = parseLocalePath(path)
85-
86-
if (!parsed) {
87-
return
88-
}
89-
90-
i18next.addResourceBundle(parsed.language, parsed.namespace, module.default || module, true, true)
81+
modules.forEach(({ namespace, module }) => {
82+
i18next.addResourceBundle(language, namespace, module.default || module, true, true)
9183
})
9284

9385
loadedLanguages.add(language)
@@ -110,8 +102,7 @@ i18next.use(initReactI18next).init({
110102
lng: FALLBACK_LANGUAGE, // Default language (will be overridden)
111103
fallbackLng: FALLBACK_LANGUAGE,
112104
debug: false,
113-
// Handed to `init` rather than added afterwards so the first render already
114-
// resolves keys; `addResourceBundle` before init settles would be dropped.
105+
// Handed to `init` so the first render already resolves keys.
115106
resources: collectLocaleResources(eagerLocaleFiles),
116107
interpolation: {
117108
escapeValue: false, // React already escapes by default

0 commit comments

Comments
 (0)