diff --git a/src/lib/CalendarMonth.svelte b/src/lib/CalendarMonth.svelte index 8f6c725..87ff74e 100644 --- a/src/lib/CalendarMonth.svelte +++ b/src/lib/CalendarMonth.svelte @@ -6,10 +6,33 @@ export let holidays = []; export let optimizedDaysOff = []; export let consecutiveDaysOff = []; + export let selectedCountryCode; + + // Function to determine the first day of the week based on locale + function getFirstDayOfWeek(locale) { + // Convert 'us' to proper locale format + const normalizedLocale = locale.toLowerCase() === 'us' ? 'en-US' : `en-${locale.toUpperCase()}`; + + try { + // Try to get firstDay from Intl.Locale weekInfo + // @ts-ignore .weekInfo exists on all browsers except Firefox + const weekFirstDay = new Intl.Locale(normalizedLocale)?.weekInfo?.firstDay; + if (weekFirstDay !== undefined) { + return weekFirstDay; + } + } catch (e) { + // Fallback if weekInfo is not supported + } + + // Fallback: US starts on Sunday (0), most others on Monday (1) + return normalizedLocale === 'en-US' ? 0 : 1; + } // Reactive declarations $: daysInMonth = getDaysInMonth(year, month); - $: firstDay = getFirstDayOfMonth(year, month); + $: locale = selectedCountryCode ? new Intl.Locale(selectedCountryCode).toString() : 'us'; + $: firstDayOfWeek = getFirstDayOfWeek(locale); + $: adjustedFirstDay = (getFirstDayOfMonth(year, month) - firstDayOfWeek + 7) % 7; function getDaysInMonth(year, month) { return new Date(year, month + 1, 0).getDate(); @@ -58,15 +81,45 @@ return date >= start && date <= end; }); } + + // Function to determine if a day is a weekend + function isWeekend(day) { + const dayOfWeek = (adjustedFirstDay + day - 1) % 7; + // Calculate the indices for Saturday and Sunday + const saturdayIndex = (6 - firstDayOfWeek + 7) % 7; + const sundayIndex = (7 - firstDayOfWeek + 7) % 7; + return dayOfWeek === saturdayIndex || dayOfWeek === sundayIndex; + } + + // Function to get the localized day initials based on the first day of the week + function getLocalizedDayInitials(locale) { + const formatter = new Intl.DateTimeFormat(locale, { weekday: 'short' }); + const dayInitials = []; + for (let i = 0; i < 7; i++) { + const date = new Date(Date.UTC(2021, 0, i + 3)); // Start from a known Thursday to ensure full week coverage + const dayName = formatter.format(date); + dayInitials.push(dayName.charAt(0).toUpperCase()); + } + return dayInitials; + } + + // Reactive declaration to get the ordered day initials + $: orderedDayInitials = getLocalizedDayInitials(locale).slice(firstDayOfWeek).concat(getLocalizedDayInitials(locale).slice(0, firstDayOfWeek));