gizlang:locale
the linguistic data of the Unicode CLDR (48.2.3, pinned; the CLDR generator writes the tables at the end of this file) and the algorithms that read it, once for the server, the browser and workers. Nothing here is a foreign runtime: a locale is a value built from a record, and every function is a pure function of that value and its arguments. tags BCP 47 (RFC 5646) canonical case, CLDR likely scripts and parent locales; RFC 4647 lookup with script awareness plurals the CLDR cardinal and ordinal rules of every language, compiled to Gizlang conditions (operands n i v w f t c) numbers decimal, percent, currency and compact patterns with the locale's symbols, grouping and digits dates the Gregorian date, time and date-time styles, a set of skeletons, and patterns; an instant and an offset relative "yesterday", "in 3 days", "5 minutes ago"; lists "a, b, and c"; the name of a language in itself Unicode data: https://www.unicode.org/license.txt (CLDR is under the Unicode License v3).
import * as Locale from "gizlang:locale";Types
NumberPattern
type NumberPattern = { prefix: string; suffix: string; negPrefix: string; negSuffix: string; minInt: i64; minFraction: i64; maxFraction: i64; primary: i64; secondary: i64; }A number pattern of CLDR (#,##0.###, ¤#,##0.00, #,##0%), read once.
prefix:stringsuffix:stringnegPrefix:stringnegSuffix:stringminInt:i64minFraction:i64maxFraction:i64primary:i64secondary:i64
Locale
type LocaleA locale with data: build one with locale(tag). tag is the tag asked for, canonical (pt-BR); data the locale whose data it reads
(pt); chain the fallback chain of tag (pt-BR, pt), which a catalog of messages follows too.
tag:stringdata:stringchain:Vec<string>language:stringscript:stringregion:stringrtl:bool
Collator
type Collator = { lang: string; strength: i64; upperFirst: bool; shifted: bool; numeric: bool; backwards: bool; caseLevel: bool; libc: bool; tailor: Vec<i64>; }A collator: sort keys of text for a language, as ICU or the C library order it.
lang:stringstrength:i64— 1 primary, 2 secondary, 3 tertiary, 4 quaternary, 5 identical.upperFirst:boolshifted:bool— Whitespace and punctuation weigh only at the fourth level (alternate=shifted).numeric:bool— Runs of digits weigh as their numeric value.backwards:bool— Accents compare from the end of the text (French).caseLevel:bool— A case level follows the secondary level.libc:bool— The C library order: punctuation and symbols are shifted too and weigh by code point.tailor:Vec<i64>— Tailored letters, five values each: base, combining mark or 0, primary, second primary or 0, secondary.
Functions
canonical
function canonical(tag: string): stringTAG in the canonical case of BCP 47 (ko, zh-Hant-TW, es-419), _ read as -, with the deprecated language codes CLDR replaces
(iw is he, in is id, ji is yi, jw is jv, mo is ro). Extensions (-u-…, -x-…) are dropped: no presentation decision reads them.
Effects: fail.
wellFormed
function wellFormed(tag: string): boolWhether TAG is a well-formed language tag of the form this module reads: a language of 2 or 3 letters (or 5 to 8), then an optional script, region and variants. A request or a file names a locale only through a tag that passes.
Effects: fail.
likelyScript
function likelyScript(tag: string): stringThe likely script of TAG (ko is Kore, zh-TW is Hant, sr-ME is Latn), or "" when CLDR has none.
Effects: fail.
rightToLeft
function rightToLeft(tag: string): boolWhether TAG is written right to left: by its script, or by the likely script of its language and region.
Effects: fail.
parent
function parent(tag: string): stringThe parent of TAG in CLDR inheritance: parentLocales (en-GB is en-001, es-MX is es-419, zh-Hant is the root), else TAG
without its last subtag; "" at the root.
Effects: fail.
chain
function chain(tag: string): Vec<string>The fallback chain of TAG, most specific first: TAG canonical, with its likely script added when the region decides it (zh-TW is
zh-Hant-TW, zh-Hant), then its parents. zh-TW never falls back to zh, whose script is Hans.
Effects: fail.
acceptLanguage
function acceptLanguage(header: string): Vec<string>The tags of an Accept-Language header, best first (by q, then in order); * and q=0 are left out.
Effects: fail.
best
function best(requested: Vec<string>, supported: Vec<string>): Option<string>The supported tag that serves REQUESTED best (RFC 4647 lookup over CLDR chains), or none. For each requested tag in order: a supported
tag in its chain (ko-KR takes ko, zh-TW takes zh-Hant), else a supported tag of the same language and script (pt takes
pt-BR, zh-CN takes zh and never zh-Hant).
Effects: fail.
available
function available(): Vec<string>The locales that have data, as canonical tags.
Effects: fail.
dataTag
function dataTag(tag: string): Option<string>The data locale for TAG: the first tag of its chain that has data (ko-KR is ko, zh-TW is zh-Hant-TW), or none.
Effects: fail.
locale
function locale(tag: string): Option<Locale>The locale of TAG: its numbers, dates and names from the first tag of its chain that has data, else from English (a language CLDR has no data for, such as Latin, still names its catalog and keeps its plural rules and direction). None for a malformed tag.
Effects: fail.
nameOf
function nameOf(tag: string): stringThe name of the language of TAG in itself, or TAG when no data has it.
Effects: fail.
plural
function plural(tag: string, n: i64): stringThe plural category ("zero", "one", "two", "few", "many" or "other") of the integer N in the language of TAG.
Effects: fail.
pluralShown
function pluralShown(tag: string, shown: string, ordinal: bool): stringThe category of a number as it is shown, in decimal digits with an optional compact exponent (1.50, 1.2c6 for 1.2M): the
operands of CLDR come from the visible digits, so 1 and 1.0 differ. ORDINAL picks the ordinal rules.
Effects: fail.
currencyDigits
function currencyDigits(code: string): i64The minor-unit digits of the currency CODE (KRW and JPY 0, most 2).
Effects: fail.
collationTag
function collationTag(name: string): Option<string>The ICU language tag of locale NAME, as uloc_toLanguageTag gives it: @colStrength=primary is und-u-ks-level1;
none for an unknown keyword.
Effects: fail.
collator
function collator(tag: string): Option<Collator>The collator of ICU locale TAG (sv, und-u-kf-upper, de@collation=phonebook, @colStrength=primary); none for a
keyword value ICU rejects.
Effects: fail.
libcCollator
function libcCollator(name: string): CollatorThe collator emulating the C library locale NAME (en_US.utf8).
Effects: fail.
lowerIn
function lowerIn(lang: string, text: string): stringTEXT in lower case by the rules of language LANG (Turkic I is ı).
Effects: fail.
upperIn
function upperIn(lang: string, text: string): stringTEXT in upper case by the rules of language LANG (Turkic i is İ).
Effects: fail.
cldrVersion
function cldrVersion(): stringThe CLDR version of the data.
Methods
Locale.slot
function Locale.slot(self: Locale, tags: Vec<string>): i64The place of the locale in TAGS, the locales of a catalog of messages, from 1; 0 when the catalog has none of its chain (the source
text then shows). pt-BR takes a catalog pt when there is no pt-BR. The code that the ghtml compiler writes picks its variant by it.
Effects: fail.
Locale.dir
function Locale.dir(self: Locale): stringThe direction of the locale's script: "rtl" or "ltr", for the dir attribute.
Locale.name
function Locale.name(self: Locale): stringThe name of the locale's language in itself ("한국어", "English", "日本語"), for a language picker.
Effects: fail.
Locale.plural
function Locale.plural(self: Locale, n: i64): stringThe cardinal category of the integer N.
Effects: fail.
Locale.ordinal
function Locale.ordinal(self: Locale, n: i64): stringThe ordinal category of N ("one" for 1st, "two" for 2nd, "few" for 3rd in English).
Effects: fail.
Locale.pluralOf
function Locale.pluralOf(self: Locale, x: f64, fraction: i64): stringThe cardinal category of X as it is shown with FRACTION digits (1.0 is "other" in English: the visible zeros count).
Effects: fail.
Locale.integer
function Locale.integer(self: Locale, n: i64): stringThe integer N in the locale (1,234,567, 1.234.567, ١٬٢٣٤).
Effects: fail.
Locale.number
function Locale.number(self: Locale, x: f64, max: i64): stringX with at most MAX fraction digits (1,234.5), by the decimal pattern of the locale.
Effects: fail.
Locale.fixed
function Locale.fixed(self: Locale, x: f64, digits: i64): stringX with exactly DIGITS fraction digits (1,234.50).
Effects: fail.
Locale.percent
function Locale.percent(self: Locale, x: f64, max: i64): stringThe ratio X as a percent (0.256 is 26%, 25,6 % with one fraction digit in French).
Effects: fail.
Locale.currencySymbol
function Locale.currencySymbol(self: Locale, code: string): stringThe symbol of the currency CODE in the locale ($, US$, ₩), or CODE.
Effects: fail.
Locale.money
function Locale.money(self: Locale, minor: i64, code: string): stringThe amount MINOR, in minor units of CODE (cents for USD, won for KRW), in the currency pattern of the locale: $1,234.50, ₩1,235.
An amount is an integer of minor units: a float never decides money.
Effects: fail.
Locale.compact
function Locale.compact(self: Locale, x: f64): stringX in the short compact form of the locale (1.2K, 12K, 1.2만, 12억): the CLDR pattern of its power of ten, with two digits of
precision as browsers show it; below 1000, the number itself.
Effects: fail.
Locale.pattern
function Locale.pattern(self: Locale, ms: i64, offset: i64, pattern: string): stringThe instant MS (milliseconds since 1970, UTC) at OFFSET seconds east of UTC, in the CLDR date pattern PATTERN of the locale
(y. M. d., MMM d, y, HH:mm). Quoted text stays; the zone letters show GMT+9.
Effects: fail.
Locale.date
function Locale.date(self: Locale, ms: i64, offset: i64, style: string): stringThe date of the instant MS at OFFSET seconds, in the STYLE of the locale: "full", "long", "medium" or "short".
Effects: fail.
Locale.time
function Locale.time(self: Locale, ms: i64, offset: i64, style: string): stringThe time of day of the instant MS at OFFSET seconds, in the STYLE of the locale.
Effects: fail.
Locale.dateTime
function Locale.dateTime(self: Locale, ms: i64, offset: i64, date: string, time: string): stringThe date and time of MS at OFFSET seconds: the date in DATE style and the time in TIME style, joined as the locale joins them.
Effects: fail.
Locale.skeleton
function Locale.skeleton(self: Locale, ms: i64, offset: i64, skeleton: string): stringMS at OFFSET seconds by a CLDR skeleton of the locale: yMMMd, yMMM, MMMd, MMMEd, yMd, Md, Hm, hm, Hms, hms, Ed, yMMMEd, MMMMd or yMMMMd. Another skeleton is the medium date.
Effects: fail.
Locale.relative
function Locale.relative(self: Locale, value: i64, unit: string): stringVALUE units UNIT from now ("year", "quarter", "month", "week", "day", "hour", "minute" or "second"): a word where the locale has one ("yesterday", "내일"), else the pattern of the plural category ("in 3 days", "3일 전").
Effects: fail.
Locale.since
function Locale.since(self: Locale, now: i64, then: i64): stringThe instant THEN seen from NOW (both in milliseconds) in the largest whole unit: "just now" is 0 seconds, then minutes, hours, days, weeks, months (30 days) and years (365 days).
Effects: fail.
Locale.list
function Locale.list(self: Locale, items: Vec<string>, kind: string): stringITEMS joined as the locale lists them: KIND "and" (a, b, and c), "or" or "unit" (3 h, 5 min).
Effects: fail.
Collator.tailored
function Collator.tailored(self: Collator, rules: string): Option<Collator>A copy tailored by ICU RULES such as &a < g < h; none for rules it cannot read.
Effects: fail.
Collator.key
function Collator.key(self: Collator, text: string): bytesThe sort key of TEXT: keys compare as bytes in the collation order.
Effects: fail.
Collator.compare
function Collator.compare(self: Collator, a: string, b: string): i64Compares A and B in the collation order: -1, 0 or 1.
Effects: fail.