Transliteration & parsing¶
These methods convert text between scripts, detect look-alike strings, and parse locale-formatted numbers, money, and dates back into values.
| Method | Use it for |
|---|---|
Transliterate(text, id) |
Run any ICU transform (script conversion, decomposition) |
Romanize(text) |
The common "to ASCII Latin" case — slugs, search keys |
Confusable(a, b) |
Are these two strings visually confusable? |
Suspicious(text) |
Is this single string spoof-prone (mixed-script)? |
ParseNumber / ParseMoney / ParseDate / ParseMoment |
The inverse of the formatters |
These methods use ICU transforms, spoof checks, and parsers.
Transliterate & romanize¶
Transliterate(text, id) runs any ICU transform. The id is a transform
specification — a source/target pair, optionally chained with ;:
| id | Effect |
|---|---|
Greek-Latin |
Greek script → Latin |
Any-Latin |
any script → Latin (best-effort) |
Latin-ASCII |
strip Latin diacritics to plain ASCII |
Any-Latin; Latin-ASCII |
the chain most slugs want |
Any-Hangul, Cyrillic-Latin, Han-Latin, … |
other script pairs |
Romanize(text) is the pre-chained common case (Any-Latin; Latin-ASCII) — to
Latin, then stripped to ASCII — ideal for slugs and search keys.
var c = new Cosmo("en");
c.Romanize("Москва"); // "Moskva"
c.Transliterate("Λάμδα", "Greek-Latin"); // "Lámda"
c.Transliterate("Łódź café", "Any-Latin; Latin-ASCII"); // "Lodz cafe"
An unknown transform id throws. The full list of ids available on your runtime
comes from SupportedValues("transliterator")
— the one SupportedValues key beyond the standard six.
Spoof detection¶
Confusable(a, b) asks whether two strings are visually confusable (e.g. a Cyrillic
look-alike of a Latin brand). Suspicious(text) flags a single string as
spoof-prone — typically because it mixes scripts that shouldn't appear together.
Backed by UTS #39
Both checks implement Unicode Technical Standard #39, Unicode Security
Mechanisms — the standard that defines
the confusable-character data and mixed-script detection rules — through ICU's
SpoofChecker.
var c = new Cosmo("en");
c.Confusable("paypal", "раураl"); // true
c.Confusable("hello", "world"); // false
c.Suspicious("pаypal"); // true
c.Suspicious("paypal"); // false
Use these to defend account names, domains, and brand mentions against homograph
attacks — Suspicious() on signup to reject mixed-script usernames, Confusable()
to flag a new name that mimics an existing one.
Parsing (inverse formatters)¶
Intl can only format, never parse — but ICU's parsers are available in the other
this library. Each reads the kind of text produced by its corresponding formatter:
| Parser | Inverts | Returns |
|---|---|---|
ParseNumber(text) |
Number() |
a float |
ParseMoney(text) |
Money() |
amount and currency |
ParseDate(text, width?) |
Date() |
a native date (width defaults to short) |
ParseMoment(text, pattern) |
FormatMoment() |
a native date from a raw pattern |
new Cosmo("de").ParseNumber("1.234,56"); // 1234.56 (German grouping)
new Cosmo("en").ParseNumber("1,234.56"); // 1234.56
new Cosmo("en-US").ParseMoney("$12.30"); // (12.3, "USD")
var utc = new Cosmo("en-US", new Modifiers(timeZone: "UTC"));
utc.ParseDate("February 2, 2020", "long"); // DateTimeOffset
utc.ParseMoment("2020-02-02", "yyyy-MM-dd"); // DateTimeOffset
A few things worth knowing:
- The locale matters —
ParseNumber("1.234,56")reads1234.56indebut would read1.234inen. Parse in the same locale you formatted (or the user's input locale). ParseDate()'swidthmust match how the text was produced — a"long"string won't parse with theshortskeleton. Set atimeZonemodifier so the resulting moment is anchored, not shifted by the runtime zone.ParseMoney()returns the amount and the recognised currency as a(double, string)tuple.- Text that doesn't parse throws rather than returning 0 — so you can reliably distinguish a real"0"from unparseable input. Wrap user input in a try/catch.
Practical examples¶
A URL slug generator. Romanize to ASCII, lower-case, then squeeze non-alphanumerics to hyphens: