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 in utils/format.ts are not, because they do not know the preferences.
Where the preferences are kept
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 needUse
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 anotherformatDateRange
The title of a calendar monthformatMonthYear
How long ago something happened, or how soonformatRelativeTime
A number, a percentage or an amount of moneyformatNumber, formatPercent, formatCurrency
A big number in a small space (1.3M)formatCompactNumber
The size of a fileformatBytes
Several items in one sentenceformatList
A text for one item or for severalgetPluralCategory
The names of months or weekdaysgetMonthName, getMonthNames, getWeekdayName, getWeekdayNames
A calendar grid, with the week starting where the person wantsgetFirstDayOfWeekIndex, getWeekdayOffset, getStartOfWeek
A list of timezones to choose fromgetTimezones, getBrowserTimezone

Formatting

Every result on this page is live. Switch the language, and the results change with it.
Never use 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

typescript
typescript
function formatDate(value: Date | number | string, options?: DateFormatOptions): string

A 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

NameTypeDefaultDescription
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"falseAdds 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

CallResult
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")""
typescript
typescript
formatDate(ms);
formatDate(ms, { weekday: false });
formatDate(ms, { size: "compact", weekday: true, time: true });

formatCalendarDate

typescript
typescript
function formatCalendarDate(date: string, options?: Omit<DateFormatOptions, "time">): string

A 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

NameTypeDefaultDescription
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

CallResult
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
typescript
typescript
formatCalendarDate("2026-11-18");
formatCalendarDate("2026-11-18", { size: "compact" });

formatDateRange

typescript
typescript
function formatDateRange(start: DateInput, end: DateInput, options?: Pick<DateFormatOptions, "size" | "year">): string

Two 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

NameTypeDefaultDescription
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

CallResult
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
typescript
typescript
formatDateRange(start, end);
formatDateRange(start, end, { size: "compact" });

formatMonthYear

typescript
typescript
function formatMonthYear(value: DateInput | { year: number; month: number }, size?: "full" | "compact"): string

A 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

CallResult
formatMonthYear(ms)November 2026
formatMonthYear({ year: 2026, month: 10 })November 2026
formatMonthYear(ms, "compact")Nov 2026
typescript
typescript
formatMonthYear({ year: 2026, month: 10 });
formatMonthYear(ms, "compact");

formatRelativeTime

typescript
typescript
function formatRelativeTime(value: DateInput): string

How 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

CallResult
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
typescript
typescript
formatRelativeTime(Date.now() - 2 * 60 * 60 * 1000);

formatNumber

typescript
typescript
function formatNumber(value: number, options?: { minimumFractionDigits?: number; maximumFractionDigits?: number }): string

A 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

NameTypeDefaultDescription
minimumFractionDigits number—The fewest decimals to show.
maximumFractionDigits number—The most decimals to show.

Live example

CallResult
formatNumber(100000.5)100,000.5
formatNumber(1234567)1,234,567
formatNumber(3.14159, { maximumFractionDigits: 2 })3.14
formatNumber(3, { minimumFractionDigits: 2 })3.00
typescript
typescript
formatNumber(100000.5);
formatNumber(3.14159, { maximumFractionDigits: 2 });

formatPercent

typescript
typescript
function formatPercent(fraction: number, options?: { minimumFractionDigits?: number; maximumFractionDigits?: number }): string

A 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

NameTypeDefaultDescription
minimumFractionDigits number—The fewest decimals to show.
maximumFractionDigits number—The most decimals to show.

Live example

CallResult
formatPercent(0.005)0.5%
formatPercent(0.256)25.6%
formatPercent(1)100%
typescript
typescript
formatPercent(0.256);

formatCurrency

typescript
typescript
function formatCurrency(value: number, currency: string): string

An 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

CallResult
formatCurrency(100000.5, "EUR")€100,000.50
formatCurrency(19.99, "USD")$19.99
typescript
typescript
formatCurrency(100000.5, "EUR");

formatCompactNumber

typescript
typescript
function formatCompactNumber(value: number): string

A 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

CallResult
formatCompactNumber(999)999
formatCompactNumber(12500)13K
formatCompactNumber(1250000)1.3M
typescript
typescript
formatCompactNumber(1250000);

formatBytes

typescript
typescript
function formatBytes(bytes: number): string

A 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

