Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export type { Span, FeatureFlagsIntegration } from '@sentry/core';
export { makeBrowserOfflineTransport } from './transports/offline';
export { browserProfilingIntegration } from './profiling/integration';
export { spotlightBrowserIntegration } from './integrations/spotlight';
export { cultureContextIntegration } from './integrations/culturecontext';
export { browserSessionIntegration } from './integrations/browsersession';
export { launchDarklyIntegration, buildLaunchDarklyFlagUsedHandler } from './integrations/featureFlags/launchdarkly';
export { openFeatureIntegration, OpenFeatureIntegrationHook } from './integrations/featureFlags/openfeature';
Expand Down
58 changes: 58 additions & 0 deletions packages/browser/src/integrations/culturecontext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { CultureContext, IntegrationFn } from '@sentry/core';
import { defineIntegration, GLOBAL_OBJ } from '@sentry/core';

const INTEGRATION_NAME = 'CultureContext';

const _cultureContextIntegration = (() => {
return {
name: INTEGRATION_NAME,
preprocessEvent(event) {
const culture = getCultureContext();

if (culture) {
event.contexts = {
...event.contexts,
culture: { ...culture, ...event.contexts?.culture },
};
}
},
};
}) satisfies IntegrationFn;

/**
* Captures culture context from the browser.
*
* Enabled by default.
*
* @example
* ```js
* import * as Sentry from '@sentry/browser';
*
* Sentry.init({
* integrations: [Sentry.cultureContextIntegration()],
* });
* ```
*/
export const cultureContextIntegration = defineIntegration(_cultureContextIntegration);

/**
* Returns the culture context from the browser's Intl API.
*/
function getCultureContext(): CultureContext | undefined {
try {
if (typeof (GLOBAL_OBJ as { Intl?: typeof Intl }).Intl === 'undefined') {
return undefined;
}

const options = Intl.DateTimeFormat().resolvedOptions();
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated

return {
locale: options.locale,
timezone: options.timeZone,
calendar: options.calendar,
};
} catch {
// Ignore errors
Comment on lines +54 to +57
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calendar property from resolvedOptions() is included here but is not included in the Node.js implementation (packages/node-core/src/integrations/context.ts:190-193). Consider checking whether calendar is consistently available across all browsers, as it may not be present in older browsers or certain browser configurations. If consistency with the Node.js implementation is desired, or if browser compatibility is a concern, the calendar property could be made optional or omitted. The Node.js implementation only includes locale and timezone.

Suggested change
calendar: options.calendar,
};
} catch {
// Ignore errors
};
} catch {
// Ignore errors
// Ignore errors

Copilot uses AI. Check for mistakes.
return undefined;
}
}
2 changes: 2 additions & 0 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { BrowserClientOptions, BrowserOptions } from './client';
import { BrowserClient } from './client';
import { breadcrumbsIntegration } from './integrations/breadcrumbs';
import { browserApiErrorsIntegration } from './integrations/browserapierrors';
import { cultureContextIntegration } from './integrations/culturecontext';
import { browserSessionIntegration } from './integrations/browsersession';
import { globalHandlersIntegration } from './integrations/globalhandlers';
import { httpContextIntegration } from './integrations/httpcontext';
Expand Down Expand Up @@ -39,6 +40,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
linkedErrorsIntegration(),
dedupeIntegration(),
httpContextIntegration(),
cultureContextIntegration(),
browserSessionIntegration(),
];
}
Expand Down
194 changes: 194 additions & 0 deletions packages/browser/test/integrations/culturecontext.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import type { Event } from '@sentry/core';
import * as SentryCore from '@sentry/core';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cultureContextIntegration } from '../../src/integrations/culturecontext';

