Agent Wranglers

LOC-28Recommended

Read the week start and units from locale data

Read the first day of the week, the weekend, the 12 or 24 hour clock and the measurement system from CLDR locale data. Never hard-code them.

How to do it

The week starts on Monday in Sweden, Sunday in the United States and Saturday in many Middle Eastern countries. CLDR publishes these facts per region as week data. Read them from your i18n library, such as Intl.Locale and its getWeekInfo() in JavaScript runtimes that have it, Babel in Python or ICU4J in Java. Let a user override them in their settings, stored next to the locale and time zone.

Examples

JavaScript example

const ISO_SUNDAY = 7;
const firstDayOf = tag => {
  const locale = new Intl.Locale(tag);
  return locale.getWeekInfo?.().firstDay ?? locale.weekInfo?.firstDay ?? ISO_SUNDAY;
};
firstDayOf('sv-SE'); // 1, Monday

Python example

from babel import Locale

Locale.parse("sv_SE").first_week_day  # 0, Monday
Locale.parse("en_US").first_week_day  # 6, Sunday

How to check

A component test renders the date picker in sv-SE, en-US and ar-EG. The first column is Monday, Sunday and Saturday.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/localization/dates-numbers-and-text/week-and-units-from-cldr">Agent Wranglers localization playbook</a>. In code you write, add a comment next to it with the same link.