Class Cosmo
Assembly: Miloun.Cosmo.dll
Syntax
public sealed class Cosmo
Constructors
Cosmo(string?, Modifiers?)
Builds a Cosmo for the given locale, optionally with modifier overrides.
Declaration
public Cosmo(string? locale, Modifiers? modifiers = null)
Parameters
| Type |
Name |
Description |
| string |
locale |
BCP-47 or underscore id (en_AU, fa-IR); null/blank uses the system default.
|
| Modifiers |
modifiers |
Optional calendar / currency / timeZone overrides.
|
Properties
Locale
Canonical ICU locale id, e.g. "en_AU".
Declaration
public string Locale { get; }
Property Value
Modifiers
Resolved modifiers (calendar / currency / timeZone).
Declaration
public Modifiers Modifiers { get; }
Property Value
Parsed language / script / region subtags.
Declaration
public Subtags Subtags { get; }
Property Value
Methods
A new Cosmo with likely subtags added ("en" → "en_Latn_US").
Declaration
public Cosmo AddLikelySubtags()
Returns
BestMatch(IEnumerable<string>)
Unsupported on the C API — needs CLDR-distance LocaleMatcher.
Declaration
public string BestMatch(IEnumerable<string> supported)
Parameters
Returns
Calendar(string?)
Localised calendar name ("buddhist" → "Buddhist Calendar").
Declaration
public string Calendar(string? code)
Parameters
| Type |
Name |
Description |
| string |
code |
|
Returns
Compact(double, string)
Compact notation (1200 → "1.2K"); "long"/"full" → "1.2 thousand".
Declaration
public string Compact(double value, string width = "short")
Parameters
Returns
Compare(string, string, CollationOptions?)
Locale-aware comparison of two strings (negative / zero / positive).
Declaration
public int Compare(string a, string b, CollationOptions? options = null)
Parameters
Returns
Confusable(string, string)
Whether two strings are visually confusable per UTS #39. Locale-independent.
Declaration
public bool Confusable(string a, string b)
Parameters
Returns
Contains(string, string, string, CollationOptions?)
Locale-aware substring test (accents/case can be ignored).
Declaration
public bool Contains(string haystack, string needle, string sensitivity = "base", CollationOptions? options = null)
Parameters
Returns
Country()
Localised name of this locale's own region.
Declaration
Returns
Country(string?)
Localised country/region name ("AU" → "Australia").
Declaration
public string Country(string? code)
Parameters
| Type |
Name |
Description |
| string |
code |
|
Returns
Currency()
Localised name of the currency modifier's currency.
Declaration
Returns
Currency(string?, bool, bool)
Localised currency name ("AUD" → "Australian Dollar") or symbol.
Declaration
public string Currency(string? code, bool symbol = false, bool strict = false)
Parameters
| Type |
Name |
Description |
| string |
code |
ISO 4217 code; null falls back to the currency modifier.
|
| bool |
symbol |
Return the symbol ("A$") instead of the name.
|
| bool |
strict |
Throw on an unknown code instead of echoing it back.
|
Returns
Date(DateTimeOffset, string)
Format just the date part of a moment.
Declaration
public string Date(DateTimeOffset value, string width = "short")
Parameters
Returns
DateRange(DateTimeOffset, DateTimeOffset, string, string)
Format a moment range; supports the documented width combinations only.
Declaration
public string DateRange(DateTimeOffset start, DateTimeOffset end, string dateWidth = "medium", string timeWidth = "none")
Parameters
Returns
Direction()
Text direction of this locale: "rtl" or "ltr".
Declaration
public string Direction()
Returns
Direction(string?)
Text direction of a language: "rtl" or "ltr" (script-based).
Declaration
public string Direction(string? language)
Parameters
| Type |
Name |
Description |
| string |
language |
|
Returns
DisplayName(string, string)
Generic localised display name — one entry point over the dedicated lookups.
Declaration
public string DisplayName(string type, string code)
Parameters
Returns
Duration(IDictionary<string, double>, bool)
Format a multi-unit duration ({hours:3, minutes:5} → "3 hr, 5 min").
Declaration
public string Duration(IDictionary<string, double> parts, bool withWords = false)
Parameters
Returns
Duration(double, bool)
Format an undirected duration in seconds as the clock form ("339:17:20").
Declaration
public string Duration(double seconds, bool withWords = false)
Parameters
| Type |
Name |
Description |
| double |
seconds |
|
| bool |
withWords |
|
Returns
Ellipsize(string, int, string)
Truncate to at most maxGraphemes graphemes on a word boundary.
Declaration
public string Ellipsize(string text, int maxGraphemes, string ellipsis = "…")
Parameters
Returns
Flag()
Country flag emoji for this locale's region ("" when it has none).
Declaration
Returns
Flag(string?)
Country flag emoji for a region ("AU" → 🇦🇺). Pure codepoint math.
Declaration
public string Flag(string? country)
Parameters
| Type |
Name |
Description |
| string |
country |
|
Returns
Format a moment with a raw ICU pattern ("yyyy-MM-dd"), optionally forcing a calendar.
Declaration
public string FormatMoment(DateTimeOffset value, string pattern, string? calendar = null)
Parameters
Returns
FromAcceptLanguage(string?, Modifiers?)
Builds a Cosmo from an HTTP Accept-Language header, picking the best-quality tag.
Declaration
public static Cosmo FromAcceptLanguage(string? header, Modifiers? modifiers = null)
Parameters
Returns
FromAcceptLanguage(string?, IEnumerable<string>, Modifiers?)
Negotiating variant — picks the supported locale that best serves the header.
Unsupported on the C API (no CLDR-distance LocaleMatcher).
Declaration
public static Cosmo FromAcceptLanguage(string? header, IEnumerable<string> supported, Modifiers? modifiers = null)
Parameters
Returns
Builds a Cosmo from locale subtags, e.g. new Subtags("en", "", "AU").
Declaration
public static Cosmo FromSubtags(Subtags subtags, Modifiers? modifiers = null)
Parameters
Returns
IndexBuckets(IEnumerable<string>)
Unsupported on the C API — AlphabeticIndex is C++-only.
Declaration
public IDictionary<string, List<string>> IndexBuckets(IEnumerable<string> names)
Parameters
Returns
Join(IEnumerable<string>, string, string)
Join a list the locale's way ("A, B, and C").
Declaration
public string Join(IEnumerable<string> items, string type = "conjunction", string width = "full")
Parameters
Returns
Language()
Localised name of this locale's own language.
Declaration
Returns
Language(string?)
Localised language name ("en" → "English").
Declaration
public string Language(string? code)
Parameters
| Type |
Name |
Description |
| string |
code |
|
Returns
Lower(string)
Locale-aware lower-casing.
Declaration
public string Lower(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
Message(string, IDictionary<string, object?>)
Format an ICU MessageFormat pattern with named placeholders. A faithful
subset (argument substitution + plural/selectordinal/select
with #), backed by ICU plural rules and number formatting — the C
umsg API supports neither named arguments nor a non-varargs entry.
Declaration
public string Message(string pattern, IDictionary<string, object?> args)
Parameters
Returns
Message(string, params object?[])
Format an ICU MessageFormat pattern with positional ({0}) placeholders.
Declaration
public string Message(string pattern, params object?[] args)
Parameters
Returns
Moment(DateTimeOffset, string, string)
Format a date and/or time using the locale's conventions.
Declaration
public string Moment(DateTimeOffset value, string dateWidth, string timeWidth)
Parameters
Returns
Money(double)
Formats a monetary value using the currency modifier.
Declaration
public string Money(double value)
Parameters
| Type |
Name |
Description |
| double |
value |
|
Returns
Money(double, string?, int?, bool, NumberOptions?)
Formats a monetary value in the given ISO 4217 currency.
Declaration
public string Money(double value, string? code, int? precision = null, bool strict = false, NumberOptions? options = null)
Parameters
Returns
MoneyRange(double, double, string?)
Format a monetary range ("$3.00 – $5.00"); "" if no currency.
Declaration
public string MoneyRange(double start, double end, string? code = null)
Parameters
Returns
MonthNames(string)
Localised month names, following the active calendar (Persian for fa_IR, etc.).
Declaration
public List<string> MonthNames(string width = "full")
Parameters
| Type |
Name |
Description |
| string |
width |
|
Returns
Number(double, NumberOptions?)
Format a number with the locale's default decimal format.
Declaration
public string Number(double value, NumberOptions? options = null)
Parameters
Returns
NumberRange(double, double)
Format a numeric range ("3–5").
Declaration
public string NumberRange(double start, double end)
Parameters
Returns
Ordinal(long)
Ordinal text (1 → "1st"). Uses ICU RBNF.
Declaration
public string Ordinal(long number)
Parameters
| Type |
Name |
Description |
| long |
number |
|
Returns
ParseDate(string, string)
Parse a localised date written at the given width.
Declaration
public DateTimeOffset ParseDate(string text, string width = "short")
Parameters
Returns
ParseMoment(string, string)
Declaration
public DateTimeOffset ParseMoment(string text, string pattern)
Parameters
Returns
ParseMoney(string)
Parse a localised monetary string ("$12.30" → 12.3 USD).
Declaration
public (double Amount, string Currency) ParseMoney(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
ParseNumber(string)
Parse a localised number ("1.234,56" in de → 1234.56).
Declaration
public double ParseNumber(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
Percentage(double, int, NumberOptions?)
Format a fraction as a percentage (0.2 → "20%").
Declaration
public string Percentage(double value, int precision = 3, NumberOptions? options = null)
Parameters
Returns
PersonName(IDictionary<string, string>, string, string)
Unsupported on this ICU build — PersonNameFormatter is ICU 73+ (this ships ICU 72).
Declaration
public string PersonName(IDictionary<string, string> fields, string length = "medium", string formality = "formal")
Parameters
Returns
PluralCategory(double, bool)
The LDML cardinal plural category for a value (1 → "one").
Declaration
public string PluralCategory(double value, bool ordinal = false)
Parameters
Returns
Precision(double, int, NumberOptions?)
Formats a number with a fixed number of fraction digits — always
exactly fractionDigits, padding with trailing zeros and
rounding as needed. Use it when 1 should render as "1.00" and
1.002 should stay "1.00", never "1.0". Pass
NumberOptions to widen the band or tweak rounding/grouping.
Declaration
public string Precision(double value, int fractionDigits = 2, NumberOptions? options = null)
Parameters
Returns
Quote(string)
Wrap text in the locale's quotation marks (“x” in en, «x» in fa).
Declaration
public string Quote(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
RelativeDuration(double, string, string)
Render a directed duration ((-3, "day") → "3 days ago").
Declaration
public string RelativeDuration(double amount, string unit, string numeric = "always")
Parameters
| Type |
Name |
Description |
| double |
amount |
|
| string |
unit |
|
| string |
numeric |
always ("1 day ago") or auto ("yesterday").
|
Returns
RelativeDurationBetween(DateTimeOffset, DateTimeOffset?, string)
Directed duration between two moments ("in 5 days", "3 days ago").
Declaration
public string RelativeDurationBetween(DateTimeOffset target, DateTimeOffset? reference = null, string numeric = "auto")
Parameters
Returns
A new Cosmo with likely subtags removed ("en_Latn_US" → "en").
Declaration
public Cosmo RemoveLikelySubtags()
Returns
Romanize(string)
Romanise text ("Москва" → "Moskva"); shorthand for Any-Latin.
Declaration
public string Romanize(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
Scientific(double)
Scientific notation (12345 → "1.2345E4").
Declaration
public string Scientific(double value)
Parameters
| Type |
Name |
Description |
| double |
value |
|
Returns
Script()
Localised name of this locale's own script ("" when it has none).
Declaration
Returns
Script(string?)
Localised script name ("Hans" → "Simplified Han").
Declaration
public string Script(string? code)
Parameters
| Type |
Name |
Description |
| string |
code |
|
Returns
Sort(IEnumerable<string>, CollationOptions?)
A new list sorted by the locale's collation rules.
Declaration
public List<string> Sort(IEnumerable<string> items, CollationOptions? options = null)
Parameters
Returns
Sort<T>(IEnumerable<T>, Func<T, string>, CollationOptions?)
Sort a collection of arbitrary items by a string key.
Declaration
public List<T> Sort<T>(IEnumerable<T> items, Func<T, string> key, CollationOptions? options = null)
Parameters
Returns
Type Parameters
Spellout(double)
Spell a number out (42 → "forty-two"). Uses ICU RBNF.
Declaration
public string Spellout(double number)
Parameters
| Type |
Name |
Description |
| double |
number |
|
Returns
SplitGraphemes(string)
Split text into grapheme clusters (combining marks / emoji stay intact).
Declaration
public List<string> SplitGraphemes(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
SplitSentences(string)
Split text into sentences using the locale's boundary rules.
Declaration
public List<string> SplitSentences(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
SplitWords(string)
Split text into words (drops whitespace/punctuation).
Declaration
public List<string> SplitWords(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
SupportedValues(string)
Values the runtime's ICU supports for key (e.g. all IANA time zones).
Declaration
public List<string> SupportedValues(string key)
Parameters
| Type |
Name |
Description |
| string |
key |
|
Returns
Suspicious(string)
Whether a string fails ICU's default spoof checks per UTS #39.
Declaration
public bool Suspicious(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
Symbol(string)
Returns a localised number symbol ("decimal", "percent", …).
Declaration
public string Symbol(string name)
Parameters
| Type |
Name |
Description |
| string |
name |
|
Returns
Time(DateTimeOffset, string)
Format just the time (clock) part of a moment.
Declaration
public string Time(DateTimeOffset value, string width = "short")
Parameters
Returns
TimeZoneName(string)
Display name of the timeZone modifier (or the system zone).
Declaration
public string TimeZoneName(string style = "long")
Parameters
| Type |
Name |
Description |
| string |
style |
long / short / longOffset / shortOffset / longGeneric / shortGeneric.
|
Returns
Transliterate(string, string)
Run an ICU transform over the text ("Any-Latin; Latin-ASCII" makes ASCII slugs).
Declaration
public string Transliterate(string text, string id)
Parameters
Returns
Unit(string, string, double, string)
Format a measurement with a localised unit (2.19 gigabytes).
Declaration
public string Unit(string category, string unit, double value, string width = "full")
Parameters
Returns
Upper(string)
Locale-aware upper-casing (e.g. Turkish dotted/dotless I).
Declaration
public string Upper(string text)
Parameters
| Type |
Name |
Description |
| string |
text |
|
Returns
WeekInfo()
Week conventions of the locale's region: first day, minimal days, weekend days.
Declaration
public WeekInfo WeekInfo()
Returns
WeekdayNames(string)
Localised weekday names, Sunday first (ICU symbol order).
Declaration
public List<string> WeekdayNames(string width = "full")
Parameters
| Type |
Name |
Description |
| string |
width |
|
Returns