CallResult
formatBytes(512)512 byte
formatBytes(1200000)1.2 MB
formatBytes(5368709120)5.4 GB
typescript
typescript
formatBytes(file.size);

formatList

typescript
typescript
function formatList(items: string[], type?: "and" | "or"): string

Items 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

CallResult
formatList(["Ana", "Ben", "Chris"])Ana, Ben, and Chris
formatList(["Ana", "Ben"])Ana and Ben
formatList(["Ana", "Ben", "Chris"], "or")Ana, Ben, or Chris
typescript
typescript
formatList(["Ana", "Ben", "Chris"]);
formatList(["Ana", "Ben", "Chris"], "or");

getPluralCategory

typescript
typescript
function getPluralCategory(count: number): Intl.LDMLPluralRule

The 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

CallResult
getPluralCategory(0)other
getPluralCategory(1)one
getPluralCategory(2)other
getPluralCategory(5)other
typescript
typescript
const category = getPluralCategory(count);
const text = category === "one" ? m.files_one() : m.files_other({ count: formatNumber(count) });

getMonthName

typescript
typescript
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

CallResult
getMonthName(10)November
getMonthName(10, "compact")Nov
getMonthNames("compact")Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
typescript
typescript
getMonthName(10);
getMonthNames("compact");

getWeekdayName

typescript
typescript
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

CallResult
getWeekdayName(3)Wednesday
getWeekdayName(3, "compact")Wed
getWeekdayNames("compact")Mon Tue Wed Thu Fri Sat Sun
typescript
typescript
getWeekdayName(3);
getWeekdayNames("compact");

getFirstDayOfWeekIndex

typescript
typescript
function getFirstDayOfWeekIndex(): number

The 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

CallResult
getFirstDayOfWeekIndex()1

getWeekdayOffset

typescript
typescript
function getWeekdayOffset(date: Date): number
function getStartOfWeek(date: Date): Date

The 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

CallResult
getWeekdayOffset(new Date(2026, 10, 1))6
formatDate(getStartOfWeek(new Date(2026, 10, 18)), { weekday: false })November 16, 2026
typescript
typescript
// The column of the first of the month in a month grid
const column = getWeekdayOffset(new Date(2026, 10, 1));

getTimezones

typescript
typescript
function getTimezones(): string[]
function getBrowserTimezone(): string

Every 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

CallResult
getTimezones().length445
getTimezones().slice(0, 3)Africa/Abidjan, Africa/Accra, Africa/Addis_Ababa
getBrowserTimezone()UTC
Older functions
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

In an app inside AppShell you rarely call these yourself: AppShell restores them from the cookies and from the account of the signed-in person. Call them from a settings screen.

setLanguage

typescript
typescript
function setLanguage(newLocale: string): void

Changes 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

Checking the session…
Result Language: en-us Cookie: language_cache= November 18, 2026 You are signed out: the cookies are all there is, and the only place where the choice is kept.
typescript
typescript
setLanguage("nl");

currentLocale

typescript
typescript
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

CallResult
currentLocale.valueen-us
typescript
typescript
const language = $derived(currentLocale.value);

setTimezone

typescript
typescript
function setTimezone(tz: string): void
function getTimezone(): string

Sets 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

Checking the session…
Result Timezone: UTC Cookie: timezone_cache= November 18, 2026, 15:30
typescript
typescript
setTimezone("Europe/Amsterdam");

setFirstDayOfWeek

typescript
typescript
function setFirstDayOfWeek(dayValue: string): void
function getFirstDayOfWeek(): string

Sets 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

Checking the session…
Result First day of the week: monday Cookie: first_day_of_week_cache= Mon Tue Wed Thu Fri Sat Sun
typescript
typescript
setFirstDayOfWeek("sunday");

setDateFormat

typescript
typescript
function setDateFormat(format: string): void
function getDateFormat(): string
function hasDateFormatPreference(): boolean

Sets 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

Checking the session…
Result Date order: none chosen (the natural order) Cookie: date_format_cache= November 18, 2026
These buttons really set the preference, in a cookie in this browser, not in your account. Reload the page and the choice is still there.
typescript
typescript
setDateFormat("DD-MM-YYYY");

// A label for a choice in a settings screen
formatDate(Date.UTC(2026, 10, 30), { weekday: false });

createTranslationEngine

typescript
typescript
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

typescript
typescript
const initializeLanguage = createTranslationEngine(paraglideRuntime);
await initializeLanguage(user.preferences.language);