Translation engine
The Translation engine knows the language, the timezone and the date preferences of the person. Everything that writes a date, a number or a list goes through it.
- What it does
- Holds the language, the timezone, the date order and the first day of the week, and writes dates, numbers, lists and plurals the way that language does.
- Exported from the package?
- Yes. Import everything on this page from
@davidnet-net/svelte-ui. The functions here are the public API; the pure functions inutils/format.tsare not, because they do not know the preferences.
Each setter stores the choice in a cookie on
.davidnet.net for 365 days, so every Davidnet app reads it, signed in or not: language_cache (the language is also in local storage as language), timezone_cache, first_day_of_week_cache and date_format_cache. The setters do not save to the account: the settings screen does. Signed in, AppShell applies the language, timezone, date order and first day of the week of the account when the session is found, so the account wins at the start. Signed out, the cookies are all there is. Without them the language and the timezone come from the browser, the week starts on Monday, and dates follow the natural order of the language.Which one do I use?
| What I need | Use |
|---|---|
| A moment, with or without the time (a message was sent) | formatDate |
| A day without a time (a due date, a birthday) | formatCalendarDate |
| A period from one date to another | formatDateRange |
| The title of a calendar month | formatMonthYear |
| How long ago something happened, or how soon | formatRelativeTime |
| A number, a percentage or an amount of money | formatNumber, formatPercent, formatCurrency |
| A big number in a small space (1.3M) | formatCompactNumber |
| The size of a file | formatBytes |
| Several items in one sentence | formatList |
| A text for one item or for several | getPluralCategory |
| The names of months or weekdays | getMonthName, getMonthNames, getWeekdayName, getWeekdayNames |
| A calendar grid, with the week starting where the person wants | getFirstDayOfWeekIndex, getWeekdayOffset, getStartOfWeek |
| A list of timezones to choose from | getTimezones, getBrowserTimezone |
Formatting
Intl or toLocale… yourself, and never type a separator or the name of a month by hand. These functions are the public API: the pure functions in utils/format.ts are not, because they do not know the preferences of the person. Import them from @davidnet-net/svelte-ui. Call them inside a template or a $derived, and the text is written again when the language or a preference changes.formatDate
function formatDate(value: Date | number | string, options?: DateFormatOptions): stringA moment as text, in the timezone of the person, with the month as a word. It takes a Date, milliseconds or an ISO string with a time, and gives an empty string for a value that is not a date. The order of the parts follows the preference of the person: without one it is the natural order of the language.
When to use
Use it for something that happened or will happen at a moment: a message, an upload, a sign-in.
When not to use, and what to use instead
For a day without a time (a due date) use formatCalendarDate: it never shifts to another day in another timezone. For how long ago, use formatRelativeTime. For a period, use formatDateRange.
Options
| Name | Type | Default | Description |
|---|---|---|---|
size | "full" | "compact" | "full" | "full" writes the names out, "compact" shortens them. Use compact only where the full form does not fit. |
weekday | boolean | — | Whether the name of the weekday is part of it. True for full, false for compact. |
time | boolean | "seconds" | false | Adds the time as 24 hours (15:30). "seconds" adds the seconds (15:30:05). |
year | "auto" | "always" | "never" | — | When the year is shown. "auto" leaves it out for the current year. Always for full, auto for compact. |
Live example
| Call | Result |
|---|---|
| formatDate(ms) | Wednesday, November 18, 2026 |
| formatDate(ms, { weekday: false }) | November 18, 2026 |
| formatDate(ms, { size: "compact" }) | Nov 18 |
| formatDate(ms, { size: "compact", weekday: true }) | Wed, Nov 18 |
| formatDate(ms, { time: true }) | Wednesday, November 18, 2026, 15:30 |
| formatDate(ms, { weekday: false, time: "seconds" }) | November 18, 2026, 15:30:05 |
| formatDate(ms, { weekday: false, year: "never" }) | November 18 |
| formatDate("not a date") | "" |
formatDate(ms);
formatDate(ms, { weekday: false });
formatDate(ms, { size: "compact", weekday: true, time: true });formatCalendarDate
function formatCalendarDate(date: string, options?: Omit<DateFormatOptions, "time">): stringA day written as YYYY-MM-DD, without a time, as text. It never shifts to another day in another timezone. It has the same options and output as formatDate, without time.
When to use
Use it for a due date, a birthday or a start date: anything that is a day and not a moment.
When not to use, and what to use instead
For a moment with a time use formatDate. It only takes a string like 2026-11-18.
Options
| Name | Type | Default | Description |
|---|---|---|---|
size | "full" | "compact" | "full" | "full" writes the names out, "compact" shortens them. Use compact only where the full form does not fit. |
weekday | boolean | — | Whether the name of the weekday is part of it. True for full, false for compact. |
year | "auto" | "always" | "never" | — | When the year is shown. "auto" leaves it out for the current year. Always for full, auto for compact. |
Live example
| Call | Result |
|---|---|
| formatCalendarDate("2026-11-18") | Wednesday, November 18, 2026 |
| formatCalendarDate("2026-11-18", { weekday: false }) | November 18, 2026 |
| formatCalendarDate("2026-11-18", { size: "compact" }) | Nov 18 |
formatCalendarDate("2026-11-18");
formatCalendarDate("2026-11-18", { size: "compact" });formatDateRange
function formatDateRange(start: DateInput, end: DateInput, options?: Pick<DateFormatOptions, "size" | "year">): stringTwo moments as a range, with the shared parts written once: November 18 – 24, 2026 in en-us, 18–24 november 2026 in nl, in the natural order of the language. Compact gives Nov 18 – 24. When the person chose an order, both ends are written out.
When to use
Use it for the period of a sprint, a trip or an event.
When not to use, and what to use instead
For one date use formatDate. Never join two dates with a hyphen yourself: the order and the spacing differ per language.
Options
| Name | Type | Default | Description |
|---|---|---|---|
size | "full" | "compact" | "full" | "full" writes the names out, "compact" shortens them. Use compact only where the full form does not fit. |
year | "auto" | "always" | "never" | — | When the year is shown. "auto" leaves it out for the current year. Always for full, auto for compact. |
Live example
| Call | Result |
|---|---|
| formatDateRange(nov18, nov24) | November 18 – 24, 2026 |
| formatDateRange(nov28, dec3) | November 28 – December 3, 2026 |
| formatDateRange(nov18, nov24, { size: "compact" }) | Nov 18 – 24 |
| formatDateRange(dec29, jan2) | December 29, 2026 – January 2, 2027 |
formatDateRange(start, end);
formatDateRange(start, end, { size: "compact" });formatMonthYear
function formatMonthYear(value: DateInput | { year: number; month: number }, size?: "full" | "compact"): stringA month with its year, for the title of a calendar: November 2026, compact Nov 2026. Give a moment, or an object with year and month, where the month goes from 0 (January) to 11 (December).
When to use
Use it for the heading of a month view or a month picker.
When not to use, and what to use instead
For a full date use formatDate. For only the name of a month, use getMonthName.
Live example
| Call | Result |
|---|---|
| formatMonthYear(ms) | November 2026 |
| formatMonthYear({ year: 2026, month: 10 }) | November 2026 |
| formatMonthYear(ms, "compact") | Nov 2026 |
formatMonthYear({ year: 2026, month: 10 });
formatMonthYear(ms, "compact");formatRelativeTime
function formatRelativeTime(value: DateInput): stringHow long ago or from now, in the unit that fits: 2 hours ago, tomorrow, in the language of the person.
When to use
Use it for recent activity: a last edit, a new comment, a deadline in a few days.
When not to use, and what to use instead
When it is weeks ago, the exact date says more: use formatDate. Put the exact date in a tooltip for anything people may need to look up.
Live example
| Call | Result |
|---|---|
| formatRelativeTime(now - 5 minutes) | 5 minutes ago |
| formatRelativeTime(now - 2 hours) | 2 hours ago |
| formatRelativeTime(now - 3 days) | 3 days ago |
| formatRelativeTime(now + 1 day) | tomorrow |
| formatRelativeTime(now + 3 days) | in 3 days |
formatRelativeTime(Date.now() - 2 * 60 * 60 * 1000);formatNumber
function formatNumber(value: number, options?: { minimumFractionDigits?: number; maximumFractionDigits?: number }): stringA number with the separators of the language: 100,000.5 in en-us, 100.000,5 in nl. The options set the fewest and the most decimals.
When to use
Use it for every number a person reads, from a count to a price without a symbol.
When not to use, and what to use instead
For a percentage use formatPercent, for money formatCurrency, and in a small space formatCompactNumber. Never build the text with toFixed or by adding separators yourself.
Options
| Name | Type | Default | Description |
|---|---|---|---|
minimumFractionDigits | number | — | The fewest decimals to show. |
maximumFractionDigits | number | — | The most decimals to show. |
Live example
| Call | Result |
|---|---|
| formatNumber(100000.5) | 100,000.5 |
| formatNumber(1234567) | 1,234,567 |
| formatNumber(3.14159, { maximumFractionDigits: 2 }) | 3.14 |
| formatNumber(3, { minimumFractionDigits: 2 }) | 3.00 |
formatNumber(100000.5);
formatNumber(3.14159, { maximumFractionDigits: 2 });formatPercent
function formatPercent(fraction: number, options?: { minimumFractionDigits?: number; maximumFractionDigits?: number }): stringA fraction as a percentage, with one decimal at most: 0.5% for 0.005 in en-us, 0,5% in nl. Pass a fraction (0.25), not a percentage (25).
When to use
Use it for progress, a change or a share.
When not to use, and what to use instead
For a plain number use formatNumber. Do not multiply by 100 yourself.
Options
| Name | Type | Default | Description |
|---|---|---|---|
minimumFractionDigits | number | — | The fewest decimals to show. |
maximumFractionDigits | number | — | The most decimals to show. |
Live example
| Call | Result |
|---|---|
| formatPercent(0.005) | 0.5% |
| formatPercent(0.256) | 25.6% |
| formatPercent(1) | 100% |
formatPercent(0.256);formatCurrency
function formatCurrency(value: number, currency: string): stringAn amount of money with the symbol and the separators of the language: €100,000.50 in en-us, € 100.000,50 in nl for 100000.5 and "EUR". The currency is an ISO 4217 code.
When to use
Use it for prices and balances. You choose the currency, the language only decides how it is written.
When not to use, and what to use instead
It never converts between currencies. For a number without a symbol use formatNumber.
Live example
| Call | Result |
|---|---|
| formatCurrency(100000.5, "EUR") | €100,000.50 |
| formatCurrency(19.99, "USD") | $19.99 |
formatCurrency(100000.5, "EUR");formatCompactNumber
function formatCompactNumber(value: number): stringA large number in a short form: 1.3M for 1250000 in en-us, 1,3 mln. in nl.
When to use
Use it where space is tight: a counter on a card, the axis of a chart, a badge.
When not to use, and what to use instead
Where there is room, or where the exact value matters (money, the total on an invoice), use formatNumber.
Live example
| Call | Result |
|---|---|
| formatCompactNumber(999) | 999 |
| formatCompactNumber(12500) | 13K |
| formatCompactNumber(1250000) | 1.3M |
formatCompactNumber(1250000);formatBytes
function formatBytes(bytes: number): stringA size in bytes, in the unit that fits: 1.2 MB for 1200000 in en-us, 1,2 MB in nl.
When to use
Use it for the size of a file, an upload or a quota.
When not to use, and what to use instead
For a count of items use formatNumber.
Live example
| Call | Result |
|---|---|
| formatBytes(512) | 512 byte |
| formatBytes(1200000) | 1.2 MB |
| formatBytes(5368709120) | 5.4 GB |
formatBytes(file.size);formatList
function formatList(items: string[], type?: "and" | "or"): stringItems as a sentence: A, B, and C in en-us, A, B en C in nl. Pass "or" as the second argument for A, B, or C.
When to use
Use it when you name several things in one sentence: members, tags, files.
When not to use, and what to use instead
Never join with ", " yourself: the Oxford comma and the word for ‘and’ differ per language. A long list that people scan is a list element, not a sentence.
Live example
| Call | Result |
|---|---|
| formatList(["Ana", "Ben", "Chris"]) | Ana, Ben, and Chris |
| formatList(["Ana", "Ben"]) | Ana and Ben |
| formatList(["Ana", "Ben", "Chris"], "or") | Ana, Ben, or Chris |
formatList(["Ana", "Ben", "Chris"]);
formatList(["Ana", "Ben", "Chris"], "or");getPluralCategory
function getPluralCategory(count: number): Intl.LDMLPluralRuleThe plural category of a number in the language: "zero", "one", "two", "few", "many" or "other". en-us and nl only have "one" and "other".
When to use
Use it to pick between the message for one item and the message for several.
When not to use, and what to use instead
Do not write count === 1: other languages have more forms. Show the number itself with formatNumber.
Live example
| Call | Result |
|---|---|
| getPluralCategory(0) | other |
| getPluralCategory(1) | one |
| getPluralCategory(2) | other |
| getPluralCategory(5) | other |
const category = getPluralCategory(count);
const text = category === "one" ? m.files_one() : m.files_other({ count: formatNumber(count) });getMonthName
function getMonthName(month: number, size?: "full" | "compact"): string
function getMonthNames(size?: "full" | "compact"): string[]The name of a month, from 0 (January) to 11: November, compact Nov. getMonthNames gives all twelve, January first.
When to use
Use them for a month picker or a chart axis.
When not to use, and what to use instead
For a month with its year use formatMonthYear. For a whole date use formatDate.
Live example
| Call | Result |
|---|---|
| getMonthName(10) | November |
| getMonthName(10, "compact") | Nov |
| getMonthNames("compact") | Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec |
getMonthName(10);
getMonthNames("compact");getWeekdayName
function getWeekdayName(weekday: number, size?: "full" | "compact"): string
function getWeekdayNames(size?: "full" | "compact"): string[]The name of a day of the week, from 0 (Sunday) like the Date object: Wednesday, compact Wed. getWeekdayNames gives all seven, starting with the first day of the week the person chose.
When to use
Use getWeekdayNames for the header of a calendar, and getWeekdayName for a single day.
When not to use, and what to use instead
For a whole date with its weekday use formatDate.
Live example
| Call | Result |
|---|---|
| getWeekdayName(3) | Wednesday |
| getWeekdayName(3, "compact") | Wed |
| getWeekdayNames("compact") | Mon Tue Wed Thu Fri Sat Sun |
getWeekdayName(3);
getWeekdayNames("compact");getFirstDayOfWeekIndex
function getFirstDayOfWeekIndex(): numberThe first day of the week the person chose, as a number like the Date object has them: 0 is Sunday, 1 is Monday.
When to use
Use it when you build your own week logic. For a calendar grid use getWeekdayOffset and getStartOfWeek: they already use it.
When not to use, and what to use instead
Do not assume Monday, or Sunday. The person chose, see Language and preferences.
Live example
| Call | Result |
|---|---|
| getFirstDayOfWeekIndex() | 1 |
getWeekdayOffset
function getWeekdayOffset(date: Date): number
function getStartOfWeek(date: Date): DateThe column of a day in a calendar grid: how many days after the first day of the week of the person it is, from 0 to 6. getStartOfWeek gives the first day of the week a date is in, at the start of that day.
When to use
Use them to lay out a month grid or a week view.
When not to use, and what to use instead
For only the names above the columns use getWeekdayNames.
Live example
| Call | Result |
|---|---|
| getWeekdayOffset(new Date(2026, 10, 1)) | 6 |
| formatDate(getStartOfWeek(new Date(2026, 10, 18)), { weekday: false }) | November 16, 2026 |
// The column of the first of the month in a month grid
const column = getWeekdayOffset(new Date(2026, 10, 1));getTimezones
function getTimezones(): string[]
function getBrowserTimezone(): stringEvery timezone there is, as IANA names. getBrowserTimezone gives the timezone the browser is in, a good first guess for a new person.
When to use
Use getTimezones for a list to choose from, and getBrowserTimezone to preselect one.
When not to use, and what to use instead
To read the timezone the person chose, use getTimezone on Language and preferences.
Live example
| Call | Result |
|---|---|
| getTimezones().length | 445 |
| getTimezones().slice(0, 3) | Africa/Abidjan, Africa/Accra, Africa/Addis_Ababa |
| getBrowserTimezone() | UTC |
formatUnixMsToPreferred, formatUnixSecToPreferred, formatIsoToPreferred, formatUnixMsToShort, formatUnixSecToShort, formatIsoToShort and formatDateOnlyToShort still work, but are deprecated. Use formatDate (with size: "compact" for the short ones) and formatCalendarDate. They now write the month as a word too.Language and preferences
setLanguage
function setLanguage(newLocale: string): voidChanges the language of the whole app, without a reload: every text made with a message function is written again. The choice is kept in a cookie and in local storage.
When to use
Use it in the language picker of a settings screen.
When not to use, and what to use instead
To read the language, use currentLocale.value. Do not set the language from a text you translated yourself: use a locale your app has.
Live example
language_cache= November 18, 2026 You are signed out: the cookies are all there is, and the only place where the choice is kept.setLanguage("nl");currentLocale
const currentLocale: { readonly value: string }The language that is active now, as an object with a value that is reactive: read it in a template or a $derived and it updates when the language changes.
When to use
Use it to show the active language, or to pick something that is not a message (like a link to a page per language).
When not to use, and what to use instead
For text use the message functions, and for dates and numbers the functions on Formatting: they already follow the language.
Live example
| Call | Result |
|---|---|
| currentLocale.value | en-us |
const language = $derived(currentLocale.value);setTimezone
function setTimezone(tz: string): void
function getTimezone(): stringSets the timezone, as an IANA name like Europe/Amsterdam. A name that is not in validTimezones is refused with a warning in the console. getTimezone reads it: the stored one, else the one of the browser, else UTC.
When to use
Use setTimezone when someone picks a timezone, and getTimezone to show the current one in a settings screen.
When not to use, and what to use instead
For the list to pick from use getTimezones on Formatting. You do not need to pass the timezone to a formatting function: they read it themselves.
Live example
timezone_cache= November 18, 2026, 15:30setTimezone("Europe/Amsterdam");setFirstDayOfWeek
function setFirstDayOfWeek(dayValue: string): void
function getFirstDayOfWeek(): stringSets the first day of the week, as a name from validDaysOfWeek ("monday" to "sunday", any case). getFirstDayOfWeek gives the name, monday when nothing is stored.
When to use
Use it in a settings screen, next to the other date preferences.
When not to use, and what to use instead
To build a calendar use getFirstDayOfWeekIndex, getWeekdayNames and getWeekdayOffset on Formatting: they give numbers and names that are ready to use.
Live example
first_day_of_week_cache= Mon Tue Wed Thu Fri Sat SunsetFirstDayOfWeek("sunday");setDateFormat
function setDateFormat(format: string): void
function getDateFormat(): string
function hasDateFormatPreference(): booleanSets the order of the parts of a date, one of validDateFormats: "DD-MM-YYYY" (day first), "MM-DD-YYYY" (month first) or "YYYY-MM-DD" (year first). The month is always a word. hasDateFormatPreference says whether the person chose an order: without one the formatting functions use the natural order of the language, and getDateFormat only gives YYYY-MM-DD as a default.
When to use
Use it when someone chooses how dates are written. Label each choice with a date written in that order by the engine itself, as in the example.
When not to use, and what to use instead
Do not use getDateFormat to write a date yourself: formatDate does that, and it handles the missing preference.
Live example
date_format_cache= November 18, 2026setDateFormat("DD-MM-YYYY");
// A label for a choice in a settings screen
formatDate(Date.UTC(2026, 10, 30), { weekday: false });createTranslationEngine
function createTranslationEngine<T extends string>(appRuntime: ParaglideRuntimeType<T>): (databasePreference?: string | null) => Promise<void>createTranslationEngine(paraglideRuntime) binds the Paraglide runtime of your app to the engine and returns an async function that picks the first language: from the language_cache cookie or local storage, then from the databasePreference you pass, then from the browser. init calls it without a preference, and once the session is found the language of the account is applied. LANGUAGE_CACHE_KEY, TIMEZONE_CACHE_KEY, FIRSTDAYOFWEEK_CACHE_KEY and DATEFORMAT_CACHE_KEY are the names of the cookies.
When to use
Call it yourself only in an app that does not use AppShell, or that sets disableInit. See the Init engine.
When not to use, and what to use instead
Do not call it twice, and not in an app with AppShell: init already did. To change the language later use setLanguage.
Live example
const initializeLanguage = createTranslationEngine(paraglideRuntime);
await initializeLanguage(user.preferences.language);