Skip to content
mylanconnollyPublic

About

A package that implements some of the CLDR formatting functionality

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CLDR for Typst

Unicode CLDR (Common Locale Data Repository) formatting for Typst. Provides locale-aware number and date/time formatting.

Installation

#import "@preview/cldr:0.1.0": number, date, time, dt

Features

  • Number formatting: Decimal, percent, scientific, and currency formats
  • Date formatting: Short, medium, long, and full date formats
  • Time formatting: Short, medium, long, and full time formats
  • DateTime formatting: Combined date and time formatting
  • 10 locales: English, Chinese, Hindi, Spanish, French, Arabic, Bengali, Portuguese, Russian, Japanese

Usage

Number Formatting

#import "@preview/cldr:0.1.0": number

// Standard decimal formatting
#number.format(1234.56)                    // "1,234.56"
#number.format(1234.56, locale: "de")      // "1.234,56"
#number.format(1234.56, locale: "fr")      // "1 234,56"

// Percent formatting
#number.format(0.15, format: "percent")    // "15%"
#number.format(0.857, format: "percent")   // "86%"

// Currency formatting
#number.format(99.99, format: "currency", currency: "USD")  // "$99.99"
#number.format(99.99, format: "currency", currency: "EUR")  // "€99.99"
#number.format(1234.56, format: "currency", currency: "JPY", locale: "ja")  // "¥1,235"

// Scientific notation
#number.format(1234567, format: "scientific")  // "1.234567E6"

// Control decimal places
#number.format(3.14159, max-fraction-digits: 2)  // "3.14"
#number.format(5, min-fraction-digits: 2)        // "5.00"

Date Formatting

#import "@preview/cldr:0.1.0": date

#let today = datetime.today()

// Format types
#date.format(today)                        // "Dec 19, 2025" (medium)
#date.format(today, format: "short")       // "12/19/25"
#date.format(today, format: "long")        // "December 19, 2025"
#date.format(today, format: "full")        // "Friday, December 19, 2025"

// Different locales
#date.format(today, locale: "fr")          // "19 déc. 2025"
#date.format(today, locale: "de")          // "19.12.2025"
#date.format(today, locale: "ja")          // "2025/12/19"
#date.format(today, locale: "zh")          // "2025年12月19日"

// Get month/day names
#date.month-name(12)                       // "December"
#date.month-name(12, width: "abbreviated") // "Dec"
#date.day-name(0)                          // "Sunday"

Time Formatting

#import "@preview/cldr:0.1.0": time

#let now = datetime.today()

// Format types
#time.format(now)                          // "3:45:30 PM" (medium)
#time.format(now, format: "short")         // "3:45 PM"

// Different locales (24-hour vs 12-hour)
#time.format(now, locale: "de")            // "15:45:30"
#time.format(now, locale: "fr")            // "15:45:30"

DateTime Formatting

#import "@preview/cldr:0.1.0": dt

#let now = datetime.today()

// Combined date and time
#dt.format(now)                            // "Dec 19, 2025, 3:45:30 PM"
#dt.format(now, format: "short")           // "12/19/25, 3:45 PM"
#dt.format(now, format: "full")            // "Friday, December 19, 2025, 3:45:30 PM"

// Mix date and time formats
#dt.format(now, date-format: "long", time-format: "short")  // "December 19, 2025, 3:45 PM"

Supported Locales

Code Language
en English
zh Chinese
hi Hindi
es Spanish
fr French
ar Arabic
bn Bengali
pt Portuguese
ru Russian
ja Japanese
de German

API Reference

number.format(num, ..)

Format a number with locale-specific formatting.

Parameter Type Default Description
num number required The number to format
locale string "en" Locale code
format string "standard" Format type: "standard", "percent", "currency", "scientific"
currency string none Currency code (required for currency format)
min-fraction-digits int varies Minimum decimal places
max-fraction-digits int varies Maximum decimal places

date.format(dt, ..)

Format a date with locale-specific formatting.

Parameter Type Default Description
dt datetime required The datetime to format
locale string "en" Locale code
format string "medium" Format type: "short", "medium", "long", "full"

time.format(dt, ..)

Format a time with locale-specific formatting.

Parameter Type Default Description
dt datetime required The datetime to format
locale string "en" Locale code
format string "medium" Format type: "short", "medium", "long", "full"

dt.format(dt, ..)

Format a combined date and time with locale-specific formatting.

Parameter Type Default Description
dt datetime required The datetime to format
locale string "en" Locale code
format string "medium" Format type: "short", "medium", "long", "full"
date-format string none Override date format
time-format string none Override time format

Development

Regenerating Locale Data

The locale data is generated from Unicode CLDR JSON files. To regenerate:

cd scripts
python3 generate.py

This will:

  1. Download CLDR JSON data (cached in .cache/)
  2. Parse number and date formatting data
  3. Generate Typst data files in data/

Adding New Locales

Edit scripts/config.py and add locale codes to the LOCALES list, then regenerate.

License

MIT License - see LICENSE

Credits

About

A package that implements some of the CLDR formatting functionality

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages