()
| 46 | * externally on the page from the assets folder or via a script tag |
| 47 | */ |
| 48 | export function getUserLocale(): Locale | undefined { |
| 49 | // Check if page has loaded any external locales |
| 50 | const availableLocales = window.dateFns?.locale ?? {}; |
| 51 | |
| 52 | // Match available locales against user locale preferences |
| 53 | const localeKeys = Object.keys(availableLocales); |
| 54 | const userLanguages = navigator.languages || [navigator.language]; |
| 55 | for (const lang of userLanguages) { |
| 56 | // First check full locale string for regional variants (e.g., 'fr-CA') |
| 57 | const normalizedLang = lang.replace('-', ''); |
| 58 | if (availableLocales[normalizedLang]) { |
| 59 | return availableLocales[normalizedLang]; |
| 60 | } |
| 61 | |
| 62 | // Fallback to simple language code (e.g., 'fr') |
| 63 | const langCode = lang.split('-')[0]; |
| 64 | if (availableLocales[langCode]) { |
| 65 | return availableLocales[langCode]; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // No match found against user language preferences, we'll use first |
| 70 | // loaded locale (ultimately determined by script order in HTML) |
| 71 | return availableLocales[localeKeys[0]]; |
| 72 | } |
| 73 | |
| 74 | export function formatDate(date?: Date, formatStr = 'YYYY-MM-DD'): string { |
| 75 | if (!date) { |
no test coverage detected
searching dependent graphs…