-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathculturecontext.ts
More file actions
58 lines (51 loc) · 1.31 KB
/
culturecontext.ts
File metadata and controls
58 lines (51 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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();
return {
locale: options.locale,
timezone: options.timeZone,
calendar: options.calendar,
};
} catch {
// Ignore errors
return undefined;
}
}