describe('CultureContext', () => {
const originalIntl = globalThis.Intl;

beforeEach(() => {
vi.restoreAllMocks();
});

afterEach(() => {
globalThis.Intl = originalIntl;
});

describe('preprocessEvent', () => {
it('adds culture context with locale and timezone', () => {
const mockResolvedOptions = vi.fn().mockReturnValue({
locale: 'en-US',
timeZone: 'America/New_York',
});

globalThis.Intl = {
DateTimeFormat: vi.fn().mockReturnValue({
resolvedOptions: mockResolvedOptions,
}),
} as unknown as typeof Intl;

// @ts-expect-error - mockReturnValue is not typed
vi.spyOn(SentryCore, 'GLOBAL_OBJ', 'get').mockReturnValue({
Intl: globalThis.Intl,
} as typeof SentryCore.GLOBAL_OBJ);

const integration = cultureContextIntegration();
const event: Event = {};

integration.preprocessEvent!(event, {}, {} as never);

expect(event.contexts?.culture).toEqual({
locale: 'en-US',
timezone: 'America/New_York',
});
});

it('preserves existing culture context values', () => {
const mockResolvedOptions = vi.fn().mockReturnValue({
locale: 'en-US',
timeZone: 'America/New_York',
});

globalThis.Intl = {
DateTimeFormat: vi.fn().mockReturnValue({
resolvedOptions: mockResolvedOptions,
}),
} as unknown as typeof Intl;

// @ts-expect-error - mockReturnValue is not typed
vi.spyOn(SentryCore, 'GLOBAL_OBJ', 'get').mockReturnValue({
Intl: globalThis.Intl,
} as typeof SentryCore.GLOBAL_OBJ);

const integration = cultureContextIntegration();
const event: Event = {
contexts: {
culture: {
calendar: 'gregorian',
display_name: 'English (United States)',
},
},
};

integration.preprocessEvent!(event, {}, {} as never);

expect(event.contexts?.culture).toEqual({
locale: 'en-US',
timezone: 'America/New_York',
calendar: 'gregorian',
display_name: 'English (United States)',
});
});

it('does not override existing locale and timezone', () => {
const mockResolvedOptions = vi.fn().mockReturnValue({
locale: 'en-US',
timeZone: 'America/New_York',
});

globalThis.Intl = {
DateTimeFormat: vi.fn().mockReturnValue({
resolvedOptions: mockResolvedOptions,
}),
} as unknown as typeof Intl;

// @ts-expect-error - mockReturnValue is not typed
vi.spyOn(SentryCore, 'GLOBAL_OBJ', 'get').mockReturnValue({
Intl: globalThis.Intl,
} as typeof SentryCore.GLOBAL_OBJ);

const integration = cultureContextIntegration();
const event: Event = {
contexts: {
culture: {
locale: 'de-DE',
timezone: 'Europe/Berlin',
},
},
};

integration.preprocessEvent!(event, {}, {} as never);

// Existing values should be preserved (not overwritten)
expect(event.contexts?.culture).toEqual({
locale: 'de-DE',
timezone: 'Europe/Berlin',
});
});

it('does not add culture context when Intl is not available', () => {
vi.spyOn(SentryCore, 'GLOBAL_OBJ', 'get').mockReturnValue({
Intl: undefined,
} as unknown as typeof SentryCore.GLOBAL_OBJ);

const integration = cultureContextIntegration();
const event: Event = {};

integration.preprocessEvent!(event, {}, {} as never);

expect(event.contexts?.culture).toBeUndefined();
});

it('handles errors gracefully when Intl.DateTimeFormat throws', () => {
globalThis.Intl = {
DateTimeFormat: vi.fn().mockImplementation(() => {
throw new Error('Intl error');
}),
} as unknown as typeof Intl;

// @ts-expect-error - mockReturnValue is not typed
vi.spyOn(SentryCore, 'GLOBAL_OBJ', 'get').mockReturnValue({
Intl: globalThis.Intl,
} as typeof SentryCore.GLOBAL_OBJ);

const integration = cultureContextIntegration();
const event: Event = {};

// Should not throw
expect(() => {
integration.preprocessEvent!(event, {}, {} as never);
}).not.toThrow();

expect(event.contexts?.culture).toBeUndefined();
});

it('preserves other contexts when adding culture context', () => {
const mockResolvedOptions = vi.fn().mockReturnValue({
locale: 'fr-FR',
timeZone: 'Europe/Paris',
});

globalThis.Intl = {
DateTimeFormat: vi.fn().mockReturnValue({
resolvedOptions: mockResolvedOptions,
}),
} as unknown as typeof Intl;

// @ts-expect-error - mockReturnValue is not typed
vi.spyOn(SentryCore, 'GLOBAL_OBJ', 'get').mockReturnValue({
Intl: globalThis.Intl,
} as typeof SentryCore.GLOBAL_OBJ);

const integration = cultureContextIntegration();
const event: Event = {
contexts: {
browser: {
name: 'Chrome',
version: '100.0',
},
},
};

integration.preprocessEvent!(event, {}, {} as never);

expect(event.contexts?.browser).toEqual({
name: 'Chrome',
version: '100.0',
});
expect(event.contexts?.culture).toEqual({
locale: 'fr-FR',
timezone: 'Europe/Paris',
});
});
});
});
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
Loading