Skip to content

Commit 13b4cf1

Browse files
committed
Add configurable URLs to HttpLocalizer and Fluent icon init
This is needed for locked-down environments to avoid hitting a CDN
1 parent c73b2a6 commit 13b4cf1

5 files changed

Lines changed: 21 additions & 15 deletions

File tree

packages/bonito-core/src/__tests__/http-localizer.spec.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ describe("HttpLocalizer", () => {
2222
);
2323

2424
jest.spyOn(httpLocalizer, "getLocale").mockReturnValue("en-US");
25-
await httpLocalizer.loadTranslations();
26-
expect(fetchMock).toHaveBeenCalledWith(
27-
"/resources/i18n/resources.en.json"
28-
);
25+
await httpLocalizer.loadTranslations("/base/url");
26+
expect(fetchMock).toHaveBeenCalledWith("/base/url/resources.en.json");
2927
expect(httpLocalizer.translate("hello")).toEqual("world");
3028
});
3129

@@ -39,7 +37,7 @@ describe("HttpLocalizer", () => {
3937

4038
// Simulate a French locale
4139
jest.spyOn(httpLocalizer, "getLocale").mockReturnValue("fr-FR");
42-
await httpLocalizer.loadTranslations();
40+
await httpLocalizer.loadTranslations("/resources/i18n");
4341
expect(fetchMock).toHaveBeenCalledWith(
4442
"/resources/i18n/resources.fr.json"
4543
);
@@ -56,7 +54,7 @@ describe("HttpLocalizer", () => {
5654

5755
// Simulate an invalid locale
5856
jest.spyOn(httpLocalizer, "getLocale").mockReturnValue("abc");
59-
await httpLocalizer.loadTranslations();
57+
await httpLocalizer.loadTranslations("/resources/i18n");
6058
expect(fetchMock).toHaveBeenCalledWith(
6159
"/resources/i18n/resources.en.json"
6260
);
@@ -78,7 +76,7 @@ describe("HttpLocalizer", () => {
7876
);
7977

8078
jest.spyOn(httpLocalizer, "getLocale").mockReturnValue("en-US");
81-
await httpLocalizer.loadTranslations();
79+
await httpLocalizer.loadTranslations("/resources/i18n");
8280
expect(httpLocalizer.translate("notFound")).toEqual("notFound");
8381
});
8482
});

packages/bonito-core/src/localization/http-localizer.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface Translations {
77
export class HttpLocalizer implements Localizer {
88
private translations?: Translations;
99

10-
async loadTranslations(): Promise<void> {
10+
async loadTranslations(baseUrl: string): Promise<void> {
1111
const languageMap: Record<string, string> = {
1212
cs: "cs",
1313
de: "de",
@@ -43,7 +43,10 @@ export class HttpLocalizer implements Localizer {
4343
}
4444

4545
try {
46-
this.translations = await this.fetchTranslations(languageToLoad);
46+
this.translations = await this.fetchTranslations(
47+
baseUrl,
48+
languageToLoad
49+
);
4750
} catch (error) {
4851
// Fall back to English if translations are not available for the selected locale
4952
if (languageToLoad !== "en") {
@@ -54,6 +57,7 @@ export class HttpLocalizer implements Localizer {
5457
);
5558
languageToLoad = "en";
5659
this.translations = await this.fetchTranslations(
60+
baseUrl,
5761
languageToLoad
5862
);
5963
} else {
@@ -66,8 +70,11 @@ export class HttpLocalizer implements Localizer {
6670
}
6771
}
6872

69-
private async fetchTranslations(lang: string): Promise<Translations> {
70-
const response = await fetch(`/resources/i18n/resources.${lang}.json`);
73+
private async fetchTranslations(
74+
baseUrl: string,
75+
lang: string
76+
): Promise<Translations> {
77+
const response = await fetch(`${baseUrl}/resources.${lang}.json`);
7178

7279
if (!response.ok) {
7380
throw new Error(

packages/bonito-ui/src/environment/browser-environment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface BrowserEnvironment<
4040

4141
export interface BrowserEnvironmentConfig extends EnvironmentConfig {
4242
enableA11yTesting?: boolean;
43+
fluentIconsBaseUrl?: string;
4344
}
4445

4546
/**
@@ -55,7 +56,7 @@ export class DefaultBrowserEnvironment<
5556
name = EnvironmentName.Browser;
5657

5758
async beforeInit(): Promise<void> {
58-
initFluentIcons();
59+
initFluentIcons(this.config.fluentIconsBaseUrl);
5960
}
6061

6162
async beforeDestroy(): Promise<void> {

packages/bonito-ui/src/environment/environment-util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ export function getMockBrowserEnvironment(): MockBrowserEnvironment {
7272
* Initializes FluentUI icons, but only if they have not been initialized
7373
* previously.
7474
*/
75-
export function initFluentIcons(): void {
75+
export function initFluentIcons(baseUrl?: string): void {
7676
if (!_fluentIconsInitialized) {
77-
initializeIcons();
77+
initializeIcons(baseUrl);
7878
_fluentIconsInitialized = true;
7979
}
8080
}

web/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ init(rootEl);
4343

4444
export async function init(rootEl: HTMLElement): Promise<void> {
4545
const localizer = new HttpLocalizer();
46-
await localizer.loadTranslations();
46+
await localizer.loadTranslations("/resources/i18n");
4747
initEnvironment(
4848
new DefaultBrowserEnvironment<
4949
BrowserEnvironmentConfig,

0 commit comments

Comments
 (0)